Results 1 to 10 of 10

Thread: Events?

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Question Events?

    I just need someone to tell me if i am on the right track to understanding events in .Net. I havent picked up VB.Net yet so i can't try out these concepts.

    Say for instance that i want to set up an event in a class. Would these be the appropriate steps that i would take?

    1.) Create a class with the event defined within it usign the Event keyword.

    2.) Create a new instance of the class that contains the event
    using the WithEvents keyword.

    3.)Set up the listener using the Handles keyword in the method signature mapping the method to the event being raised.

    4.) Raise the event using the RaiseEvent keyword.

    Any help would be sweet.

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

    VB Code:
    1. 'in class
    2. Public Event Change(ByVal Sender as object, e as System.EventArgs)
    3.  
    4. 'whereever you want it to fire
    5. RaiseEvent Change(Me,Nothing)
    6.  
    7. 'in app
    8. Dim WithEvents MyClass as New ClassName()
    9.  
    10. Public Sub MyClass_Change(ByVal Sender as object,e as System.EventArgs) Handles MyClass.Change
    11.     Msgbox "Fired"
    12. End Sub

    You can also do it without the WithEvents by using the AddHandler method:
    VB Code:
    1. 'in app
    2. Dim MyClass as New ClassName()
    3. AddHandler MyClass.Change, AddressOf SometingChanged
    4.  
    5. Public Sub SomethingChanged(ByVal Sender as object,e as System.EventArgs)
    6.     Msgbox "Fired"
    7. End Sub
    8.  
    9. 'during clean up remove the handler also
    10. RemoveHandler MyClass.Change, AddressOf SometingChanged
    Last edited by Edneeis; Aug 11th, 2002 at 01:34 AM.

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Wow! Easy enough. One more question. In terms of
    speed would the linkage be better at compile time using
    the WithEvents Handles approach or at runtime using the AddHandler, AddressOf method. Meaning, if i don't need the added flexibility of adding listener dynamically should i just go with the WithEvents Handles. Thanks for the help.

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I don't know if there is any speed difference or not but if you I would think the code would be easier to follow if you use the WithEvents way when you can. Otherwise you might not realize that a method is being used as an event handler unless you scan through the whole code.

  5. #5
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    umm what do you use WithEvents for?

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    So you can then use Handles to handle events. Otherwise you get an 'Handles clause requires a WithEvents variable.' error.

  7. #7
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    hmm how come it's not needed when you are creating controls dynamically?
    For example you create a textbox dynamically and you use AddHandler to add the Text_Change event to it... How come that one works without the WithEvents thingie?

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Thats true you can use AddHandler and RemoveHandler without declaring it WithEvents but you can't use Handles. Its early binding vs. late binding.

  9. #9
    New Member
    Join Date
    Jul 2002
    Location
    Germoney
    Posts
    9
    Cool,
    But is it able to Raise Events for arrays too?
    I wanted to code a Winsock-like Socket class but to
    do that I need arrays for each created socket.
    If there is a event like OnConnect or DataArrival I would
    like to trigger something using Handle.
    But I cant use WithEvents because I get a error:
    WithEvents cannot be declared with variables typed as arrays.

    What do do?

  10. #10
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    This is in VB 6 but the same concepts still apply:
    http://www.vbforums.com/showthread.p...hreadid=166968

    You have a collection class that can track and receive events for its children and then pass them along to the app.

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