-1

I have a code snipped I converted from c# to vb.net. However, the vb.net code does not allow me to query if an Event is instantiated. Here's the c# code:

    public event EventHandler<TreeModelEventArgs> NodesInserted;
    internal void OnNodeInserted(Node parent, int index, Node node)
    {
        if (NodesInserted != null)
        {
            TreeModelEventArgs args = new TreeModelEventArgs(GetPath(parent), new int[] { index }, new object[] { node });
            NodesInserted(this, args);
        }

    }

The specific line it 'acks' on is:

        if (NodesInserted!= null)

Here's the vb.net code:

Public Event NodesInserted As EventHandler(Of TreeModelEventArgs) Implements ITreeModel.NodesInserted
Friend Sub OnNodeInserted(parent As Node, index As Integer, node As Node)
  If NodesInserted IsNot Nothing Then
    Dim args As New TreeModelEventArgs(GetPath(parent), New Integer() {index}, New Object() {node})
    RaiseEvent NodesInserted(Me, args)
  End If

End Sub

Specifically:

  If NodesInserted IsNot Nothing Then

Returns an error, saying that I should use RaiseEvent, which of course I am using already.

My question is, do I even need to check for null? How could an event in c# ever be called if it didn't exist? I'm thinking I should just remove the 'If' statement, but this is a complicated project, and I might be missing something. Can an expert help me?

Andrew H
  • 395
  • 1
  • 7
  • 24

2 Answers2

2

You shouldn't need to check for 'Nothing' in VB, but if you want the equivalent, you'll need to use the hidden event field:

Public Event NodesInserted As EventHandler(Of TreeModelEventArgs)
Friend Sub OnNodeInserted(ByVal parent As Node, ByVal index As Integer, ByVal node As Node)
    If NodesInsertedEvent IsNot Nothing Then 'yes - add the 'Event'
        Dim args As New TreeModelEventArgs(GetPath(parent), New Integer() { index }, New Object() { node })
        RaiseEvent NodesInserted(Me, args)
    End If
End Sub

As I said, VB will handle it otherwise, but sometimes you just really want to know whether there are event subscribers or not.

Dave Doknjas
  • 6,394
  • 1
  • 15
  • 28
  • Ok, thanks so much for responding, but now I'm confused. As you can see, I DO Raise the event, I just do it after the check for null. Can you show me what the code would look like to raise the event, and then raise the event again? I'm not seeing it... How would I accomplish the equivalent of what is being done in c#? – Andrew H Feb 05 '16 at 05:32
  • 1
    All the null check does is ensure than you have at least one subscribed handler. If you use 'RaiseEvent' in VB without a null check, VB takes care of it and prevents an exception - i.e., VB does the null check for you. However, using the hidden (nearly undocumented) field ending with 'Event' will also allow you to check for subscribers. – Dave Doknjas Feb 05 '16 at 05:36
  • The C# example is only raising the event once. The code "NodesInserted != null" is not checking to see if a returned value from the function is null, it's checking to see if there is an event handler listening to the event. Looks similar to a function call in VB, but it's doing something different. – DenverJT Feb 05 '16 at 05:54
  • @DaveDoknjas, Could you be so kind as to post a link to the reference about the eventName**Event** field. I know you are correct about its existence, but I never seen a official reference to it. – TnTinMn Feb 05 '16 at 15:14
  • @TnTinMn: I wish I could find it - I spent a few minutes looking for the Microsoft documentation and couldn't find it - I know it's there somewhere (I know it works, and I know I've seen documentation for it once). – Dave Doknjas Feb 05 '16 at 17:28
  • @DaveDoknjas, Thanks for looking. I wonder if the text under "Defining a Class to Raise the Event" in [Raising an Event](https://msdn.microsoft.com/en-us/library/wkzf914z%28v=vs.100%29.aspx) is what you are thinking of, but it does not explicitly state what the naming convention is for the backing delegate instance. – TnTinMn Feb 05 '16 at 18:05
1

VB.net is different from C# in that it will take care of checking for you when you call RaiseEvent. However if you are interested in preserving most of the code above, change the check for nothing on "NodesInserted" to "NodesInsertedEvent". The NodesInsertedEvent variable is created automatically for this reason.

If NodesInsertedEvent IsNot Nothing Then
...
End If

Similar to this answer: https://stackoverflow.com/a/11142499/3325680

Community
  • 1
  • 1
DenverJT
  • 125
  • 7
  • Thanks Denver! That's exactly what I was looking for. I wish I could share the points, since you answered the question with much clarity, but Dave was first and he DID mention the 'Event' thing, though I didn't understand it. Thanks to both of you! Actually, now that I thought about it, you really did answer the question. Dave also did, but I didn't understand the adding of the 'Event'. I see from your code that you add it to the END of the event name...again, wish I could split the points. Sorry Dave. – Andrew H Feb 05 '16 at 06:09