Event handlers

  • Thread starter Thread starter Theo Verweij
  • Start date Start date
T

Theo Verweij

Does anyone know if there is a way to check if an event is handled?

Example code:

If EventIsHandled then 'Is this possible?
RaiseEvent MyEvent(Me, New System.EventArgs)
Else
'Do something else ...
End If
 
Theo said:
Does anyone know if there is a way to check if an event is handled?

Example code:

If EventIsHandled then 'Is this possible?
RaiseEvent MyEvent(Me, New System.EventArgs)
Else
'Do something else ...
End If

An event handler is the result of having raised an event. That's where
you put the code to execute, in the body of the event handler.
Why would you want to raise some other event if an event is raised? And
if the event is not raised, won't your application just proceed as normal?

T
 
Yes, I know how it works.

But what I try to do is give the user of the component the possibility
to take some actions, like formating or translating some strings by
implementing/handling an event.

When the user of this component chooses not to implement this, I want
the component to fall back to a default formatting.

An example:

If EventIsHandled(MyEvent) then 'just made up some syntax
RaiseEvent MyEvent(Me, New MyFormattingEventArgs(aStringToFormat))
Else
'Do the standard formatting
End If

And, by typing this, I found the solution.
I just have to put the value of Nothing into the FormattedResult of
MyFormattingEventArgs.
If it is still Nothing after RaiseEvent, the event was not handled, so I
can do the standard formatting.

Thanks for your input!
 
You need to use the hidden (nearly undocumented) <event>Event variable:

If MyEventEvent Then
RaiseEvent MyEvent(Me, New System.EventArgs)
Else
'Do something else ...
End If

--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
 
David Anton said:
You need to use the hidden (nearly undocumented) <event>Event variable:

If MyEventEvent Then
RaiseEvent MyEvent(Me, New System.EventArgs)
Else
'Do something else ...
End If

ACK, but the test is not even necessary because it's already being performed
by 'RaiseEvent'.
 
Personally, I don't do this, but I've come across a lot of code where the
hidden Event variable is checked to ensure that if it is null/Nothing, some
other branch of code is executed (even though the RaiseEvent checks for it
anyway).
Btw, my earlier post should have had:
If Not MyEventEvent Is Nothing Then
RaiseEvent MyEvent(Me, New System.EventArgs)
Else
'Do something else ...
End If
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
 
Back
Top