Events question

Razor89

Member
Joined
Dec 15, 2004
Messages
12
I dont know if this should have gone in the general area or what but since
I use VB.NET and I have no clue if C# supports events and all that stuff
I will ask my question here.

Now for my question.
I have seen some events in my coding experience that have the following
parameters for example:

Public Event MyEvent(Byval Cancel As Boolean) Handles Whatever.MyEvent

Now in events like these if you set Cancel to TRUE, whatever should happen
after the event is raised will stop. How do I do this in my own classes?

I tried playing with ByRef and all that stuff to pass a variable from inside
my sub that raises the event and then checking the value of that variable
after the event has been raised, but no luck.

Thanks for any input!
 
The best way to do this would be to create your own arguments object and expose a boolean property (my example uses a public variable for the sake of simplicity). Here is the example:

Public Class Class1

Public Event Hello(ByVal cancel As ARGS)

Public Sub RaiseTheEvent()

Dim res As New ARGS
RaiseEvent Hello(res)
The message will now show the value of cancel that is there, it will be different
if you changed it in the event handler
MessageBox.Show(res.cancel.ToString())
of course you can do anything you want with that value, i just used the MBox
as an example

End Sub

End Class


Public Class ARGS

Public cancel As Boolean = False

End Class
 
Last edited by a moderator:
Back
Top