472,807 Members | 3,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,807 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 5270
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
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.