473,397 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

What's the use of making an event virtual?

does it works like ordinary virtual method??
coz I find that child class can't invoke the event of the parent class.

class parent
{
public virtual event SomeDelegate SomeChanged;
}

class child : parent
{
void abc()
{
this.SomeChanged+=SomeMethod; // ok

if (this.SomeChanged != null) // can't
this.SomeChanged(); // can't
}
}


class parent
{
public virtual event SomeDelegate SomeChanged;
}

class child : parent
{
public override event SomeDelegate SomeChanged; // what's the use of
overriding a parent event?
}

void main()
{
parent childInstance = new childInstance;
childInstance.SomeChanged+=SomeMethod; // it's parent's one or child's
one?
}
Nov 16 '05 #1
5 5307
Hi Action,

Don't do that. You'll get nothing but troubles.

class parent
{
public virtual event SomeDelegate SomeChanged;
}

class child : parent
{
void abc()
{
this.SomeChanged+=SomeMethod; // ok

if (this.SomeChanged != null) // can't
this.SomeChanged(); // can't
}
}

Remove the virtual keyword infront of the event declaration and this is the
way to go. The only thing you should do is to declare one protected virtual
OnXXX method that raises the event.

class parent
{
public event SomeDelegate SomeChanged;

protected virtual OnSomeChanged(SomeChangedEventArgs e)
{
if(SomeChanged != null)
SomeChanged(this, e);
}
}
That's it. If you want to raise the event from the child class simply call
OnSomeChanged method

If you want to handle the event in your derived class you have options:
either to hook the event or better to override OnSomeChanged method. If you
choose the latter you can suppress the event by not calling the base
implementation.
Don't ever use the following.
class parent
{
public virtual event SomeDelegate SomeChanged;
}

class child : parent
{
public override event SomeDelegate SomeChanged; // what's the use of
overriding a parent event?
}

void main()
{
parent childInstance = new childInstance;
childInstance.SomeChanged+=SomeMethod; // it's parent's one or child's
one?
}


When you declare an event using the syntax above you declare three things.
One private member of SomeDelegate type (this is what actually keeps the
chain of event handlers and what you test for null). Obviously it cannot be
virtual

And two public accessor methods *add* and *remove* which are actually
declared as virtual.
When you override that event you override the accessors and declare again
the member. So you end up having two different delagate members for the
parent and child classes. Because *add* and *remove* are virtual they always
update the child's memeber so the parent member is always null. If you don's
use virtual method for firing the event base class will never have that
event fired because from its perspective there are never be event handlers.

Actually events has one more accessor which is not supported by C# called
*invoke*. C++ porgrammers use to use it. Thus they use to use virtual
events. However it is always dangerous. So my suggestion is to stick with
OnXXX method and don't use virtual events.

