Why can't check event instance is nothing?

  • Thread starter Thread starter Patrick
  • Start date Start date
P

Patrick

In C# I can check whether an event instance is nothing or not

public event SomeEvent SomeClickEvent;
if (SomeClickEvent != null)
{
}

But when I do this in VB it returns an error message

Public Event SomeClickEvent As SomeEvent

If Not SomeClickEvent Is Nothing Then

End If

Error Message: Public Event SomeClickEvent is an event, and cannot be called
directly. Use an RaiseEvent statement to raise an event.

Hope some one can give insights into this.
 
In C# I can check whether an event instance is nothing or not

public event SomeEvent SomeClickEvent;
if (SomeClickEvent != null)
{
}

But when I do this in VB it returns an error message

Public Event SomeClickEvent As SomeEvent

If Not SomeClickEvent Is Nothing Then

End If

Error Message: Public Event SomeClickEvent is an event, and cannot be called
directly. Use an RaiseEvent statement to raise an event.

Hope some one can give insights into this.

The reason you usually do that in C# is that, you want to raise the
event without getting null reference exception :) You don't have that
problem in VB.NET - since RaiseEvent will take care of it for you, so
it's safe to just raise the event without checking...

That said, why do you need to check this?
 
Hi Patrick,

As Tom has pointed out, you rarely need to do this check in VB as you simply
use RaiseEvent and VB worries about the storage mechanism for you, testing
to see if it is a null reference or not. That being said, there is a way in
VB, albeit undocumented (and hence unsupported)

Lets assume you declare an event named Foo. Vb creates a backing field for
the delegate called FooEvent, hence you can write If FooEvent IsNot Nothing
Then ..
 
Public Event SomeClickEvent As SomeEvent
If Not SomeClickEvent Is Nothing Then

End If

As Bill has mentioned, VB.NET likes to hide the event delegate. In
your example you would use the following:

///////////////
If SomeClickEventEvent IsNot Nothing Then

End If
//////////////

This is a scarcely documented feature of VB.NET, and is a bit scary if
you ask me. For some reason it seems the VB team tried to hide
delegates from the language, as evident from the RaiseEvent, AddressOf
and other keywords.

Thanks,

Seth Rowe [MVP]
 
For some reason it seems the VB team tried to hide
delegates from the language, as evident from the RaiseEvent, AddressOf
and other keywords.

The boffins at Microsoft have been trying to save us from ourselves since
VB4 when they deprecated Varptr etc.

Actually, they did it before then when they removed PEEK and POKE from the
language. It is all virtual address spaces anyway, why they can't leave the
memory-affecting keywords in the language and handle the memory corruption
issues at a lower level? Do even C++ programmers write to real (not virtual)
memory addresses nowadays?

I wish they'd just leave the unsafe stuff in the language and have it as an
option whether to use them or not (e.g. "Option Safe On" or something).

</rant>

--
David Streeter
Synchrotech Software
Sydney Australia
 
Back
Top