473,595 Members | 2,442 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Overriding properties and events

Is it possible for a derived class to override a property and/or event of
its base class ?
Jul 21 '05 #1
8 2254
Only if the base class has that item marked as overrideable. If not, you'll
need to use Shadows.
"Edward Diener" <ed******@tropi csoft.com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
Is it possible for a derived class to override a property and/or event of
its base class ?

Jul 21 '05 #2
Scott M. wrote:
Only if the base class has that item marked as overrideable. If not,
you'll need to use Shadows.
I assume when you say Shadows you mean that the derived class creates a
property or event with the same name as the base class and calls down to the
base class as needed.

What in the documentation tells me if the item is marked as overrideable ?
If I look at the MSDN documentation for the .NET framework and I see a
property for a class, I see no mention of overrideable in any language
example for get or set functions. Can you point me to a property in the
documentation which shows this ?

As far as events are concerned, I was thinking of the ability to override
the adding, removing, and/or raising of an event. Yet documentation for an
event tells me nothing about these individual event functions for a given
event but just about the event itself. For my own events I make them virtual
in C++ and, I believe, all functions are virtual in C#. Can I override the
add_XXX, remove_XXX, or raise_XXX for a public event ? What abouyt for a
protected event ?


"Edward Diener" <ed******@tropi csoft.com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
Is it possible for a derived class to override a property and/or
event of its base class ?

Jul 21 '05 #3

"Edward Diener" <ed******@tropi csoft.com> wrote in message
news:%2******** *********@TK2MS FTNGP12.phx.gbl ...
Scott M. wrote:
Only if the base class has that item marked as overrideable. If not,
you'll need to use Shadows.
I assume when you say Shadows you mean that the derived class creates a
property or event with the same name as the base class and calls down to
the
base class as needed.

Yes, Shadows is a VB term. Its called a "new" member in C# and I dunno about
C++, though I'm sure you do. I believe Shadows may actually remove all
overloads with the same name, but I'm not sure.
What in the documentation tells me if the item is marked as overrideable ?
If I look at the MSDN documentation for the .NET framework and I see a
property for a class, I see no mention of overrideable in any language
example for get or set functions. Can you point me to a property in the
documentation which shows this ?
For C# and C++, the declaration should be marked virtual or abstract, while
the VB version will be marked overridable or MustInherit(I think). Again,
overridable and MustIherit are vb specific terms.
As far as events are concerned, I was thinking of the ability to override
the adding, removing, and/or raising of an event. Yet documentation for an
event tells me nothing about these individual event functions for a given
event but just about the event itself. For my own events I make them
virtual
in C++ and, I believe, all functions are virtual in C#. Can I override the
add_XXX, remove_XXX, or raise_XXX for a public event ? What abouyt for a
protected event ?


You should be able to override them for *virtual* events. Well, only add_XXX
and remove_XXX directly in C#, just as you would a property. I believe C#
requires that you override both add and remove if you override either, but
it does not support raise operators per se, instead you will simply see a
raise_XXX method. So the runtime will support it entirely, its all a matter
of what your language allows. You may wanna post to the MC++ group for
specific syntax and restrictions that C++ places on event overriding(I put
example C# code at the bottom, unfortunatly I'm not familiar enough with C++
to give you an example).

In practice, virtual events seem pretty rare, made rarer by the lack of
raise support in C# I think. Its a pity its missing, especially since C++
and apparently VB in the near future will support them.
public class A
{
public virtual event EventHandler Test;
}

public class B : A
{
EventHandler handler;
public override event EventHandler Test
{
add
{
handler = (EventHandler)D elegate.Combine (handler,value) ;
}

remove
{
handler = (EventHandler)D elegate.Remove( handler,value);
}
}

}
Jul 21 '05 #4

"Edward Diener" <ed******@tropi csoft.com> wrote in message
news:%2******** *********@TK2MS FTNGP12.phx.gbl ...
Scott M. wrote:
Only if the base class has that item marked as overrideable. If not,
you'll need to use Shadows.
I assume when you say Shadows you mean that the derived class creates a
property or event with the same name as the base class and calls down to
the
base class as needed.

What in the documentation tells me if the item is marked as overrideable ?
If I look at the MSDN documentation for the .NET framework and I see a
property for a class, I see no mention of overrideable in any language
example for get or set functions. Can you point me to a property in the
documentation which shows this ?

Forgot:
http://msdn.microsoft.com/library/de...stexttopic.asp

