|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
delegatesHi there
I have a delegate that needs to have only 1 function to call. There is a possibility that the function will be added to the delegate a second time if the button is clicked twice. Is it "legal" to assign a delegate null before adding a function to the delegate. eg del = null; del += new delfunc(); Thanks Dave DiamondDavo <DiamondD***@discussions.microsoft.com> wrote:
> I have a delegate that needs to have only 1 function to call. There is a No need to do that - just do:> possibility that the function will be added to the delegate a second time if > the button is clicked twice. Is it "legal" to assign a delegate null before > adding a function to the delegate. eg > del = null; > del += new delfunc(); del = new delfunc(); However, neither of these will work if instead of a delegate variable, you're actually dealing with an event, which only has subscribe/unsubscribe behaviour. -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too
Show quote
"Jon Skeet [C# MVP]" wrote: Hi Jon> DiamondDavo <DiamondD***@discussions.microsoft.com> wrote: > > I have a delegate that needs to have only 1 function to call. There is a > > possibility that the function will be added to the delegate a second time if > > the button is clicked twice. Is it "legal" to assign a delegate null before > > adding a function to the delegate. eg > > del = null; > > del += new delfunc(); > > No need to do that - just do: > > del = new delfunc(); > > However, neither of these will work if instead of a delegate variable, > you're actually dealing with an event, which only has > subscribe/unsubscribe behaviour. > > -- > Jon Skeet - <sk***@pobox.com> > http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet > If replying to the group, please do not mail me too > Thanks for that. It is actually an event. I tried "del = new delfunc();" but the compiler barfs with only += and -= are valid. Is there a way I can make sure it is only assigned once? Thanks Dave DiamondDavo <DiamondD***@discussions.microsoft.com> wrote:
<snip> > Thanks for that. It is actually an event. I tried "del = new The best way is to design the problem away - change your design to make > delfunc();" but the compiler barfs with only += and -= are valid. Is > there a way I can make sure it is only assigned once? sure that it's only going to be added once in the first place. Alternatively, keep a flag to say whether or not you've already added the delegate in question. -- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too DiamondDavo schreef:
> Hi there make the event private, and implement accessor methods to do the > > I have a delegate that needs to have only 1 function to call. There is a > possibility that the function will be added to the delegate a second time if > the button is clicked twice. verifications... private event EventHandler bar; public event EventHandler Bar { add { // only subscribe if there aren't other subscribers... if (this.bar.GetInvocationList().Length == 0) { this.bar += value; } } remove { this.bar -= value; } } DiamondDavo schreef:
> Hi there Wrap the event in a property and implement the add method so that it > > I have a delegate that needs to have only 1 function to call. There is a > possibility that the function will be added to the delegate a second time if > the button is clicked twice. Is it "legal" to assign a delegate null before > adding a function to the delegate. eg > del = null; > del += new delfunc(); verifies
Show quote
"Tim Van Wassenhove" wrote: Thanks very much Tim and Jon> DiamondDavo schreef: > > Hi there > > > > I have a delegate that needs to have only 1 function to call. There is a > > possibility that the function will be added to the delegate a second time if > > the button is clicked twice. Is it "legal" to assign a delegate null before > > adding a function to the delegate. eg > > del = null; > > del += new delfunc(); > > Wrap the event in a property and implement the add method so that it > verifies > > > -- > Tim Van Wassenhove <url:http://www.timvw.be/> > Thats got me going. Cheers Dave |
|||||||||||||||||||||||