|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Events in C#invoke it ? The sample code is provided below - I have two events - 'my' and 'ev' - while invoking, my() works but ev() does not ! The compiler complains that : The event "'Blog.EventsvsDelegates.Form1.ev' can only appear on the left hand side of += or -=" Why ? public delegate void MyEvent(); public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private event MyEvent my; private event MyEvent pvt_ev; public event MyEvent ev { add { pvt_ev +=value; } remove { pvt_ev-=value; } } public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); my+=new MyEvent(this.Test); my+=new MyEvent(this.Test1); ev+=new MyEvent (this.Test ); } protected void Test() { MessageBox.Show ("Test"); } protected void Test1() { MessageBox.Show ("Test1"); } /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void button1_Click(object sender, System.EventArgs e) { my(); // WORKS //ev (); //DOES NOT WORK } } pSm wrote:
> When I override the 'add' and 'remove' methods for an event, why can't I The help page for this error (CS0079) includes a sample very similar to > invoke it ? The sample code is provided below - I have two events - 'my' and > 'ev' - while invoking, my() works but ev() does not ! The compiler complains > that : The event "'Blog.EventsvsDelegates.Form1.ev' can only appear on the > left hand side of += or -=" > > Why ? yours that shows you exactly what to do. I found this out by reproducing your sample then hitting F1 with the error selected... -- Larry Lard larryl***@googlemail.com The address is real, but unread - please reply to the group For VB and C# questions - tell us which version Thanks Larry.
But this does not really answer my question ! Why does the compiler restrict the invocation ? pSm <p**@discussions.microsoft.com> wrote:
> When I override the 'add' and 'remove' methods for an event, why can't I And it's right. An event is really just an add/remove pair of methods. > invoke it ? The sample code is provided below - I have two events - 'my' and > 'ev' - while invoking, my() works but ev() does not ! The compiler complains > that : The event "'Blog.EventsvsDelegates.Form1.ev' can only appear on the > left hand side of += or -=" (There's also raise, but that doesn't get exposed in C#.) When you declare a field-like event, that's really declaring both the event *and* a delegate. When you access the name within the class, it refers to the delegate. When you access the name outside the class, it refers to the event. Delegates can be invoked - events can't. -- 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 > And it's right. An event is really just an add/remove pair of methods. No, there is no such thing as "raise"!> (There's also raise, but that doesn't get exposed in C#.) When you Events are stored as multicast delegates and used as such. > declare a field-like event, that's really declaring both the event Well events are just delegate, and of course they could be ".Invoke(..)", as > *and* a delegate. When you access the name within the class, it refers > to the delegate. When you access the name outside the class, it refers > to the event. Delegates can be invoked - events can't. any other delegate. But a semantic akin to private/protect/public prevent to do it from anywhere but the declaring class itself. Show quote > > -- > 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 Lloyd Dupont wrote:
> > And it's right. An event is really just an add/remove pair of methods. C# doesn't expose it, but it's part of the .NET event system.> > (There's also raise, but that doesn't get exposed in C#.) When you > > No, there is no such thing as "raise"! > Events are stored as multicast delegates and used as such. See CLI spec partition 1 section 10.4. > > declare a field-like event, that's really declaring both the event No, events aren't delegates any more than properties are variables. An> > *and* a delegate. When you access the name within the class, it refers > > to the delegate. When you access the name outside the class, it refers > > to the event. Delegates can be invoked - events can't. > > Well events are just delegate, and of course they could be ".Invoke(..)", as > any other delegate. event is solely a set of appropriately decorated methods. It's possible (although it would be very odd) to implement an event without actually storing a delegate. You'd need to store pretty much the same information, but the only ways in which events are intrinsically linked to delegates are: 1) C#'s field-like event support, which declares a delegate variable and an event 2) the signatures of event methods. > But a semantic akin to private/protect/public prevent to do it from anywhere Events only have add/remove semantics, along with the raise as> but the declaring class itself. mentioned before. Whether they are implemented by having a delegate variable is an implementation matter. Some implementations keep the delegates for all events in a hashtable, so that you don't need a variable per event where there are lots of events which may not be subscribed to at all. Jon give pvt_ev a public accessor.
otherwise it's quite obvious, if you have no access to it, how could you hope invoke it?!?! Show quote "pSm" <p**@discussions.microsoft.com> wrote in message news:8A65460C-9937-41BD-958B-50078772C822@microsoft.com... > When I override the 'add' and 'remove' methods for an event, why can't I > invoke it ? The sample code is provided below - I have two events - 'my' > and > 'ev' - while invoking, my() works but ev() does not ! The compiler > complains > that : The event "'Blog.EventsvsDelegates.Form1.ev' can only appear on the > left hand side of += or -=" > > Why ? > > public delegate void MyEvent(); > public class Form1 : System.Windows.Forms.Form > { > private System.Windows.Forms.Button button1; > private event MyEvent my; > > private event MyEvent pvt_ev; > public event MyEvent ev > { > add > { > pvt_ev +=value; > } > > remove > { > pvt_ev-=value; > } > } > > public Form1() > { > // > // Required for Windows Form Designer support > // > InitializeComponent(); > > my+=new MyEvent(this.Test); > my+=new MyEvent(this.Test1); > > ev+=new MyEvent (this.Test ); > } > > protected void Test() > { > MessageBox.Show ("Test"); > } > > protected void Test1() > { > MessageBox.Show ("Test1"); > } > > /// <summary> > /// The main entry point for the application. > /// </summary> > [STAThread] > static void Main() > { > Application.Run(new Form1()); > } > > private void button1_Click(object sender, System.EventArgs e) > { > my(); // WORKS > //ev (); //DOES NOT WORK > } > > } > Lloyd,
Why would pvt_ev() need a public accessor when I invoke it from the same class ? In fact any 'event' that is declared as public, would be converted to 'private' by the compiler - events can never be invoked from outside it's class. I was checking the MSIL and noticed that when 'add' and 'remove' accessors are declared, the ev declaration is removed, and so invoke on ev() no longer works. I had posted this question on GOTDOTNET discussion forum too and I think Alan came close to answering why the compiler does not allow invoking ev(). You can read the whole story here : <a href="http://blogs.ittoolbox.com/visualbasic/operating/archives/events-vs-delegates-contd-10596">Events vs Delegates </a> Show quote "Lloyd Dupont" wrote: > give pvt_ev a public accessor. > otherwise it's quite obvious, if you have no access to it, how could you > hope invoke it?!?! > > "pSm" <p**@discussions.microsoft.com> wrote in message > news:8A65460C-9937-41BD-958B-50078772C822@microsoft.com... > > When I override the 'add' and 'remove' methods for an event, why can't I > > invoke it ? The sample code is provided below - I have two events - 'my' > > and > > 'ev' - while invoking, my() works but ev() does not ! The compiler > > complains > > that : The event "'Blog.EventsvsDelegates.Form1.ev' can only appear on the > > left hand side of += or -=" > > > > Why ? > > > > public delegate void MyEvent(); > > public class Form1 : System.Windows.Forms.Form > > { > > private System.Windows.Forms.Button button1; > > private event MyEvent my; > > > > private event MyEvent pvt_ev; > > public event MyEvent ev > > { > > add > > { > > pvt_ev +=value; > > } > > > > remove > > { > > pvt_ev-=value; > > } > > } > > > > public Form1() > > { > > // > > // Required for Windows Form Designer support > > // > > InitializeComponent(); > > > > my+=new MyEvent(this.Test); > > my+=new MyEvent(this.Test1); > > > > ev+=new MyEvent (this.Test ); > > } > > > > protected void Test() > > { > > MessageBox.Show ("Test"); > > } > > > > protected void Test1() > > { > > MessageBox.Show ("Test1"); > > } > > > > /// <summary> > > /// The main entry point for the application. > > /// </summary> > > [STAThread] > > static void Main() > > { > > Application.Run(new Form1()); > > } > > > > private void button1_Click(object sender, System.EventArgs e) > > { > > my(); // WORKS > > //ev (); //DOES NOT WORK > > } > > > > } > > > > > > Why would pvt_ev() need a public accessor when I invoke it from the same Sorry, I misread your sample.> class ? just change: >> > private void button1_Click(object sender, System.EventArgs e) private void button1_Click(object sender, System.EventArgs e)>> > { >> > //ev (); //DOES NOT WORK >> > } to { pvt_ev (); } Show quote > In fact any 'event' that is declared as public, would be converted to > 'private' by the compiler - events can never be invoked from outside it's > class. > > I was checking the MSIL and noticed that when 'add' and 'remove' accessors > are declared, the ev declaration is removed, and so invoke on ev() no > longer > works. > > I had posted this question on GOTDOTNET discussion forum too and I think > Alan came close to answering why the compiler does not allow invoking > ev(). > > You can read the whole story here : <a > href="http://blogs.ittoolbox.com/visualbasic/operating/archives/events-vs-delegates-contd-10596">Events > vs Delegates </a> > > "Lloyd Dupont" wrote: > >> give pvt_ev a public accessor. >> otherwise it's quite obvious, if you have no access to it, how could you >> hope invoke it?!?! >> >> "pSm" <p**@discussions.microsoft.com> wrote in message >> news:8A65460C-9937-41BD-958B-50078772C822@microsoft.com... >> > When I override the 'add' and 'remove' methods for an event, why can't >> > I >> > invoke it ? The sample code is provided below - I have two events - >> > 'my' >> > and >> > 'ev' - while invoking, my() works but ev() does not ! The compiler >> > complains >> > that : The event "'Blog.EventsvsDelegates.Form1.ev' can only appear on >> > the >> > left hand side of += or -=" >> > >> > Why ? >> > >> > public delegate void MyEvent(); >> > public class Form1 : System.Windows.Forms.Form >> > { >> > private System.Windows.Forms.Button button1; >> > private event MyEvent my; >> > >> > private event MyEvent pvt_ev; >> > public event MyEvent ev >> > { >> > add >> > { >> > pvt_ev +=value; >> > } >> > >> > remove >> > { >> > pvt_ev-=value; >> > } >> > } >> > >> > public Form1() >> > { >> > // >> > // Required for Windows Form Designer support >> > // >> > InitializeComponent(); >> > >> > my+=new MyEvent(this.Test); >> > my+=new MyEvent(this.Test1); >> > >> > ev+=new MyEvent (this.Test ); >> > } >> > >> > protected void Test() >> > { >> > MessageBox.Show ("Test"); >> > } >> > >> > protected void Test1() >> > { >> > MessageBox.Show ("Test1"); >> > } >> > >> > /// <summary> >> > /// The main entry point for the application. >> > /// </summary> >> > [STAThread] >> > static void Main() >> > { >> > Application.Run(new Form1()); >> > } >> > >> > private void button1_Click(object sender, System.EventArgs e) >> > { >> > my(); // WORKS >> > //ev (); //DOES NOT WORK >> > } >> > >> > } >> > >> >> >> |
|||||||||||||||||||||||