-----------------------------------------------------------
New Message on BDOTNET
-----------------------------------------------------------
From: Pithvi
Message 5 in Discussion
A: I�ll divide the answer into 2 parts.
Syntax wise:
Here�s how one could define a delegate in a class:
public class Class1
{
public delegate void DomSomethingDelegate(int num1,int num2);
public DomSomethingDelegate MyDeleageteCallback;
}
And here�s how I would declare an event of this delegate:
public class Class2
{
public delegate void DomSomethingDelegate(int num1,int num2);
public event DomSomethingDelegate MyDeleageteCallback;
}
Notice that syntactically, the only difference is that I place an �event� keyword
before the delegate variable declaration. That�s it.
So what�s the �event� keyword adding to this?
Well, to understand this, consider Class1 in the code above. If I was using this
class, as a client I could set it�s delegate variable like this:
Class1 c = new Class1();
c.MyDeleageteCallback+=new
Class1.DomSomethingDelegate(this.Calculate);
This simple code adds a new target to the delegate�s invocation list, which is
perfectly fine. In fact, this same code will word with Class2. no difference what so
ever so far. But consider this code which, instead of adding a new target to the
delegate�s Invocation list, simply sets the delegate to a new delegate:
Class1 c = new Class1();
c.MyDeleageteCallback=new
Class1.DomSomethingDelegate(this.Calculate);
This piece of code will work just fine with class1, but if we were to try to use it on
class2, where there is the event keyword declared, we would get a compilation error.
In essence, declaring the event keyword prevents any of the delegate�s users from
setting it to null. Why is this important? Image that as a client I would add to the
delegates invocation list a callback to one of my class�s functions. So would other
clients. All is well and good. Now imagine that someone, instead of using the �+=�, is
simply setting the delegate to a new callback by using simply �=�. They basically just
threw the old delegate and its invocation list down the drain and created a whole new
delegate with a single item in its invocation list. All the other clients will not
receive their callbacks when the time comes. It is this kind of situation that having
the �event� keyword is aiming to solve.
In conclusion: an event declaration adds a layer of protection on the delegate
instance. This protection prevents clients of the delegate from resetting the delegate
and its invocation list, and only allows adding or removing targets from the
invocation list.
-----------------------------------------------------------
To stop getting this e-mail, or change how often it arrives, go to your E-mail
Settings.
http://groups.msn.com/bdotnet/_emailsettings.msnw
Need help? If you've forgotten your password, please go to Passport Member Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help
For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact
If you do not want to receive future e-mail from this MSN group, or if you received
this message by mistake, please click the "Remove" link below. On the pre-addressed
e-mail message that opens, simply click "Send". Your e-mail address will be deleted
from this group's mailing list.
mailto:[EMAIL PROTECTED]