|
-
May 20th, 2004, 12:17 AM
#1
Adding Class Events
The book I have here is pathetic.
It shows me how to add a class event, which is quite easy:
Now, let's say I wanted to raise an event with a couple arguments, how would I do that?
VB Code:
Public Event SetTraining(ByVal bolTrained As Boolean)
Did I do that right?
-
May 20th, 2004, 01:09 AM
#2
The code you posted declares the event to raise it you'd use Raiseevent just like in VB6. You can also declare an Event as a Delegate or use a system delegate.
VB Code:
'in class declaration of an event
Public Event Changed(ByVal sender as object, ByVal e as EventArgs)
'in class declaration of an event using a system delegate
Public Event Changed As EventHandler
'in class declaration of a delegate and event for it
Public Delegate Sub ChangedHandler(ByVal sender as object, ByVal e as EventArgs)
Public Event Changed as ChangedHandler
'in all cases you can raise the event from within the class like this
RaiseEvent Changed(Me, Nothing)
-
May 20th, 2004, 02:01 AM
#3
VB Code:
'in class declaration of an event
Public Event SetTraining(ByVal sender As Object, ByVal e As EventArgs)
I tried the above, and I Raised the event. Now, when handling it in the form, I tried doing:
VB Code:
MessageBox.Show(sender.vbnet.ToString)
Did I do this the right way, or is there another way of working with the sender object? I have been doing it this way all along, just wanted to confirm it.
Moving on,
VB Code:
'in class declaration of an event using a system delegate
Public Event SetTraining As EventHandler
I tried the above. And I saw that it automatically passes a "sender" and "e", so I assume that "EventHandler" was created for this very purpose. Am I right?
Next:
VB Code:
'in class declaration of a delegate and event for it
Public Delegate Sub ChangedHandler(ByVal sender as object, ByVal e as EventArgs)
How do I raise this above event? And how do I handle it? Trying RaiseEvent Changed(Me, Nothing) didn't work.
VB Code:
Public Event Changed as ChangedHandler
Doesn't work. "ChangedHandler" doesn't show up either.
What am I missing?
-
May 20th, 2004, 02:19 AM
#4
What is this suppose to do?
sender.vbnet.ToString
Whats the vbnet part?
Yes EventHandler is the default Event arguments and is a delegate. If you select it and press F1 or right click and hit 'Go To Definition' it will show you the signature and more info.
You can't raise a delegate only an event (which really is a delegate behind the scenes but that is another topic). And since the event declaration of Changed didn't work that is why the RaiseEvent didn't work also.
I'm not sure what happened with the ChangedHandler it should work fine I use it all the time. A delegate does not have to 'belong' to any other object thus it is good to declare it outside of any class or anything, but it will work the same either way, it just changes the namespace of it.
VB Code:
Public Delegate Sub ChangedHandler(ByVal sender As Object, ByVal e As EventArgs)
Public Class DelegateEventSample
Public Event Changed As ChangedHandler
Public Sub OnChanged(ByVal e As EventArgs)
RaiseEvent Changed(Me, e)
End Sub
End Class
-
May 20th, 2004, 03:29 AM
#5
Originally posted by Edneeis
What is this suppose to do?
sender.vbnet.ToString
Whats the vbnet part?
vbnet is a Boolean variable in the class I've created, I should have named it properly. What I was strying to do was take the sender (the class), it's vbnet variable, convert to string, and show in a message box.
I'm not sure what happened with the ChangedHandler it should work fine I use it all the time. A delegate does not have to 'belong' to any other object thus it is good to declare it outside of any class or anything, but it will work the same either way, it just changes the namespace of it.
VB Code:
Public Delegate Sub ChangedHandler(ByVal sender As Object, ByVal e As EventArgs)
Public Class DelegateEventSample
Public Event Changed As ChangedHandler
Public Sub OnChanged(ByVal e As EventArgs)
RaiseEvent Changed(Me, e)
End Sub
End Class
Got it, it's working now.
Mind if I ask one more question? What is the use of a delegate here? Why not simply raise an event?
-
May 20th, 2004, 09:34 PM
#6
In this example there is no real benefit. An event is a type of delegate and only in VB C# makes you declare your events as Delegates (which they are in VB behind the scenes). To access the Event as a Delegate in VB.NET You can append Event to the end of the name (i.e. Changed = ChangedEvent). A delegate is a great thing but in this example there are not huge differences. I just wanted to point out the different ways as an FYI.
Also its a good idea to cast your variables to a strong or proper type before using them if they are declared as the generic Object. It will save you trouble later. So since sender is declared as type Object in the event you should cast it to what you expect it to be when using it as a safety.
Msgbox(DirectCast(sender, MyClass).vbnet.ToString)
-
May 20th, 2004, 11:26 PM
#7
Thank you very much. I've understood (and learnt) quite a bit here.
When I take over the world, I'll give you a country of your own.
-
May 20th, 2004, 11:48 PM
#8
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|