Read the declarations, note virtual, __virtual, and overridable
As far as events are concerned, I was thinking of the ability to override
the adding, removing, and/or raising of an event. Yet documentation for an
event tells me nothing about these individual event functions for a given
event but just about the event itself. For my own events I make them
virtual
in C++ and, I believe, all functions are virtual in C#. Can I override the
add_XXX, remove_XXX, or raise_XXX for a public event ? What abouyt for a
protected event ?


"Edward Diener" <ed******@tropi csoft.com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
Is it possible for a derived class to override a property and/or
event of its base class ?


Jul 21 '05 #5
Daniel O'Connell [C# MVP] wrote:
"Edward Diener" <ed******@tropi csoft.com> wrote in message
news:%2******** *********@TK2MS FTNGP12.phx.gbl ...
Scott M. wrote:
Only if the base class has that item marked as overrideable. If
not, you'll need to use Shadows.


I assume when you say Shadows you mean that the derived class
creates a property or event with the same name as the base class and
calls down to the
base class as needed.

What in the documentation tells me if the item is marked as
overrideable ? If I look at the MSDN documentation for the .NET
framework and I see a property for a class, I see no mention of
overrideable in any language example for get or set functions. Can
you point me to a property in the documentation which shows this ?

Forgot:

http://msdn.microsoft.com/library/de...stexttopic.asp

Thanks, I see it now.
Jul 21 '05 #6
Daniel O'Connell [C# MVP] wrote:
"Edward Diener" <ed******@tropi csoft.com> wrote in message
news:%2******** *********@TK2MS FTNGP12.phx.gbl ...
Scott M. wrote:
Only if the base class has that item marked as overrideable. If
not, you'll need to use Shadows.
I assume when you say Shadows you mean that the derived class
creates a property or event with the same name as the base class and
calls down to the
base class as needed.

Yes, Shadows is a VB term. Its called a "new" member in C# and I
dunno about C++, though I'm sure you do. I believe Shadows may
actually remove all overloads with the same name, but I'm not sure.
What in the documentation tells me if the item is marked as
overrideable ? If I look at the MSDN documentation for the .NET
framework and I see a property for a class, I see no mention of
overrideable in any language example for get or set functions. Can
you point me to a property in the documentation which shows this ?

For C# and C++, the declaration should be marked virtual or abstract,
while the VB version will be marked overridable or MustInherit(I
think). Again, overridable and MustIherit are vb specific terms.


I do see this in the documentation, after you have pointed it out to me for
Text.Control. What is amusing is that the VB and C# doc syntax example will
have the appropriate syntax for telling me that the property is
overrideable, while the C++ does not, for many properties. However this just
tells me that the doc is wrong since if a property is overrideable in the
other languages it has to be virtual in C++ also. After all, .NET components
are usable from all .NET languages in the same general way.
As far as events are concerned, I was thinking of the ability to
override the adding, removing, and/or raising of an event. Yet
documentation for an event tells me nothing about these individual
event functions for a given event but just about the event itself.
For my own events I make them virtual
in C++ and, I believe, all functions are virtual in C#. Can I
override the add_XXX, remove_XXX, or raise_XXX for a public event ?
What abouyt for a protected event ?
You should be able to override them for *virtual* events. Well, only
add_XXX and remove_XXX directly in C#, just as you would a property.
I believe C# requires that you override both add and remove if you
override either, but it does not support raise operators per se,
instead you will simply see a raise_XXX method. So the runtime will
support it entirely, its all a matter of what your language allows.
You may wanna post to the MC++ group for specific syntax and
restrictions that C++ places on event overriding(I put example C#
code at the bottom, unfortunatly I'm not familiar enough with C++ to
give you an example).

In practice, virtual events seem pretty rare, made rarer by the lack
of raise support in C# I think. Its a pity its missing, especially
since C++ and apparently VB in the near future will support them.


I agree with you that there should be more virtual events. I would even go
so far as to say that nearly all properties and events should be
overrideable in derived classes. When I develop components, I almost always
make all properties and events overrideable. I do not see the overhead of
this as being that great anymore on modern OSs and frameworks. Of course if
I did not want a derived class to override a property or event, because its
operation is too specific to my class itself, I would not make it
overrideable. But that is a very rare case.

In C++ the raise_XXX method is overrideable if it is declared virtual.
Normally for public events automatically generated, the add_XXX and
remove_XXX are generated as public and the raise_XXX is generated as
protected. If the event is virtual, then all of them are overrideable by a
derived class in C++.

