How to invoke base class methods instead overridens [modified]
-
Hi all. I have OOP Question. Can i invoke base class methods instead overridens; in C# Examp:
class BaseClass { public void Fun1() { this.Fun2();//I want write here - base class method //What i should write here for invoke Fun2() of this class (exactly this class, // not derived), i suppose what i shoul write as in C++ ((BaseClass)this).Fun2(); //But this not work, in this case still invoke method of derived class. } protected virtual void Fun2() { WriteLine("base class method"); } } class DervClass : BaseClass { protected overriden void Fun2() { WriteLine("overriden class method"); } } .... DervClass derv = new DervClass(); derv.Fun1();
modified on Wednesday, December 17, 2008 6:37 AM
you can use
base.FunctionName()
. But the code provided by you is not showing the inheritance.Navaneeth How to use google | Ask smart questions
-
you can use
base.FunctionName()
. But the code provided by you is not showing the inheritance.Navaneeth How to use google | Ask smart questions
N a v a n e e t h wrote:
you can use base.FunctionName(). But the code provided by you is not showing the inheritance.
Thats the point. I want to invoke base method from the base class!!!
-
Hi all. I have OOP Question. Can i invoke base class methods instead overridens; in C# Examp:
class BaseClass { public void Fun1() { this.Fun2();//I want write here - base class method //What i should write here for invoke Fun2() of this class (exactly this class, // not derived), i suppose what i shoul write as in C++ ((BaseClass)this).Fun2(); //But this not work, in this case still invoke method of derived class. } protected virtual void Fun2() { WriteLine("base class method"); } } class DervClass : BaseClass { protected overriden void Fun2() { WriteLine("overriden class method"); } } .... DervClass derv = new DervClass(); derv.Fun1();
modified on Wednesday, December 17, 2008 6:37 AM
First, you have to do this:
class DervClass : BaseClass
Second, in your derived class, you have to do this:
protected override void Fun2()
{
base.Fun2();
}"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
First, you have to do this:
class DervClass : BaseClass
Second, in your derived class, you have to do this:
protected override void Fun2()
{
base.Fun2();
}"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001John Simmons / outlaw programmer wrote:
First, you have to do this: class DervClass : BaseClass
Yes, it's just misstyping.
John Simmons / outlaw programmer wrote:
econd, in your derived class, you have to do this: protected override void Fun2() { base.Fun2(); }
I can't do this in derived class snice implemented logic.
-
N a v a n e e t h wrote:
you can use base.FunctionName(). But the code provided by you is not showing the inheritance.
Thats the point. I want to invoke base method from the base class!!!
El'Cachubrey wrote:
I want to invoke base method from the base class!!!
:confused: Base classes methods are invoked using
base
keyword from a child class. What are you trying to do?Navaneeth How to use google | Ask smart questions
-
El'Cachubrey wrote:
I want to invoke base method from the base class!!!
:confused: Base classes methods are invoked using
base
keyword from a child class. What are you trying to do?Navaneeth How to use google | Ask smart questions
Not base classes!!!. I try to invoke from base class method other method of the same base class even if one overriden in derived class. Like that in C++ ((BaseClass)this).Fun2(); But in C# - it's diffrent i wan't to know how. :confused::confused::confused:
-
John Simmons / outlaw programmer wrote:
First, you have to do this: class DervClass : BaseClass
Yes, it's just misstyping.
John Simmons / outlaw programmer wrote:
econd, in your derived class, you have to do this: protected override void Fun2() { base.Fun2(); }
I can't do this in derived class snice implemented logic.
El'Cachubrey wrote:
I can't do this in derived class snice implemented logic.
First you tell us you want to do what I told you how to do, and then you tell us you *can't* do it because of logic in your code. Make up your freakin' mind, or stop wasting our time. If your "logic" prevents you from doing what you need to do, then your code is completely screwed up, and you need to start over.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
Hi all. I have OOP Question. Can i invoke base class methods instead overridens; in C# Examp:
class BaseClass { public void Fun1() { this.Fun2();//I want write here - base class method //What i should write here for invoke Fun2() of this class (exactly this class, // not derived), i suppose what i shoul write as in C++ ((BaseClass)this).Fun2(); //But this not work, in this case still invoke method of derived class. } protected virtual void Fun2() { WriteLine("base class method"); } } class DervClass : BaseClass { protected overriden void Fun2() { WriteLine("overriden class method"); } } .... DervClass derv = new DervClass(); derv.Fun1();
modified on Wednesday, December 17, 2008 6:37 AM
First let me check I understand what you are trying to do
class Program
{
static void Main(string[] args)
{
MySub instance = new MySub();instance.CallMe(); Console.ReadLine(); } } public class MyBase { public virtual void Method1() { Console.WriteLine("I want this to run"); } public void CallMe() { Method1(); } } public class MySub : MyBase { public override void Method1() { Console.WriteLine("But this is what really gets run"); } }
You create a new instance of the sub class, and call "CallMe" which triggers a method. You want it to trigger the method in the base class, and not the one in the sub class. Well, like others have said, the standard way to do this would be to put
base.Method1()
in the Method1 in the subclass to call the base method. But for some reason you don't want to do that. I'm assuming you don't want to do that because sometimes, when you call method1 directly on the subclass, you do want it to run. This is the nature of what 'override' means. The method1 in the subclass overrides the method1 in the base. Perhaps the override behaviour isn't what you want. Perhaps you should just consider naming the methods with different names. Then you can call which ever one you want to call. Alternatively, consider using the 'new' keyword.public class MySub : MyBase { public new void Method1() { Console.WriteLine("But this is what really gets run"); } }
Using the 'new' keyword makes the method1 in the sub class hide the method1 in the base class, but not override it. This means when you cast back down to a base class (or call from within the base class) it's the base's method that gets run, because nothing is overriding it, instead, when you have an instance of the subclass, the method1 just hides the bases method1. Confusing. Yeah. I can't explain it very clearly. Try running the snippets I've given you and see the difference. Be careful if you use new. This isn't very standard behaviour, and other people may easily be surprised by what happens. You should probably just be naming your methods differently if they have different behaviour. Some links on the subject: htt
-
El'Cachubrey wrote:
I can't do this in derived class snice implemented logic.
First you tell us you want to do what I told you how to do, and then you tell us you *can't* do it because of logic in your code. Make up your freakin' mind, or stop wasting our time. If your "logic" prevents you from doing what you need to do, then your code is completely screwed up, and you need to start over.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001Cool up. I don't beg you to respond my question. I make example just for explanate what i try to do and i mean exactly what i mean how i can invoke base class member from other base class member even if it's one overriden in derived class. Be more attentive in future :laugh: .
-
First let me check I understand what you are trying to do
class Program
{
static void Main(string[] args)
{
MySub instance = new MySub();instance.CallMe(); Console.ReadLine(); } } public class MyBase { public virtual void Method1() { Console.WriteLine("I want this to run"); } public void CallMe() { Method1(); } } public class MySub : MyBase { public override void Method1() { Console.WriteLine("But this is what really gets run"); } }
You create a new instance of the sub class, and call "CallMe" which triggers a method. You want it to trigger the method in the base class, and not the one in the sub class. Well, like others have said, the standard way to do this would be to put
base.Method1()
in the Method1 in the subclass to call the base method. But for some reason you don't want to do that. I'm assuming you don't want to do that because sometimes, when you call method1 directly on the subclass, you do want it to run. This is the nature of what 'override' means. The method1 in the subclass overrides the method1 in the base. Perhaps the override behaviour isn't what you want. Perhaps you should just consider naming the methods with different names. Then you can call which ever one you want to call. Alternatively, consider using the 'new' keyword.public class MySub : MyBase { public new void Method1() { Console.WriteLine("But this is what really gets run"); } }
Using the 'new' keyword makes the method1 in the sub class hide the method1 in the base class, but not override it. This means when you cast back down to a base class (or call from within the base class) it's the base's method that gets run, because nothing is overriding it, instead, when you have an instance of the subclass, the method1 just hides the bases method1. Confusing. Yeah. I can't explain it very clearly. Try running the snippets I've given you and see the difference. Be careful if you use new. This isn't very standard behaviour, and other people may easily be surprised by what happens. You should probably just be naming your methods differently if they have different behaviour. Some links on the subject: htt
Nicely. Instead "Confusing" it's very clear. Let's make a count: 1. There is no exists way to invoke Base Class method if they overriden in derived classes. 2. Only way to invoke Base Class method it's mark overriden method with "new" keyword in derived class.
-
Nicely. Instead "Confusing" it's very clear. Let's make a count: 1. There is no exists way to invoke Base Class method if they overriden in derived classes. 2. Only way to invoke Base Class method it's mark overriden method with "new" keyword in derived class.
El'Cachubrey wrote:
1. There is no exists way to invoke Base Class method if they overriden in derived classes.
You can call base.method1() from the derived class. I don't know of any other way. Although I'm not Anders[^], there could be a way, I just can't think of one. Perhaps someone with more experience can confirm or deny this.
El'Cachubrey wrote:
2. Only way to invoke Base Class method it's mark overriden method with "new" keyword in derived class.
Well, like I said, it might not be the only way. It's a way though. Just be aware that the 'new' keyword causes different behaviour to the override keyword. What you are doing is conceptually something slightly different. Make sure you understand all the behaviour differences and consequences and that they are what you want before you do it. (And don't forget, other people who use your code won't be expecting this, so note it heavily in the comments, or consider naming the methods differently)
Simon
-
El'Cachubrey wrote:
1. There is no exists way to invoke Base Class method if they overriden in derived classes.
You can call base.method1() from the derived class. I don't know of any other way. Although I'm not Anders[^], there could be a way, I just can't think of one. Perhaps someone with more experience can confirm or deny this.
El'Cachubrey wrote:
2. Only way to invoke Base Class method it's mark overriden method with "new" keyword in derived class.
Well, like I said, it might not be the only way. It's a way though. Just be aware that the 'new' keyword causes different behaviour to the override keyword. What you are doing is conceptually something slightly different. Make sure you understand all the behaviour differences and consequences and that they are what you want before you do it. (And don't forget, other people who use your code won't be expecting this, so note it heavily in the comments, or consider naming the methods differently)
Simon
GRATEFULL
-
Nicely. Instead "Confusing" it's very clear. Let's make a count: 1. There is no exists way to invoke Base Class method if they overriden in derived classes. 2. Only way to invoke Base Class method it's mark overriden method with "new" keyword in derived class.
El'Cachubrey wrote:
1. There is no exists way to invoke Base Class method if they overriden in derived classes.
Wrong, wrong, wrong. I showed you how to do it.
El'Cachubrey wrote:
2. Only way to invoke Base Class method it's mark overriden method with "new" keyword in derived class. Quote Selected Text
Wrong again. If you make a "new" method, it completely hides the base version from other objects that use your derived class.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
Hi all. I have OOP Question. Can i invoke base class methods instead overridens; in C# Examp:
class BaseClass { public void Fun1() { this.Fun2();//I want write here - base class method //What i should write here for invoke Fun2() of this class (exactly this class, // not derived), i suppose what i shoul write as in C++ ((BaseClass)this).Fun2(); //But this not work, in this case still invoke method of derived class. } protected virtual void Fun2() { WriteLine("base class method"); } } class DervClass : BaseClass { protected overriden void Fun2() { WriteLine("overriden class method"); } } .... DervClass derv = new DervClass(); derv.Fun1();
modified on Wednesday, December 17, 2008 6:37 AM
By marking the method in the base class virtual and the method in the derived class override, you are stating that you do not want to be able to call the base class' version directly from an instance of the derived class, and therefore it can't be done. If, you do want to be able to call the base class' version from an instance of the derived class, then do not use virtual and override.
-
By marking the method in the base class virtual and the method in the derived class override, you are stating that you do not want to be able to call the base class' version directly from an instance of the derived class, and therefore it can't be done. If, you do want to be able to call the base class' version from an instance of the derived class, then do not use virtual and override.
PIEBALDconsult wrote:
and therefore it can't be done.
Well, not *technically*...
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001