Sign in to follow this  

C# event question

This topic is 2789 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can an event only be "fired" from the class it belongs to? I have the following main() which exists in a separate class from "myClass": myClass classInstance = new myClass(); classInstance.testEvent += new testDelegate(classInstance.sayHi); classInstance.testEvent(); and I end up withe the error: Error 1 The event 'ConsoleApplication2.myClass.testEvent' can only appear on the left hand side of += or -= (except when used from within the type 'ConsoleApplication2.myClass') So it doesn't like the third line, but if I make a function within myClass that just calls testEvent() and use that, then everything is fine...

Share this post


Link to post
Share on other sites
Because the 'event' keyword is specifically designed to prevent that behavior (among other things).

If you want to raise the delegate from outside the class itself, remove the event keyword and make it a regular multicast delegate, or create a public method to fire it.

Share this post


Link to post
Share on other sites

This topic is 2789 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this