Overriding events, and properties also, should be given more information in
the .NET docs, and every language should have the abilities to override
properties and events just as they would override methods. This gives the
component developer maximum flexibility in creating new classes from the
ones in the .NET framework. The documentation does not really address this
issue of overriding properites and events very well, but it should.


public class A
{
public virtual event EventHandler Test;
}

public class B : A
{
EventHandler handler;
public override event EventHandler Test
{
add
{
handler = (EventHandler)D elegate.Combine (handler,value) ;
}

remove
{
handler = (EventHandler)D elegate.Remove( handler,value);
}
}

}

Jul 21 '05 #7

"Edward Diener" <ed******@tropi csoft.com> wrote in message
news:uw******** ******@TK2MSFTN GP14.phx.gbl...
Daniel O'Connell [C# MVP] wrote:
"Edward Diener" <ed******@tropi csoft.com> wrote in message
news:%2******** *********@TK2MS FTNGP12.phx.gbl ...
Scott M. wrote:
Only if the base class has that item marked as overrideable. If
not, you'll need to use Shadows.

I assume when you say Shadows you mean that the derived class
creates a property or event with the same name as the base class and
calls down to the
base class as needed.

Yes, Shadows is a VB term. Its called a "new" member in C# and I
dunno about C++, though I'm sure you do. I believe Shadows may
actually remove all overloads with the same name, but I'm not sure.
What in the documentation tells me if the item is marked as
overrideable ? If I look at the MSDN documentation for the .NET
framework and I see a property for a class, I see no mention of
overrideable in any language example for get or set functions. Can
you point me to a property in the documentation which shows this ?

For C# and C++, the declaration should be marked virtual or abstract,
while the VB version will be marked overridable or MustInherit(I
think). Again, overridable and MustIherit are vb specific terms.


I do see this in the documentation, after you have pointed it out to me
for
Text.Control. What is amusing is that the VB and C# doc syntax example
will
have the appropriate syntax for telling me that the property is
overrideable, while the C++ does not, for many properties. However this
just
tells me that the doc is wrong since if a property is overrideable in the
other languages it has to be virtual in C++ also. After all, .NET
components
are usable from all .NET languages in the same general way.
As far as events are concerned, I was thinking of the ability to
override the adding, removing, and/or raising of an event. Yet
documentation for an event tells me nothing about these individual
event functions for a given event but just about the event itself.
For my own events I make them virtual
in C++ and, I believe, all functions are virtual in C#. Can I
override the add_XXX, remove_XXX, or raise_XXX for a public event ?
What abouyt for a protected event ?


You should be able to override them for *virtual* events. Well, only
add_XXX and remove_XXX directly in C#, just as you would a property.
I believe C# requires that you override both add and remove if you
override either, but it does not support raise operators per se,
instead you will simply see a raise_XXX method. So the runtime will
support it entirely, its all a matter of what your language allows.
You may wanna post to the MC++ group for specific syntax and
restrictions that C++ places on event overriding(I put example C#
code at the bottom, unfortunatly I'm not familiar enough with C++ to
give you an example).

In practice, virtual events seem pretty rare, made rarer by the lack
of raise support in C# I think. Its a pity its missing, especially
since C++ and apparently VB in the near future will support them.


I agree with you that there should be more virtual events. I would even go
so far as to say that nearly all properties and events should be
overrideable in derived classes. When I develop components, I almost
always
make all properties and events overrideable. I do not see the overhead of
this as being that great anymore on modern OSs and frameworks. Of course
if
I did not want a derived class to override a property or event, because
its
operation is too specific to my class itself, I would not make it
overrideable. But that is a very rare case.


The biggest issue is that, since C# does not support raise_, there is no
enforcement and providing virtual events to a C# user who derives from them
is basically begging for the user to break your component. Since the
framework shy's away from raise accessors, most C# developers aren't even
aware they can exist and very well may not have the sense to override
raise_MyEvent to have it raise the event they just changed. I think its a
bit too risky to do in any component that will be released into the wild,
atleast without *very* strong documentation and following the OnXxx pattern
exposed in windows forms.

