9

I have two objects - one that contains some code with will fire an event, and one that contains the handler for that event. I can't "AddHandler" in the Load of the first object, because an instance of the second object doesn't exist yet. When I raise my event, I want to check to see if a copy of object2 has been instantiated (easy to do), and if a handler has been attached to the event yet (not sure how to do this).

I'm also open to another recommendation about how to do this instead. If I do my AddHandler in Object1.Load, and Object2 doesn't exist yet, then it will never handle my event, even if I create it later. Right now, in the code that fires the event, I've just resorted to doing a RemoveHandler and then an AddHandler every single time the event is raised, and then I know I'll attach when the object finally exists, but I know this is a crappy method.

I saw an article about something similar (Determine list of event handlers bound to event), and maybe I'm missing something in the translation, but I can't get the code to work on my custom event in VB.NET.

Community
  • 1
  • 1
SqlRyan
  • 33,116
  • 33
  • 114
  • 199
  • 1
    Why can't you use withevents? – chrissie1 Jun 18 '09 at 14:44
  • I'm not sure how using WithEvents would affect my situation - it's not a matter of the event not being raised, or of the handler not working properly - they're both fine. It's just that I can't attach the handler until I have an instance of the object that will handle the event, so I'm unsure of best place to check for that. – SqlRyan Jun 18 '09 at 15:31

4 Answers4

24

VB.Net creates a special private member variable in the pattern of <YourEvent>Event that you can then use to test against Nothing.

Public Event MyClick As EventHandler

Private Sub OnMyClick()
    If MyClickEvent IsNot Nothing Then
        RaiseEvent MyClick(Me, New EventArgs())
    Else
        ' No event handler has been set.
        MsgBox("There is no event handler. That makes me sad.")
    End If
End Sub

http://blogs.msdn.com/b/vbteam/archive/2009/09/25/testing-events-for-nothing-null-doug-rothaus.aspx

Jon
  • 2,389
  • 2
  • 20
  • 35
  • Actually, in VB.Net Event cannot use like this. If EventName IsNot Nothing can only result a compiling error. – scegg Jun 03 '21 at 08:03
2

You could also just have a bool field that you check before hooking the event.

if not eventHooked then
 addhandler
 eventHooked = true
end if

Also if you need a good c# to vb converter http://www.tangiblesoftwaresolutions.com/ has one that can translate a 100 lines on the fly or less for or translate a project of a 1000 lines for free. More than that you have to purchase it, but that usually those limits will work just fine. No I am not trying to advertise for them :-)

NastyNateDoggy
  • 292
  • 1
  • 2
  • 14
  • I had considered this, and it's probably what I'll go with. I just figured there might be an easy way to detect if the event is already hooked, but if there's not, then this is my preferred method. – SqlRyan Jun 18 '09 at 15:26
  • Though I was hoping to avoid this solution, it solves my problem and works like a champ. Thanks for your help. – SqlRyan Jun 18 '09 at 20:07
1

According to the responses here: http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/9ec8ff1c-eb9b-4cb3-8960-9cd4b25434f2 (which seem to work according to my testing), a check for existing event handlers is done upon calling RaiseEvent. If you do not want to raise an event and just need to check to see if any handlers are attached, you can check the value of a hidden variable called <your_event_name>Event like:

Public Event Foo As ActionFoo

If FooEvent IsNot Nothing Then...
Vloxxity
  • 980
  • 1
  • 9
  • 30
Joshua Whitley
  • 1,196
  • 7
  • 21
  • Actually, in VB.Net Event cannot use like this. If EventName IsNot Nothing can only result a compiling error. – scegg Jun 03 '21 at 08:02
0

If you just want to know whether any handler has been attached, you should be able to check whether the event is null.

if (MyButton.Click == null)
{
    MyButton.Click += myEventHandler;
}

(I'll let you translate that into VB)

StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • I'm doing this for a custom event, not a standard one, and this code doesn't seem to work in this case - checking the value of MyEvent fails syntax, telling me "This is an event, and can't be called directly", so I can't even compile. I suppose this is something C# just handles differently than VB... – SqlRyan Jun 18 '09 at 15:28
  • @ChrisF: That's why I was wonderinf if he wants to know whether *any* handler was attached (versus a specific handler). Maybe he knows that he's the only one that will be touching it. @rwmnau: As I understand it, Events can only be directly accessed by their declaring class, so this code would have to be put into the class itself. – StriplingWarrior Jun 18 '09 at 15:41