Events and Derived Classes

Events defined in the base class cannot be raised by a derived class. Events can only be invoked by the class that declared them. For example, the following gives a compile-time error,

Event1.JPG
To get over this, we can simply define a protected method in the base that will raise the event. This protected method can then be called by the derived class, like this,

Event2.JPG

If the ‘RaiseMyEvent’ method in the base class is declared virtual then the derived class can override it and provide custom processing before or after the base class raises the event,

Event4.JPG

Also, note that the derived class can, of course, hook up to the base’s events. Heres the complete example,

Event3.JPG

Advertisements

About soumya chattopadhyay
I live and work in Seattle, WA. I work with Microsoft technologies, and I'm especially interested in C#.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: