Results 1 to 8 of 8

Thread: Adding Class Events

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Adding Class Events

    The book I have here is pathetic.

    It shows me how to add a class event, which is quite easy:

    VB Code:
    1. Public Event UnderAge()

    Now, let's say I wanted to raise an event with a couple arguments, how would I do that?

    VB Code:
    1. Public Event SetTraining(ByVal bolTrained As Boolean)

    Did I do that right?

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. 'in class declaration of an event
    2. Public Event Changed(ByVal sender as object, ByVal e as EventArgs)
    3.  
    4. 'in class declaration of an event using a system delegate
    5. Public Event Changed As EventHandler
    6.  
    7. 'in class declaration of a delegate and event for it
    8. Public Delegate Sub ChangedHandler(ByVal sender as object, ByVal e as EventArgs)
    9.  
    10. Public Event Changed as ChangedHandler
    11.  
    12. 'in all cases you can raise the event from within the class like this
    13. RaiseEvent Changed(Me, Nothing)

  3. #3

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    VB Code:
    1. 'in class declaration of an event
    2. 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:
    1. 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:
    1. 'in class declaration of an event using a system delegate
    2. 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:
    1. 'in class declaration of a delegate and event for it
    2. 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:
    1. Public Event Changed as ChangedHandler

    Doesn't work. "ChangedHandler" doesn't show up either.

    What am I missing?

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. Public Delegate Sub ChangedHandler(ByVal sender As Object, ByVal e As EventArgs)
    2.  
    3. Public Class DelegateEventSample
    4.  
    5.     Public Event Changed As ChangedHandler
    6.  
    7.     Public Sub OnChanged(ByVal e As EventArgs)
    8.         RaiseEvent Changed(Me, e)
    9.     End Sub
    10.  
    11. End Class

  5. #5

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    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:
    1. Public Delegate Sub ChangedHandler(ByVal sender As Object, ByVal e As EventArgs)
    2.  
    3. Public Class DelegateEventSample
    4.  
    5.     Public Event Changed As ChangedHandler
    6.  
    7.     Public Sub OnChanged(ByVal e As EventArgs)
    8.         RaiseEvent Changed(Me, e)
    9.     End Sub
    10.  
    11. 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?

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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)

  7. #7

    Thread Starter
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    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.

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Wow thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width