Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I don't 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!

  • *Experts*
Posted (edited)

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

Edited by mutant

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...