|
-
Dec 9th, 2013, 09:33 AM
#1
-
Dec 9th, 2013, 10:07 AM
#2
Re: Where in code to put AddHandler
 Originally Posted by opus
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.
-
Dec 9th, 2013, 12:22 PM
#3
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:
Public Custom Event FirstNameChanging As FirstNameChangingEventHandler AddHandler(ByVal value As FirstNameChangingEventHandler) Me.firstNameChangingHandlers.Add(value) End AddHandler RemoveHandler(ByVal value As FirstNameChangingEventHandler) Me.firstNameChangingHandlers.Remove(value) End RemoveHandler RaiseEvent(ByVal sender As Object, ByVal e As StringPropertyChangingEventArgs) For Each handler As FirstNameChangingEventHandler In Me.firstNameChangingHandlers handler(sender, e) If e.Cancel Then Exit For Next End RaiseEvent 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!
-
Dec 9th, 2013, 12:50 PM
#4
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)
-
Dec 9th, 2013, 01:27 PM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|