I would like to override a method from an inherited class, but the new
method has a different signature to the inherited class. Example:
class A {
private void Init() {
// code
}
}
class B : A {
private void Init(string text) {
base.Init();
// do something with "text"
}
}
I don't want to see class A's version of Init(). Any instance of class B
should only see Init(text). I know this is possible in Delphi using the
reintroduce modifier, but I couldn't find a way in C#. The "override" and
"new" modifiers didn't help either.
--
Regards,
Charcoal 9 6076
it didn't help because the method Init was not inherited, it should be
declared as protected.
"Chazza" <em***@address.com> wrote in message
news:eP**************@TK2MSFTNGP09.phx.gbl... I would like to override a method from an inherited class, but the new method has a different signature to the inherited class. Example:
class A { private void Init() { // code } }
class B : A { private void Init(string text) { base.Init(); // do something with "text" } }
I don't want to see class A's version of Init(). Any instance of class B should only see Init(text). I know this is possible in Delphi using the reintroduce modifier, but I couldn't find a way in C#. The "override" and "new" modifiers didn't help either.
-- Regards, Charcoal
That was a typo in my example. I noticed that but thought that you could
just ignore that.
Try,
class A
{
protected virtual void Init()
{
// code
}
}
class B : A
{
protected override void Init()
{
base.Init();
// do something with "text"
}
}
Regards,
Phil.
"Chazza" wrote: That was a typo in my example. I noticed that but thought that you could just ignore that.
Phil,
You missed the point of my post. I already know how to use override the way
it is meant to use. What I'm looking for is a way to override an inherited
method with a new method which has a different signature. Look at my example
of class B again, and you will notice the Init method has one parameter
which is a string. Now look at your example, where has this parameter gone?
I don't know if this is even possible, you might tell me that C# cannot do
this. However, I know in Delphi it was easy to do using the "reintroduce"
modifier and it was very useful in some places...
Regards,
chazza
Chazza <em***@address.com> wrote: You missed the point of my post. I already know how to use override the way it is meant to use. What I'm looking for is a way to override an inherited method with a new method which has a different signature. Look at my example of class B again, and you will notice the Init method has one parameter which is a string. Now look at your example, where has this parameter gone?
I don't know if this is even possible, you might tell me that C# cannot do this. However, I know in Delphi it was easy to do using the "reintroduce" modifier and it was very useful in some places...
Well, what value would you expect your parameter to have?
You basically can't do this in C#, but I suspect you can fake the same
behaviour using two methods - one which does the overriding, and then
calls the other.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jon,
I know about the two methods trick, but I wanted to hide the old inherited
method. More specifically I wanted to generate compiler errors where it was
used so I could fix up the code to use the new method, and to prevent people
in future from using the old method directly. Well, what value would you expect your parameter to have?
Does it matter? It's simply a different signature which I am asking about.
Any Delphi programmer should understand in a heart beat what I am asking
for...but you C# guys, I don't think you understand the concept I'm trying
to explain.
--
Regards,
Charcoal
Chazza <em***@address.com> wrote: I know about the two methods trick, but I wanted to hide the old inherited method. More specifically I wanted to generate compiler errors where it was used so I could fix up the code to use the new method, and to prevent people in future from using the old method directly.
You definitely can't do that, thank goodness - it would violate
Liskov's Substitutability Principle, for one thing. Well, what value would you expect your parameter to have?
Does it matter?
Absolutely! If you don't mind it being null or some fixed value, that's
one thing. If Delphi somehow gets a value in some "special" way, that
may be a lot harder to "fake".
It's simply a different signature which I am asking about. Any Delphi programmer should understand in a heart beat what I am asking for...but you C# guys, I don't think you understand the concept I'm trying to explain.
That shouldn't really be a surprise - asking about a Delphi concept in
a C# group is bound to involve a bit more communication than asking
about a Delphi concept in a Delphi group!
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Chazza,
I believe you want the VB.NET Shadows keyword, which is very similar to the
C# new keyword, but not different then the C# new keyword.
Shadows hides all the base methods and only shows the derived method that
have the same name.
However as Jon states, Shadows also violate the Liskov's Substitutability
Principle.
Hope this helps
Jay
"Chazza" <em***@address.com> wrote in message
news:OR**************@TK2MSFTNGP12.phx.gbl... Jon,
I know about the two methods trick, but I wanted to hide the old inherited method. More specifically I wanted to generate compiler errors where it was used so I could fix up the code to use the new method, and to prevent people in future from using the old method directly.
Well, what value would you expect your parameter to have?
Does it matter? It's simply a different signature which I am asking about. Any Delphi programmer should understand in a heart beat what I am asking for...but you C# guys, I don't think you understand the concept I'm trying to explain.
-- Regards, Charcoal
Doh! C# new keyword, but not different then the C# new keyword.
Didn't mean the negative.
It should be "but different then the C# new keyword."
Jay
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:el**************@TK2MSFTNGP15.phx.gbl... Chazza, I believe you want the VB.NET Shadows keyword, which is very similar to the C# new keyword, but not different then the C# new keyword.
Shadows hides all the base methods and only shows the derived method that have the same name.
However as Jon states, Shadows also violate the Liskov's Substitutability Principle.
Hope this helps Jay
"Chazza" <em***@address.com> wrote in message news:OR**************@TK2MSFTNGP12.phx.gbl... Jon,
I know about the two methods trick, but I wanted to hide the old inherited method. More specifically I wanted to generate compiler errors where it was used so I could fix up the code to use the new method, and to prevent people in future from using the old method directly.
Well, what value would you expect your parameter to have?
Does it matter? It's simply a different signature which I am asking about. Any Delphi programmer should understand in a heart beat what I am asking for...but you C# guys, I don't think you understand the concept I'm trying to explain.
-- Regards, Charcoal
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Dieter Salath? |
last post by:
Hi,
in our webpage, a user could open a windows explorer to his temp
directory with a simple link and usage of the file protocol:
<a href="file://C:\temp" target="_blank">C:\temp</a>
This...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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,...
|
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...
|
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,...
| |