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?
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
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
So the delegate in use is SerialReceivedEventHandler
Hope this helps
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.
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++
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)
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...
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:)