Home All Groups Group Topic Archive Search About
Author
12 Apr 2007 5:44 AM
DiamondDavo
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();

Thanks
Dave

Author
12 Apr 2007 6:57 AM
Jon Skeet [C# MVP]
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
Author
13 Apr 2007 5:50 AM
DiamondDavo
Show quote
"Jon Skeet [C# MVP]" wrote:

> 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
>

Hi Jon

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
Author
13 Apr 2007 6:31 AM
Jon Skeet [C# MVP]
DiamondDavo <DiamondD***@discussions.microsoft.com> wrote:

<snip>

> 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?

The best way is to design the problem away - change your design to make
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
Author
13 Apr 2007 4:09 PM
Tim Van Wassenhove
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.

make the event private, and implement accessor methods to do the
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; }
}


--
Tim Van Wassenhove <url:http://www.timvw.be/>
Author
13 Apr 2007 4:10 PM
Tim Van Wassenhove
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/>
Author
24 Apr 2007 9:24 PM
DiamondDavo
Show quote
"Tim Van Wassenhove" wrote:

> 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/>
>

Thanks very much Tim and Jon

Thats got me going.

Cheers
Dave

AddThis Social Bookmark Button