[Mono-list] Little question ...
Nick Berardi
nberardi@zigamorph.com
Thu, 19 Feb 2004 16:12:12 -0500
Yeah that is true and I don't think anybody is debating that event is =
very
limited in what it can do. But I think he was more asking why have =
events.
The "no support for assignment" is something that I forgot to include in =
my
last e-mail. Here is the text from the actual language specification.
"
Since +=3D and -=3D are the only operations that are permitted on an =
event
outside the type that declares the event, external code can add and =
remove
handlers for an event, but cannot in any other way obtain or modify the
underlying list of event handlers.
In an operation of the form x +=3D y or x -=3D y, when x is an event and =
the
reference takes place outside the type that contains the declaration of =
x,
the result of the operation has type void (as opposed to having the type =
of
x, with the value of x after the assignment). This rule prohibits =
external
code from indirectly examining the underlying delegate of an event.
"
In addition there is a special attribute modifier for events, like the
following.
[event: MyEventAttribute]
public event EventHandler MyEvent;
delegates have one but it is defined the same that a class is. =20
(i.e. [type: MyClassAttribute])
Hope this helped out a little.
-----Original Message-----
From: Jaroslaw Kowalski [mailto:jaak@zd.com.pl]=20
Sent: Thursday, February 19, 2004 3:46 PM
To: Nick Berardi; mono-list@lists.ximian.com
Subject: Re: [Mono-list] Little question ...
I believe that events are there to solve the problem overwriting the
devegate instead of appending a new handler to it.
Events only support add/remove (+=3D, -=3D), there's no support for =
assignment.
All you can do is:
myObject.RightListItemChanged +=3D new EventHandler =
(myObject_ItemChanged);
myObject.RightListItemChanged -=3D new EventHandler =
(myObject_ItemChanged);
With delegates you would be able to:
myObject.RightListItemChanged =3D new EventHandler =
(myObject_ItemChanged);
which would overwrite all handlers in the delegate chain.
Jarek
----- Original Message -----=20
From: "Nick Berardi" <nberardi@zigamorph.com>
To: <mono-list@lists.ximian.com>
Sent: Thursday, February 19, 2004 9:13 PM
Subject: RE: [Mono-list] Little question ...
> Sergio,
>
> Events are more like delegate properties. Such is you can do the
following.
> This is good if you want to encapsulate a child objects event. Such =
as
how
> I am doing it below. When somebody calls the following:
>
> myObject.RightListItemChanged +=3D new EventHandler =
(myObject_ItemChanged);
>
> this is what happens underneath in the code. It sets the child =
objects
> SelectedItemChange to the method that I selected above. Even though =
it is
> twice removed. In addition it enabled AutoPostBack.
>
> public event EventHandler RightListItemChanged {
> add
> {
> deniedListBox.SelectedIndexChanged +=3D value;
> deniedListBox.AutoPostBack =3D true;
> }
> remove
> {
> deniedListBox.SelectedIndexChanged -=3D value;
> deniedListBox.AutoPostBack =3D false;
> }
> }
>
> -----Original Message-----
> From: mono-list-admin@lists.ximian.com
> [mailto:mono-list-admin@lists.ximian.com] On Behalf Of Sergio Blanco
> Cuaresma
> Sent: Thursday, February 19, 2004 2:56 PM
> To: mono-list@lists.ximian.com
> Subject: Re: [Mono-list] Little question ...
>
> El s=E1b, 07-02-2004 a las 17:21, Miguel de Icaza escribi=F3:
> > Hey,
> >
> > > Only one little question: What are the main differences between =
using
> > > "event" keyword or not when creating delegates?
> > >
> > > Example:
> > >
> > > delegate void ProbeDelegate (string msg);
> > >
> > > ProbeDelegate d1;
> > > event ProbeDelegate d2;
> >
> > events can notify more than one function, a delegate only points to =
a
> > single method.
> >
> > So you can have multiple "listeners".
>
> But, only using delegates you can do:
>
> DelegadoOperacion multiD;
> multiD =3D new DelegadoOperacion(s.suma);
> multiD +=3D new DelegadoOperacion(s.resta);
>
> And then call multiD which will call more than one method. So what is
> the real difference with events ??
>
> Sergio.
>
> --=20
>
> [aka Marble]
> Web Personal <> http://www.marblestation.com
> Registered LiNUX user #140941 <> http://counter.li.org/
> Socio #3274 de HispaLinux <> http://www.hispalinux.es
> Miembro de GPL URV <> http://www.gplurv.org
> GnuPG key: 0x0ED2CF9D <> hkp://pgp.escomposlinux.org
>
> _______________________________________________
> Mono-list maillist - Mono-list@lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
>