Also, when you override a property or event, I believe the compiler emits
new metadata for that property as well as overriding the methods. By doing
this, a language that doesn't recognize raise may not include the raise
method(or others) in its redefinition and result in some nasty situations
where a class written in C++ is inherited in C#, and then that class is
inherited by another written in C++, and the event no longer has a raise
method associated.(Not e that, since I havn't tested, C# and C++ should not
be taken as absolutes. Unless there is a spec section dealing with this,
this could be any pair of languages)
Unfortunatly, I can't test that right now since I do not have MC++
installed, I would be curious to see if there are any issues with this. I do
not recall a section in the spec that defines this behaviour....I' ll look
into it. Hopefully there is definition, but it seems like something obscure
enough that you might want to be atleast aware of the possiblity.
Jul 21 '05 #8
No, Shadowed members replace the implementation of the base class member.
The difference between Shadows and Overrides is that to Override, you need
permission from the base class (the base class member must be declared as
Overrideable). With Shadows, you do not need "permission " from the base
class.
"Edward Diener" <ed******@tropi csoft.com> wrote in message
news:%2******** *********@TK2MS FTNGP12.phx.gbl ...
Scott M. wrote:
Only if the base class has that item marked as overrideable. If not,
you'll need to use Shadows.


I assume when you say Shadows you mean that the derived class creates a
property or event with the same name as the base class and calls down to
the
base class as needed.

What in the documentation tells me if the item is marked as overrideable ?
If I look at the MSDN documentation for the .NET framework and I see a
property for a class, I see no mention of overrideable in any language
example for get or set functions. Can you point me to a property in the
documentation which shows this ?

As far as events are concerned, I was thinking of the ability to override
the adding, removing, and/or raising of an event. Yet documentation for an
event tells me nothing about these individual event functions for a given
event but just about the event itself. For my own events I make them
virtual
in C++ and, I believe, all functions are virtual in C#. Can I override the
add_XXX, remove_XXX, or raise_XXX for a public event ? What abouyt for a
protected event ?


"Edward Diener" <ed******@tropi csoft.com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
Is it possible for a derived class to override a property and/or
event of its base class ?


Jul 21 '05 #9

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

Similar topics

3
4177
by: Ali Eghtebas | last post by:
Hi, I have 3 questions regarding the code below: 1) Why can't I trap the KEYDOWN while I can trap KEYUP? 2) Is it correct that I use Return True within the IF-Statement? (I've already read the documentation but it is rather hard to understand so please don't refer to it :) 3) Many examples in the newsgroups use Return MyBase.ProcessKeyPreview(m) as the last code line while I have used Return MyBase.ProcessKeyEventArgs(m)
10
5199
by: muscha | last post by:
I don't get it. What's the main differences between overriding a base class's methods vs. hiding them? Thanks, /m
1
1635
by: Robert | last post by:
Hi, I've inherited the XmlDocument class to include some custom methods that I want to use on a particular XML file. I need to know whether the document has changed since being loaded, and I wanted to be clever and hook up to the events that are already defined in the XmlDocument class (i.e. NodeChanged, NodeInserted). I had presumed I would simply be able to override them, however I cannot
17
2905
by: Bob Weiner | last post by:
What is the purpose of hiding intead of overriding a method? I have googled the question but haven't found anything that makes any sense of it. In the code below, the only difference is that when the Poodle is upcast to the Dog (in its wildest dreams) it then says "bow wow" where the bernard always says "woof" (see code). Basically, it appears that I'm hiding the poodle's speak method from everything except the poodle. Why would I...
6
2667
by: Mike | last post by:
Hi, I have such problem: On my form I have TabControl. I want to move from one tab to another using "Next" and "Prev" button. This part works fine. But control also supports switching between TabPagess using combination of buttons Ctrl+Tab (forward) and Ctrl+Shift+Tab (back). I would like to suppress such possibility.
4
5948
by: Phill. W | last post by:
Here's a knotty little problem. I have some nasty little controls that needs to behave in a non- windows-Standard way - don't ask why; it's a large application being converted from some older code and my users are adamant that this behaviour mustn't change. Specifically, I want to be able to set myControl.Enabled = False
2
1649
by: project | last post by:
Hi every body, Any body can help me the following doubts? 1. what is constructor? 2. what is destructor? 3. what is overriding function. 4. different between structure and array 5. what is objected oriented
8
302
by: Edward Diener | last post by:
Is it possible for a derived class to override a property and/or event of its base class ?
4
9841
by: FullBandwidth | last post by:
I have been perusing various blogs and MSDN pages discussing the use of event properties and the EventHandlerList class. I don't believe there's anything special about the EventHandlerList class in this context, in fact some articles from pre-2.0 suggest using any collection class of your choice. So my questions focus more on the syntax of event properties provided by the "event" keyword in C#. (Disclaimer - I am a C++ programmer working...
0
7957
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
7883
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
8262
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...
0
8379
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6675
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5839
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
3915
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1491
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1226
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.