0

This might be a dumb question, but I just can't get this to work.

Using a 3rd party C# library, I need to get live data via an Event Handler, It goes something like this:

Library.KEvents bnoEvent = new Library.KEvents();
envtsList.Add(bnoEvent);
/* 
Some code to set bnoEvent up
*/
bnoEvent.OnEvent += new Library._IKEventsEvents_OnEventEventHandler(bnoEvent_OnEvent);

The function bnoEvent_OnEvent is never executing.

The demo code I got (in VB) looks like this:

If KEventsA Is Nothing Then
    Set KEventsA = New Library.KEvents
End If  
/* Setting KEventA up */

The method that is being called is:

Private Sub KEvents_OnEvent(data As KType)

This drives me CRAZY! I cannot find any connection between this function and the KEventA object! How does it know?

I know it is pretty hard to debug without knowing how this library actually works, but is there any major things I should be doing while registering for events beside declaring the function and adding it as a new eventHandler?

1
  • 2
    It is ancient VB6 era code. No specific event subscription was needed in that language, just providing a subroutine with the right name was enough. Your C# looks correct, there are of course gazillion ways in which ancient code won't do what you hope. Getting help from the author or vendor is your only real recourse. Commented Mar 30, 2014 at 19:02

1 Answer 1

1

Either use the Addhandler Statement or the Handles clause on the event sub. That is how VB sets up delegates.

If KEventsA Is Nothing Then
 KEventsA = New Library.KEvents
 Addhandler KEventsA.OnEvent, AddressOf KEvents_OnEvent
End If  

Or

'must use this for declarations for this way
Private WithEvents KEventsA As New Library.KEvents
'then the method
Private Sub KEvents_OnEvent(data As KType) Handles KEventsA.OnEvent

C#

Library.KEvents KEventsA = new Library.KEvents;
KEventsA.OnEvent += new EventHandler(KEvents_OnEvent);
5
  • You are correct, the VB code does declare : "Private WithEvents KEvents As Library.KEvents". But It never connects KEvent_OnEvent to KEventA. How can I make this connection in C#? Commented Mar 30, 2014 at 18:35
  • So are you saying it will not let you use the Handles clause?
    – OneFineDay
    Commented Mar 30, 2014 at 18:36
  • Or am I confused and you need the answer in C#?
    – OneFineDay
    Commented Mar 30, 2014 at 18:38
  • I didn't write the VB code. It's a demo code I got from the company. Their code doesn't user 'Handles' anywhere, yet it works. I am confused and would very much appreciate an answer in C# :) Commented Mar 30, 2014 at 18:39
  • 1
    @YotamVaknin The sample code you've got is VB6, not VB.NET. In VB6 there was no Handles or AddHandler, but there was WithEvents which worked under a naming convention. See Hans Passant's comment.
    – GSerg
    Commented Mar 30, 2014 at 19:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.