473,854 Members | 1,837 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Virtual Override Events Inheritance

Hi
I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using events since I never saw it used anywhere in MSDN documentation/samples?! Or it will just break when I upgrade to .NET Framework 2.x in the coming years

namespace MyNamespac

public delegate void MyDel()

public class MyBase

public virtual event MyDel MyEvent
public void MyMethod2(

MyEvent()

public class MyDerived : MyBas

// Override base class event so that it can
// be raise from child (here!
public override event MyDel MyEvent

public void MyMethod(
{ // Raise event. Without the virtual/override keywords,
// child classes are not allowed to raise events declared
// in their base clas
MyEvent()


Thank
JPRoo

Nov 15 '05 #1
14 12168
I use the following syntax to have events inherited from base to child
classes which works nicely (virtual and override keyword on events).
But I am wondering if it is a "supported" way of using events since I
never saw it used anywhere in MSDN documentation/samples?! Or it will
just break when I upgrade to .NET Framework 2.x in the coming years?


It's supported and documented alright. See

http://msdn.microsoft.com/library/en...pspec_10_7.asp
http://msdn.microsoft.com/library/en...pec_10_7_4.asp

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #2
I've never had the occasion to use that syntax, but the
ECMA C# spec says that virtual events should be supported
in the same way that virtual methods are, so I think
you're ok on that front. As to future support, does
anyone know where a 2.x spec might be that could change
that?

JER
-----Original Message-----
Hi,
I use the following syntax to have events inherited from base to child classes which works nicely (virtual and
override keyword on events). But I am wondering if it is
a "supported" way of using events since I never saw it
used anywhere in MSDN documentation/samples?! Or it will
just break when I upgrade to .NET Framework 2.x in the
coming years?
namespace MyNamespace
{
public delegate void MyDel();

public class MyBase
{
public virtual event MyDel MyEvent;
public void MyMethod2()
{
MyEvent();
}
}

public class MyDerived : MyBase
{
// Override base class event so that it can // be raise from child (here!)
public override event MyDel MyEvent;

public void MyMethod()
{ // Raise event. Without the virtual/override keywords, // child classes are not allowed to raise events declared // in their base class
MyEvent();
}
}
}

Thanks
JPRoot

.

Nov 15 '05 #3
Do you mean the OnEventBlah method call which is defined as

protected virtual OnEvenetMethodH ere(blah)
{
}

?
"Jerry Negrelli" <je************ @nospamdatascie ntific.com> wrote in message
news:03******** *************** *****@phx.gbl.. .
I've never had the occasion to use that syntax, but the
ECMA C# spec says that virtual events should be supported
in the same way that virtual methods are, so I think
you're ok on that front. As to future support, does
anyone know where a 2.x spec might be that could change
that?

JER
-----Original Message-----
Hi,
I use the following syntax to have events inherited from

base to child classes which works nicely (virtual and
override keyword on events). But I am wondering if it is
a "supported" way of using events since I never saw it
used anywhere in MSDN documentation/samples?! Or it will
just break when I upgrade to .NET Framework 2.x in the
coming years?

namespace MyNamespace
{
public delegate void MyDel();

public class MyBase
{
public virtual event MyDel MyEvent;
public void MyMethod2()
{
MyEvent();
}
}

public class MyDerived : MyBase
{
// Override base class event so that it

can
// be raise from child (here!)
public override event MyDel MyEvent;

public void MyMethod()
{ // Raise event. Without the

virtual/override keywords,
// child classes are not allowed

to raise events declared
// in their base class
MyEvent();
}
}
}

Thanks
JPRoot

.

Nov 15 '05 #4
Oh wait, the actual event, why would you need to do this?
"Alvin Pruney" <.> wrote in message
news:ei******** *****@tk2msftng p13.phx.gbl...
Do you mean the OnEventBlah method call which is defined as

protected virtual OnEvenetMethodH ere(blah)
{
}

?
"Jerry Negrelli" <je************ @nospamdatascie ntific.com> wrote in message news:03******** *************** *****@phx.gbl.. .
I've never had the occasion to use that syntax, but the
ECMA C# spec says that virtual events should be supported
in the same way that virtual methods are, so I think
you're ok on that front. As to future support, does
anyone know where a 2.x spec might be that could change
that?

JER
-----Original Message-----
Hi,
I use the following syntax to have events inherited from

base to child classes which works nicely (virtual and
override keyword on events). But I am wondering if it is
a "supported" way of using events since I never saw it
used anywhere in MSDN documentation/samples?! Or it will
just break when I upgrade to .NET Framework 2.x in the
coming years?

namespace MyNamespace
{
public delegate void MyDel();

public class MyBase
{
public virtual event MyDel MyEvent;
public void MyMethod2()
{
MyEvent();
}
}

public class MyDerived : MyBase
{
// Override base class event so that it

can
// be raise from child (here!)
public override event MyDel MyEvent;

public void MyMethod()
{ // Raise event. Without the

virtual/override keywords,
// child classes are not allowed

to raise events declared
// in their base class
MyEvent();
}
}
}

Thanks
JPRoot

.


Nov 15 '05 #5
Is the ECMA C# specs actually final or draft?

Has any other part submitted draft ammendments , after all surely anybody
could, right?

"Alvin Pruney" <.> wrote in message
news:ej******** ******@TK2MSFTN GP10.phx.gbl...
Oh wait, the actual event, why would you need to do this?
"Alvin Pruney" <.> wrote in message
news:ei******** *****@tk2msftng p13.phx.gbl...
Do you mean the OnEventBlah method call which is defined as

protected virtual OnEvenetMethodH ere(blah)
{
}

?
"Jerry Negrelli" <je************ @nospamdatascie ntific.com> wrote in

message
news:03******** *************** *****@phx.gbl.. .
I've never had the occasion to use that syntax, but the
ECMA C# spec says that virtual events should be supported
in the same way that virtual methods are, so I think
you're ok on that front. As to future support, does
anyone know where a 2.x spec might be that could change
that?

JER

>-----Original Message-----
>Hi,
>I use the following syntax to have events inherited from
base to child classes which works nicely (virtual and
override keyword on events). But I am wondering if it is
a "supported" way of using events since I never saw it
used anywhere in MSDN documentation/samples?! Or it will
just break when I upgrade to .NET Framework 2.x in the
coming years?
>
>namespace MyNamespace
>{
> public delegate void MyDel();
>
> public class MyBase
> {
> public virtual event MyDel MyEvent;
> public void MyMethod2()
> {
> MyEvent();
> }
> }
>
> public class MyDerived : MyBase
> {
> // Override base class event so that it
can
> // be raise from child (here!)
> public override event MyDel MyEvent;
>
> public void MyMethod()
> { // Raise event. Without the
virtual/override keywords,
> // child classes are not allowed
to raise events declared
> // in their base class
> MyEvent();
> }
> }
>}
>
>Thanks
>JPRoot
>
>.
>



Nov 15 '05 #6
Here's a link to the most recent version of the spec:

http://www.ecma-
international.o rg/publications/standards/Ecma-334.htm

and everything's a work in progress, right? ;)

JER
-----Original Message-----
Is the ECMA C# specs actually final or draft?

Has any other part submitted draft ammendments , after all surely anybodycould, right?

"Alvin Pruney" <.> wrote in message
news:ej******* *******@TK2MSFT NGP10.phx.gbl.. .
Oh wait, the actual event, why would you need to do this?

"Alvin Pruney" <.> wrote in message
news:ei******** *****@tk2msftng p13.phx.gbl...
> Do you mean the OnEventBlah method call which is defined as >
> protected virtual OnEvenetMethodH ere(blah)
> {
> }
>
> ?
>
>
> "Jerry Negrelli" <je************ @nospamdatascie ntific.com> wrote in
message
> news:03******** *************** *****@phx.gbl.. .
> > I've never had the occasion to use that syntax,

but the > > ECMA C# spec says that virtual events should be supported > > in the same way that virtual methods are, so I think > > you're ok on that front. As to future support, does > > anyone know where a 2.x spec might be that could change > > that?
> >
> > JER
> >
> > >-----Original Message-----
> > >Hi,
> > >I use the following syntax to have events inherited from > > base to child classes which works nicely (virtual and > > override keyword on events). But I am wondering if it is > > a "supported" way of using events since I never saw it > > used anywhere in MSDN documentation/samples?! Or it will > > just break when I upgrade to .NET Framework 2.x in the > > coming years?
> > >
> > >namespace MyNamespace
> > >{
> > > public delegate void MyDel();
> > >
> > > public class MyBase
> > > {
> > > public virtual event MyDel MyEvent;
> > > public void MyMethod2()
> > > {
> > > MyEvent();
> > > }
> > > }
> > >
> > > public class MyDerived : MyBase
> > > {
> > > // Override base class event so that it
> > can
> > > // be raise from child (here!)
> > > public override event MyDel MyEvent;
> > >
> > > public void MyMethod()
> > > { // Raise event. Without the
> > virtual/override keywords,
> > > // child classes are not allowed
> > to raise events declared
> > > // in their base class
> > > MyEvent();
> > > }
> > > }
> > >}
> > >
> > >Thanks
> > >JPRoot
> > >
> > >.
> > >
>
>


.

Nov 15 '05 #7
100
Hi JPRoot,

IMHO this is indeed very strange way to use base class's events. And beside
I find it strange it is wrong or at least error prone.

You have to check the event for null before fire it because you can't know
if you have handlers registered for that event:
if(MyEvent != null) MyEvent();

But this is not the bigest flaw.

When you define an event as virtual actually you define its accessors (add
and remove) as virtual, but each class in the hierarchy will have its own
field of type *delegate void MyDel()* to hold the handlers

What does it means? Let me provide a new example.

public delegate void MyDel();

class Foo
{
public virtual MyDel MyEvent;
public FooRaiseMyEvent ()
{
if(MyEvent != null) MyEvent();
}
}

class Bar: Foo
{
public override MyDel MyEvent;
public BarRaiseMyEvent ()
{
if(MyEvent != null) MyEvent();
}
}

Now let we have the following code snippet

Foo foo = new Bar();
foo.MyEvent += new MyDel(foo_MyEve nt);
Bar bar = (Bar) foo; //we can do that because the object is of type Bar.

bar.BarRaiseMyE vent(); //Everything is just perfect the event fires
bar.FooRaiseMyE vent(); // Bar inherits this method from Foo, but no event
fires.

Which means that no methods form the base class can fire the event. Imagine
if you have more classes in the hierarchy....

Why is this happen?
Because doing
foo.MyEvent += new MyDel(foo_MyEve nt);
we call MeEvent's *add* accessor polymorphically and the one decalred in Bar
will be called. It will add the handler to the delegate chain in Bar's field
of type MyDel.
Now when BarRaiseMyEvent calles MyEvent() it uses Bar's delegate field,
which has something inside.
FooRaiseMyEvent ., though, uses Foo's delegate field which has nothing inside
and no event is fired.
Access to Fields cannot be polymorphical.

To use events declared in the base class use the design pattern used
throughout the framework (OnXXX methods).
I believe it is described somewhere in MSDN but I don't remember where.
Try to look at
ms-help://MS.MSDNQTR.2003 FEB.1033/cpguide/html/cpconprovidinge ventfunctional
ity.htm
HTH
B\rgds
100

"JPRoot" <jp****@hotmail .com> wrote in message
news:0A******** *************** ***********@mic rosoft.com...
Hi,
I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I
am wondering if it is a "supported" way of using events since I never saw it
used anywhere in MSDN documentation/samples?! Or it will just break when I
upgrade to .NET Framework 2.x in the coming years?
namespace MyNamespace
{
public delegate void MyDel();

public class MyBase
{
public virtual event MyDel MyEvent;
public void MyMethod2()
{
MyEvent();
}
}

public class MyDerived : MyBase
{
// Override base class event so that it can
// be raise from child (here!)
public override event MyDel MyEvent;

public void MyMethod()
{ // Raise event. Without the virtual/override keywords,
// child classes are not allowed to raise events declared
// in their base class
MyEvent();
}
}
}

Thanks
JPRoot

Nov 15 '05 #8
That's a good new since having it documented makes it a little more difficult for MS to stop supporting it..

Second question then..
Seems by trial and error that the virtual keyword on the event "virtualize " only the Add and Remove accessor and not the Raise

Taking for exemple the code that I have posted previously, it appears that when raising the event in MyBase::MyMetho d2() uses the invocation list of the base class instance MyBase::MyEvent and when raising the event MyDerived::MyMe thod() it uses the invocation list of the derived class instance. That defeats the purpose of "virtual" where the base class doesn't use the specialized implementation? !

So if I register to the event, using a pointer to the base class or the child, it always get registered in the derived class invocation list (so Add/Remove are really virtual). If I call MyMethod I receive the event, if I call MyMethod2, I receive an exception stating that the event invocation list is empty !!

Any thoughts

Than
JPRoo
----- Mattias Sjögren wrote: ----

I use the following syntax to have events inherited from base to chil
classes which works nicely (virtual and override keyword on events)
But I am wondering if it is a "supported" way of using events since
never saw it used anywhere in MSDN documentation/samples?! Or it wil
just break when I upgrade to .NET Framework 2.x in the coming years


It's supported and documented alright. Se

http://msdn.microsoft.com/library/en...rpspec_10_7.as
http://msdn.microsoft.com/library/en...spec_10_7_4.as

Mattia

--
Mattias Sjögren [MVP] mattias @ mvps.or
http://www.msjogren.net/dotnet
Please reply only to the newsgroup

Nov 15 '05 #9

Hi,

I have reviewed your post. I will do some research for you.
I will reply to you ASAP.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| Thread-Topic: Virtual Override Events Inheritance
| thread-index: AcOqNrWCuoOYF9S rS3WjsXs6rYhgEQ ==
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
| From: =?Utf-8?B?SlBSb290?= <JP****@hotmail .com>
| References: <0A************ *************** *******@microso ft.com>
<##************ **@TK2MSFTNGP12 .phx.gbl>
| Subject: Re: Virtual Override Events Inheritance
| Date: Thu, 13 Nov 2003 14:37:24 -0800
| Lines: 39
| Message-ID: <39************ *************** *******@microso ft.com>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 8bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| NNTP-Posting-Host: TK2MSFTCMTY1 10.40.1.180
| Path: cpmsftngxa06.ph x.gbl!cpmsftngx a10.phx.gbl
| Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1991 96
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
|
| That's a good new since having it documented makes it a little more
difficult for MS to stop supporting it...

Second question then...
Seems by trial and error that the virtual keyword on the event "virtualize "
only the Add and Remove accessor and not the Raise.

Taking for exemple the code that I have posted previously, it appears that
when raising the event in MyBase::MyMetho d2() uses the invocation list of
the base class instance MyBase::MyEvent and when raising the event
MyDerived::MyMe thod() it uses the invocation list of the derived class
instance. That defeats the purpose of "virtual" where the base class
doesn't use the specialized implementation? !

So if I register to the event, using a pointer to the base class or the
child, it always get registered in the derived class invocation list (so
Add/Remove are really virtual). If I call MyMethod I receive the event, if
I call MyMethod2, I receive an exception stating that the event invocation
list is empty !!!

Any thoughts?

Thank
JPRoot

----- Mattias Sjögren wrote: -----

I use the following syntax to have events inherited from base to child
classes which works nicely (virtual and override keyword on events).
But I am wondering if it is a "supported" way of using events since I
never saw it used anywhere in MSDN documentation/samples?! Or it will
just break when I upgrade to .NET Framework 2.x in the coming years?


It's supported and documented alright. See
http://msdn.microsoft.com/library/en...pspec_10_7.asp

http://msdn.microsoft.com/library/en...pspec_10_7_4.a
sp

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

|

Nov 15 '05 #10

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

Similar topics

3
4807
by: news.microsoft.com | last post by:
Hi, It is possible to override a non virtual method with the "new" keyword So how is this different from specifying a method as virtual then providing the override keyword? Is there any differences between these two methods of overriding? Thanks.
4
2231
by: Rafael Veronezi | last post by:
I have some questions about override in inheritance, and virtual members. I know that you can you override a method by two ways in C#, one, is overriding with the new keyword, like: public new bool Equals(object obj) {} Another is using the override keyword, like: public override bool Equals(object obj) {}
8
2900
by: JPRoot | last post by:
Hi M. Jeffrey Tan, Just hopping you didn't forget me? :) Thanks JPRoot ----- \"Jeffrey Tan\" wrote: -----
5
2621
by: Mark Broadbent | last post by:
Oh yes its that chestnut again! Ive gone over the following (http://www.yoda.arachsys.com/csharp/faq/ -thanks Jon!) again regarding this subject and performed a few of my own tests. I have two classes yClass which inherits xClass. xClass has a virtual method which simply writes a line of text stating its origin, yClass implements the same method which writes a line of text stating its origin also (i.e. "From yClass"). I ran the...
5
5344
by: Action | last post by:
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 {
14
1936
by: Bruno van Dooren | last post by:
Hi all, i am having a problems with inheritance. consider the following: class A { public: A(int i){;} };
5
4539
by: Marcel Hug | last post by:
Hi NG ! I'm new in C# and I'm reading a book about the fundamentals and concepts. In the chapter Methods it's written to use virtual, if i would like to override the method in a subclass. This I've to do by using override. It's also written, that's possible to "hide" the base class method by using the new key word. Because I've already written some C# code and I didn't know anything
2
3048
by: junw2000 | last post by:
In the following code: #include <iostream> using namespace std; class V { public: int i; virtual void f() { cout << "V::f()" << endl;}
8
2045
by: puzzlecracker | last post by:
The statement is taken from FAQ . What about non-virtual functions? Can they be overriden? I still don't see a good justification to prefer private inheritance over composition. In fact, I have never seen it in a commercial code. If someone did, please share the use-case and decisions behind it. Thanks
0
9899
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9750
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11024
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10740
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7909
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5738
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5937
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4149
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3182
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.