I have a situation where I want to call only one method and have all the methods its derived from called also.
public class Base
{
public virtual void Method()
{
// Do something
}
}
public class Derived1 : Base
{
public override void Method()
{
base.Method();
// Do something
}
}
public class Derived2 : Derived1
{
public override void Method()
{
base.Method();
// Do something
}
}
I want to only call Derived2.Method() and have all the Methods() called that it derives from
The issue with the way I currently have it set up is that Derived1.Method() never gets called.
Is there a way to make the called method call all the previous instances of it in a derived class?
Related
Is it possible to get a reference to the class that called a virtual method, from within the method defined in the abstract class?
Basically, I have an abstract class, let's say BaseAction, and this contains a virtual method, called RetrieveData:
public abstract class BaseAction
{
protected virtual void RetrieveData()
{
}
}
in the implementation, I pass this virtual method into a method as an Action, something to this effect:
public class Action: BaseAction
{
public Action()
{
ActionStatement(RetrieveData);
}
}
Is is possible to get a reference to Action class, in the RetrieveData method without having to override it in the Action class, something to this effect:
public abstract class BaseAction
{
protected virtual void RetrieveData()
{
// using reflection to get a handle on instance of Action?
}
}
The reason for this is, that I want to use this virtual method in various different type of classes, each one having an ID field which needs to be modified, but I don't want to override this virtual method in each of the 20+ action classes, just to change the ID field.
I'd like this to happen in the base class, to limit the amount of code duplication.
You don't need reflection to this. You have many options:
If the ID field is common in each implemented class, just declare it in the abstract class and change the value in the RetrieveData() method.
public abstract class BaseAction
{
protected int ID;
protected virtual void RetrieveData()
{
}
}
If the ID field is not common across all implemented classes, you can add an abstract method and use that method to access the ID property.
public abstract class BaseAction
{
protected abstract ChangeID(int value);
protected virtual void RetrieveData()
{
//Do your stuff
ChangeID(<with-new-value>);
}
}
public class Action: BaseAction
{
Protected override ChangeID(int value)
{
//Do whatever you want
}
public Action()
{
ActionStatement(RetrieveData);
}
}
If you just do:
public abstract class BaseAction
{
protected void Foo()
{
Console.WriteLine(this.GetType());
}
}
public class Action : BaseAction
{
public void Bar()
{
Foo();
}
}
// the runtime type of foo will be Action regardless of compile-time type
BaseAction foo = new Action();
foo.Bar();
it will output Action - that is, in an inherited method (Foo()) the object this will already be of the inheriting type Action if the method was ultimately called through an object of runtime type Action.
(Ignoring for now things like the new modifier on methods or explicit interface implementation.)
I have a merly simple question, but seems cant find an answer for it, I want to know if its possible to override a method from a instance class structore would look like this:
public class A : baseA
{
public virtual void methodA()
{
}
}
public class B : baseB
{
public void method B()
{
var ClassA = new A();
}
/* Now Is there some sort of overide like */
public override methodA()
{
//Do stuff
}
}
And those classes do not inherit from each other, to make it more difficult.
Now if this sort of construction is possible in c#?
No. You cannot override a class's behavior if you don't inherit from it.
The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event.
Class B must inherit from class A in order to do so.
public class A
{
public virtual void methodA()
{
}
}
public class B : A
{
public void methodB()
{
var ClassA = new A();
}
public override void methodA()
{
//Do stuff
}
}
Check MSDN for more details:
An override method provides a new implementation of a member that is inherited from a base class. The method that is overridden by an override declaration is known as the overridden base method. The overridden base method must have the same signature as the override method
I have a base and a derived class as below:
//base class
public class BST
{
public virtual void Find();
public virtual void Insert()
{
Find();
}
}
// derived class
public class splayTree:BST
{
public override void Find()
{
base.Find();
...
}
public override void Insert()
{
base.Insert();
.....
}
}
After I created a splayTree object splayTree,
I call splayTree.Insert();
Insert() calls base.Insert()
base.Insert() calls Find() of the derived class.
How can I let base.Insert() call Find() of base class?
Thank you.
The base class has defined Find as Virtual, which is a signal that the child class may override it and if so, the base class will use that override.
If you want to call the base Find in your child class, just do not implement Find.
If you want to define a method called Find in your child class, that is not virtually connected to base.Find, use the new keyword instead of the override keyword.
By not overriding the Find method:
//base class
public class BST
{
public virtual void Find();
public virtual void Insert()
{
Find();
}
}
// derived class
public class splayTree:BST
{
public void Find()
{
base.Find();
...
}
public override void Insert()
{
base.Insert();
.....
}
}
If you want to call the base class' Find() method but still retain flexibility of having Find() be a virtual method with derived implementations, break out the logic of the base.Find() method into a private method you can call explicitly from the base class:
//base class
public class BST
{
private void _Find()
{
// base.Find() implementation here
}
public virtual void Find()
{
_Find();
}
public virtual void Insert()
{
_Find();
}
}
// derived class
public class splayTree:BST
{
public override void Find()
{
base.Find();
...
}
public override void Insert()
{
base.Insert();
.....
}
}
I want to be able to run the method in my base class which is displaySomething()
and in addition run the method in my subclass called displaysomething()
Is this possible? How can I achieve this please?
I have a base class that looks like this
public class baseClass
{
public void displaySomething()
{
MessageBox.Show("Method from base class: display something");
}
}
I have a subclass that looks like this
public class subClass : baseClass
{
public void displaySomething()
{
MessageBox.Show("Additional method to run after base class method");
}
}
I have a button click event that looks like this.
private void button1_Click(object sender, EventArgs e)
{
subClass mySubClass = new subClass();
mySubClass.displaySomething();
}
You need to add the call of the overriden method of the base class in the implementation of the overriding method of the subclass, like this:
public void displaySomething() {
// You can decide to call the base before, after,
// or in the middle of your new method.
base.displaySomething();
MessageBox.Show("Additional method to run after running base class method");
}
Note that your code does not override the method, because it is not declared virtual. You need to declare it like this:
public class baseClass {
public virtual void displaySomething() ...
}
public class subClass : baseClass {
public override void displaySomething() ...
}
I have a base class and a class inheriting base. The base class has several virtual functions that the inherited class may override. However, the virtual functions in the base class has code that MUST to run before the inherited class overrides get called. Is there some way that I can call the base classes virtual functions first then the inherited class overrides. Without making a call to base.function().
I know I can simply make two functions, one that gets called, the other virtual. But is there a way I can keep the same names as well? I know I may need to change some things around.
class myBase
{
public virtual myFunction()
{ /* must-run code, Called first */ }
}
class myInherited : myBase
{
public override myFunction()
{ /* don't use base.myFunction();,
called from base.myFunction(); */ }
}
Similar question here.
C# doesn't have support for automatically enforcing this, but
you can enforce it by using the template method pattern. For example, imagine you had this code:
abstract class Animal
{
public virtual void Speak()
{
Console.WriteLine("I'm an animal.");
}
}
class Dog : Animal
{
public override void Speak()
{
base.Speak();
Console.WriteLine("I'm a dog.");
}
}
The trouble here is that any class inheriting from Animal needs to call base.Speak(); to ensure the base behavior is executed. You can automatically enforce this by taking the following (slightly different) approach:
abstract class Animal
{
public void Speak()
{
Console.WriteLine("I'm an animal.");
DoSpeak();
}
protected abstract void DoSpeak();
}
class Dog : Animal
{
protected override void DoSpeak()
{
Console.WriteLine("I'm a dog.");
}
}
In this case, clients still only see the polymorphic Speak method, but the Animal.Speak behavior is guaranteed to execute. The problem is that if you have further inheritance (e.g. class Dachshund : Dog), you have to create yet another abstract method if you want Dog.Speak to be guaranteed to execute.
A common solution that can be found in the .NET Framework is to split a method in a public method XXX and a protected, virtual method OnXXX that is called by the public method. For your example, it would look like this:
class MyBase
{
public void MyMethod()
{
// do something
OnMyMethod();
// do something
}
protected virtual void OnMyMethod()
{
}
}
and
class MyInherited : MyBase
{
protected override void OnMyMethod()
{
// do something
}
}
public abstract class BaseTemp
{
public void printBase() {
Console.WriteLine("base");
print();
}
public abstract void print();
}
public class TempA: BaseTemp
{
public override void print()
{
Console.WriteLine("TempA");
}
}
public class TempB: BaseTemp
{
public override void print()
{
Console.WriteLine("TempB");
}
}
There is no way to do what you're seeking other than the 2 ways you already named.
Either you make 2 functions in the base class, one that gets called and the other virtual.
Or you call base.functionName in the sub-class.
Not exactly. But I've done something similar using abstract methods.
Abstract methods must be overriden by derived classes. Abstract procs are virtual so you can be sure that when the base class calls them the derived class's version is called. Then have your base class's "Must Run Code" call the abstract proc after running. voila, your base class's code always runs first (make sure the base class proc is no longer virtual) followed by your derived class's code.
class myBase
{
public /* virtual */ myFunction() // remove virtual as we always want base class's function called here
{ /* must-run code, Called first */
// call derived object's code
myDerivedMustcallFunction();
}
public abstract myDerivedMustCallFunction() { /* abstract functions are blank */ }
}
class myInherited : myBase
{
public override myDerivedMustCallFunction()
{ /* code to be run in derived class here */ }
}
What do you think of this?
class myBase
{
public void myFunctionWrapper()
{
// do stuff that must happen first
// then call overridden function
this.myFunction();
}
public virtual void myFunction(){
// default implementation that can be overriden
}
}
class myInherited : myBase
{
public override void myFunction()
{
}
}