EventHandler problem

Help
qwertyfax
2005-07-20
2013-03-22
  • qwertyfax
    qwertyfax
    2005-07-20

    Hi,

    I am programming in C# and I have this problem with eventhandler:(

    I am trying to add eventhandler for ReceivedEvent
    SPort.ReceivedEvent+= new System.EventHandler(this.SPort_ReceivedEvent);

    where SPort is OpenNETCF.IO.Ports.SerialPort SPort

    , but I get error:
    Cannot implicitly convert type 'System.EventHandler' to 'OpenNETCF.IO.Ports.SerialReceivedEventHandler'

    Could someone please help me with this?

     
    • Mr ChriZ
      Mr ChriZ
      2005-07-20

      The Received Event uses it's own delegate type to declare an event handler. (If you don't understand this have a read around about delegates and events).

      If you are using Visual Studio 2003 onwards you should be able to do this
      SPort.ReceivedEvent+=
      then press tab and it will insert the right type of delegate for you, then press tab again and it will actually create the event handler for you!

      mine looks like this
      serialCommsHandler.ReceivedEvent+=new SerialReceivedEventHandler(serialCommsHandler_ReceivedEvent);

      So the delegate in use is SerialReceivedEventHandler

      Hope this helps
      Mr (hriZ

       
    • qwertyfax
      qwertyfax
      2005-07-20

      Big thanks Mr ChriZ,

      it helped, but now I have a new error at line

      SPort.ReceivedEvent +=new OpenNETCF.IO.Ports.SerialReceivedEventHandler(this.SPort_ReceivedEvent);

      during debugging pops up a window which says:
      An unhandled exception of type 'System.NullReferenceException' occurred in serial port project.exe

      Additional information: Object reference not set to an instance of an object.

       
      • Mr ChriZ
        Mr ChriZ
        2005-07-20

        OK, not much to go on there, and I've gotta leave in a minute.
        Somewhere however you've got an object that is null.
        Have you definitly created your SPort object?
        Remembering this is not C++
        ie
        SPort = new SerialPort( ..... what ever is needed here)

        And does your SPort_ReceivedEvent correctly implement the SerialReceivedEventHandler delegate
        ie should take the form
        public/private/protected void SPort_ReceivedEvent(object sender, SerialReceivedEventArgs e)
        {

        }

         
    • Mr ChriZ
      Mr ChriZ
      2005-07-20

      Oh one other thing to try also....
      Make sure you are not trying to create the event in a static method

      ie straight from Main.

      Thats asking for trouble...

       
    • qwertyfax
      qwertyfax
      2005-07-20

      Thanks for your patience! That was it:) I'll try to look in some books about C# so I wouldn't have to ask this kind of questions again:)

       
    • Mr ChriZ
      Mr ChriZ
      2005-07-20

      Your welcome!