Results 1 to 5 of 5

Thread: [RESOLVED] Where in code to put AddHandler

  1. #1

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Resolved [RESOLVED] Where in code to put AddHandler

    Hi, I'm using dynamically created "Transmitters" and "Recievers".
    The Recievers are created once during the Load-Event of the Starting Form. The Transmitters get created and deleted during runtime.

    For the Transmitters I declared an Public "Transmit"-Event which is raised by this object, that way I believe each of the instantiated Transmitters (correct wording??) to be able to raise that Event.
    Now I want each of the Recievers react on that event individually. Where shall I put the "AddHandler" Statement?

    -At each creating of a new Transmitter for each Reciever (and RemoveHandler when Deleting)??

    or is there a better way?


    @jmcilhinney: If you have explained that in your blog, I didn't get it.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  2. #2
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Where in code to put AddHandler

    Quote Originally Posted by opus View Post
    Where shall I put the "AddHandler" Statement?

    -At each creating of a new Transmitter for each Reciever (and RemoveHandler when Deleting)??
    Without seeing the code in question, I'd say that sounded right.

  3. #3

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Where in code to put AddHandler

    OK,
    I've done that (at the moment the AddHanler part only) and it works.
    I did put the code in the main part (i.e. outside the class of the Reciever). Would it be possible to include that in the Reciever Class?
    Maybe that is what jmcilhinney was explaining in his Blog and I didn't quite understand.
    In his example he came up with this Declaration:
    vb.net Code:
    1. Public Custom Event FirstNameChanging As FirstNameChangingEventHandler
    2.     AddHandler(ByVal value As FirstNameChangingEventHandler)
    3.         Me.firstNameChangingHandlers.Add(value)
    4.     End AddHandler
    5.  
    6.     RemoveHandler(ByVal value As FirstNameChangingEventHandler)
    7.         Me.firstNameChangingHandlers.Remove(value)
    8.     End RemoveHandler
    9.  
    10.     RaiseEvent(ByVal sender As Object, ByVal e As StringPropertyChangingEventArgs)
    11.         For Each handler As FirstNameChangingEventHandler In Me.firstNameChangingHandlers
    12.             handler(sender, e)
    13.             If e.Cancel Then Exit For
    14.         Next
    15.     End RaiseEvent
    16. End Event
    I'm not sure whow this code would work, if it would solve my problem and where to put it.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  4. #4
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Where in code to put AddHandler

    Hmmm, custom events in VB are implemented on the raising class a bit differently than in C#, but basically that code you posted goes inside the Transmitter class, the FirstNameChangingEventHandler delegate (? I presume) can either be nested (if it is very strongly tied to the Transmitter class - make sure it's public though!) or (preferably) in its own file. I would tend to use the generic EventHandler(Of T) definition though and just define a FirstNameChangingEventArgs class.

    The code that calls AddHandler I would expect to find outside the Receiver class - because I would have the work of the Receivers orchestrated externally; if that's not how you've set up your processing flow then it would be slightly different.

    The other thing to note is that it is idiomatic .NET to have events be raised after something happens and be named in the past tense. Using the gerund form of the name is then an additional event (i.e. you wouldn't have an -ing event without a corresponding -ed event) that occurs before the thing actually happens and one of the EventArg properties is a cancel flag that event handlers can use to signal that the thing should not happen (at which point the thing should not happen and the -ed event obviously doesn't get raised)

  5. #5

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Where in code to put AddHandler

    Thanks for the input. Your last coment is on the naming of Events, I posted code from jmcilhinneys Blog, in which he used ..Changing and Changed Events. He did all you are referring too.

    As you suggest I'l put the AddHandler and RemoveHandler Parts outside the Reciever Class!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

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