--
HTH
Stoitcho Goutsev (100) [C# MVP]

Nov 16 '05 #2
Action <ac***************@hotmail.com> wrote:
does it works like ordinary virtual method??
coz I find that child class can't invoke the event of the parent class.

class parent
{
public virtual event SomeDelegate SomeChanged;
}

class child : parent
{
void abc()
{
this.SomeChanged+=SomeMethod; // ok

if (this.SomeChanged != null) // can't
this.SomeChanged(); // can't
}
}
Presumably that's because you haven't overridden it.
class parent
{
public virtual event SomeDelegate SomeChanged;
}

class child : parent
{
public override event SomeDelegate SomeChanged; // what's the use of
overriding a parent event?
}
You may want to implement the event in a different way. For instance,
if you have several events, only some of which will be subscribed to,
you could keep them in a hashtable to avoid using memory for each
implicit delegate variable declaration. It's rare to do this, IME, but
there *can* be a point of overriding an event.
void main()
{
parent childInstance = new childInstance;
childInstance.SomeChanged+=SomeMethod; // it's parent's one or child's
one?
}


The child's one, I would hope - just as overriding works elsewhere.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me
Nov 16 '05 #3
I thought there weren't any "don't do that. You'll get nothing but
trouble" things in this managed coding style :)

David Logan

Stoitcho Goutsev (100) [C# MVP] wrote:
Hi Action,

Don't do that. You'll get nothing but troubles.

class parent
{
public virtual event SomeDelegate SomeChanged;
}

class child : parent
{
void abc()
{
this.SomeChanged+=SomeMethod; // ok

if (this.SomeChanged != null) // can't
this.SomeChanged(); // can't
}
}

Remove the virtual keyword infront of the event declaration and this is the
way to go. The only thing you should do is to declare one protected virtual
OnXXX method that raises the event.

class parent
{
public event SomeDelegate SomeChanged;

protected virtual OnSomeChanged(SomeChangedEventArgs e)
{
if(SomeChanged != null)
SomeChanged(this, e);
}
}
That's it. If you want to raise the event from the child class simply call
OnSomeChanged method

If you want to handle the event in your derived class you have options:
either to hook the event or better to override OnSomeChanged method. If you
choose the latter you can suppress the event by not calling the base
implementation.
Don't ever use the following.
class parent
{
public virtual event SomeDelegate SomeChanged;
}

class child : parent
{
public override event SomeDelegate SomeChanged; // what's the use of
overriding a parent event?
}

void main()
{
parent childInstance = new childInstance;
childInstance.SomeChanged+=SomeMethod; // it's parent's one or child's
one?
}

When you declare an event using the syntax above you declare three things.
One private member of SomeDelegate type (this is what actually keeps the
chain of event handlers and what you test for null). Obviously it cannot be
virtual

And two public accessor methods *add* and *remove* which are actually
declared as virtual.
When you override that event you override the accessors and declare again
the member. So you end up having two different delagate members for the
parent and child classes. Because *add* and *remove* are virtual they always
update the child's memeber so the parent member is always null. If you don's
use virtual method for firing the event base class will never have that
event fired because from its perspective there are never be event handlers.

Actually events has one more accessor which is not supported by C# called
*invoke*. C++ porgrammers use to use it. Thus they use to use virtual
events. However it is always dangerous. So my suggestion is to stick with
OnXXX method and don't use virtual events.

Nov 16 '05 #4
Thank you very much!
Currently, I also use OnXXX to raise events
but I just wonder what's the use of marking an event virtual (since it can
be compiled correctly)

thx

"Stoitcho Goutsev (100) [C# MVP]" <10*@100.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
Hi Action,

Don't do that. You'll get nothing but troubles.

class parent
{
public virtual event SomeDelegate SomeChanged;
}

class child : parent
{
void abc()
{
this.SomeChanged+=SomeMethod; // ok

if (this.SomeChanged != null) // can't
this.SomeChanged(); // can't
}
}

Remove the virtual keyword infront of the event declaration and this is

the way to go. The only thing you should do is to declare one protected virtual OnXXX method that raises the event.

class parent
{
public event SomeDelegate SomeChanged;

protected virtual OnSomeChanged(SomeChangedEventArgs e)
{
if(SomeChanged != null)
SomeChanged(this, e);
}
}
That's it. If you want to raise the event from the child class simply call
OnSomeChanged method

If you want to handle the event in your derived class you have options:
either to hook the event or better to override OnSomeChanged method. If you choose the latter you can suppress the event by not calling the base
implementation.
Don't ever use the following.

class parent
{
public virtual event SomeDelegate SomeChanged;
}

class child : parent
{
public override event SomeDelegate SomeChanged; // what's the use of
overriding a parent event?
}

void main()
{
parent childInstance = new childInstance;
childInstance.SomeChanged+=SomeMethod; // it's parent's one or child's
one?
}
When you declare an event using the syntax above you declare three things.
One private member of SomeDelegate type (this is what actually keeps the
chain of event handlers and what you test for null). Obviously it cannot

be virtual

And two public accessor methods *add* and *remove* which are actually
declared as virtual.
When you override that event you override the accessors and declare again
the member. So you end up having two different delagate members for the
parent and child classes. Because *add* and *remove* are virtual they always update the child's memeber so the parent member is always null. If you don's use virtual method for firing the event base class will never have that
event fired because from its perspective there are never be event handlers.
Actually events has one more accessor which is not supported by C# called
*invoke*. C++ porgrammers use to use it. Thus they use to use virtual
events. However it is always dangerous. So my suggestion is to stick with
OnXXX method and don't use virtual events.

--
HTH
Stoitcho Goutsev (100) [C# MVP]

Nov 16 '05 #5
Unfotunately there are. In the managed word gives you is that you
theoretically cannot crash the system. But nothing saves you to make logical
errors, some of them (the case with the virtual events is I believe) are
hard to track down. Spending even half an hour tryng to figure out why
othewise perfectly written code doesn't work I call trouble. Managed
environment doesn't help you there.

--

Stoitcho Goutsev (100) [C# MVP]
"David Logan" <dj******@comcast.net> wrote in message
news:6PlJc.80691$MB3.44398@attbi_s04...
I thought there weren't any "don't do that. You'll get nothing but
trouble" things in this managed coding style :)

David Logan

Stoitcho Goutsev (100) [C# MVP] wrote:
Hi Action,

Don't do that. You'll get nothing but troubles.

class parent
{
public virtual event SomeDelegate SomeChanged;
}

class child : parent
{
void abc()
{
this.SomeChanged+=SomeMethod; // ok

if (this.SomeChanged != null) // can't
this.SomeChanged(); // can't
}
}

Remove the virtual keyword infront of the event declaration and this is the way to go. The only thing you should do is to declare one protected virtual OnXXX method that raises the event.

class parent
{
public event SomeDelegate SomeChanged;

protected virtual OnSomeChanged(SomeChangedEventArgs e)
{
if(SomeChanged != null)
SomeChanged(this, e);
}
}
That's it. If you want to raise the event from the child class simply call OnSomeChanged method

If you want to handle the event in your derived class you have options:
either to hook the event or better to override OnSomeChanged method. If you choose the latter you can suppress the event by not calling the base
implementation.
Don't ever use the following.
class parent
{
public virtual event SomeDelegate SomeChanged;
}

class child : parent
{
public override event SomeDelegate SomeChanged; // what's the use of
overriding a parent event?
}

void main()
{
parent childInstance = new childInstance;
childInstance.SomeChanged+=SomeMethod; // it's parent's one or child's
one?
}

When you declare an event using the syntax above you declare three things. One private member of SomeDelegate type (this is what actually keeps the
chain of event handlers and what you test for null). Obviously it cannot be virtual

And two public accessor methods *add* and *remove* which are actually
declared as virtual.
When you override that event you override the accessors and declare again the member. So you end up having two different delagate members for the
parent and child classes. Because *add* and *remove* are virtual they always update the child's memeber so the parent member is always null. If you don's use virtual method for firing the event base class will never have that
event fired because from its perspective there are never be event handlers.
Actually events has one more accessor which is not supported by C# called *invoke*. C++ porgrammers use to use it. Thus they use to use virtual
events. However it is always dangerous. So my suggestion is to stick with OnXXX method and don't use virtual events.

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jarrod Sharp | last post by:
When a user changes the text in a textbox i wish to update the registry key it represents. If I use event TextChanged event then as you type every character in your update the registry routines are...
4
by: Alex Vinokur | last post by:
http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.6 Faq-20.6 contains function userCode(Shape&) : void userCode(Shape& s) { Shape* s2 = s.clone(); Shape* s3 = s.create();...
4
by: David | last post by:
Hi, Buddy, a newbie's question for you guys, In C++, some functions have a return value type "result", what does this mean, I searched on web, but no hint. thanks a lot David
5
by: jerars | last post by:
Hi! I have recently realized that some computer in my office have a different behavior when it comes to handle the onresize event in Internet explorer. I red a lot on this forum about this but I...
4
by: e-mid | last post by:
i used mouseEvents or clickEvents etc.. but i did not still understand what an event is yet. eg: when we press a button, the eventhandler registered with the the button's click *event* is...
0
by: COHENMARVIN | last post by:
I have a web site where I put my asp.net application in a folder called /asp.net/hotelallocation. I want it to be protected by a password, so I put a web.config file in that folder. But now when...
4
by: moleskyca1 | last post by:
What operators cannot be virtual and why? I looked at FAQ and found nothing. I think there are operators that cannot be virtual, but I don't know why?
2
by: =?Utf-8?B?YW5kcnl1aGE=?= | last post by:
I have a listbox and auto generated lb_SelectedIndexChanged event that handles lb.SelectedIndexChanged. The event obviously fires when I change the selectedIndex visually with a mouse and also...
0
by: shashank narayan | last post by:
what is preinit event in asp.net and why it is used?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.