What is the equivalent of Delphi's "reintroduce"?

C

Chazza

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.
 
J

Joey Callisay

it didn't help because the method Init was not inherited, it should be
declared as protected.
 
C

Chazza

That was a typo in my example. I noticed that but thought that you could
just ignore that.
 
G

Guest

Try,

class A
{
protected virtual void Init()
{
// code
}
}

class B : A
{
protected override void Init()
{
base.Init();
// do something with "text"
}
}

Regards,
Phil.
 
C

Chazza

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
 
J

Jon Skeet [C# MVP]

Chazza said:
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.
 
C

Chazza

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.
 
J

Jon Skeet [C# MVP]

Chazza said:
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.
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!
 
J

Jay B. Harlow [MVP - Outlook]

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
 
J

Jay B. Harlow [MVP - Outlook]

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 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top