Call base method from the base class - c#

To remove complexity let me explain it using the below code snippet.
I want to call the parent's sum method. Now this code will call recursively and end up in stackoverflow error
public class Program
{
public static void Main()
{
var chd_obj = new child();
chd_obj.x = 10;
chd_obj.y=20;
chd_obj.sum();
}
class parent {
public int x {get;set;}
public int y {get;set;}
public virtual void sum(){
Console.WriteLine("From Base" + x+y);
}
public virtual void DoSum(){
this.sum(); // this should call the sum method in the parent class .
//But it is calling the child *sum* method
}
}
class child:parent {
public override void sum(){
base.DoSum();
}
}
}

I don't know if there is a better solution, but I would declare a private method that both sum in the base class and DoSum in the base class call:
private void SumImpl() {
Console.WriteLine("From Base" + x+y);
}
public virtual void sum(){
SumImpl();
}
public virtual void DoSum(){
SumImpl();
}

You get in infinite loop cause child class call Dosum method in Base class and again Dosum from base call child class that's why you see StackOverflow exception.
you should define what sum you mean to the compiler so the derived class can not be called again from the base class for many times.
static void Main(string[] args)
{
child sample = new child();
sample.x = 20;
sample.y = 15;
sample.sum();
Console.ReadLine();
}
public class parent
{
public int x { get; set; }
public int y { get; set; }
public virtual void sum()
{
Console.WriteLine("From Base " + (x + y));
}
public virtual void DoSum()
{
parent a = new parent();
a.x = this.x;
a.y = this.y;
a.sum();
}
}
class child : parent
{
public override void sum()
{
base.DoSum();
}
}
In this way, there is no need to create new methods but you create new instance from your parent class.

You can't do this in C#. There are workarounds, depending on your actual application.
In general, the solution requires you to extract the base's functionality into one or more non-virtual methods.
private void SumCore()
{
// Do stuff.
}
public virtual void Sum()
{
SumCore();
}

Related

Unity C# Inheritance get value from child class to parent class then to child class

I'm trying to get the ClicksCounter from FableScript child class' value to another child class and I don't know how to transfer the value. From what I achieved, the ClicksCounter are read by the parent class (GameManagerRevamped) successfully, but on the BattleSystem child class it seems that the values are not transported there.
Assuming this is my Parent Class
public class GameManagerRevamped : MonoBehaviour
{
public int ClicksCounter;
public virtual void firstButtonClicked()
{
Debug.Log(ClicksCounter);
}
}
My Child class which is FableScript
public class FableScript : GameManagerRevamped
{
public override void firstButtonClicked()
{
ClicksCounter += 1;
base.firstButtonClicked();
}
}
And my 3rd child class BattleSystem is where I'm trying to get the value of ClicksCounter
public class BattleSystem : GameManagerRevamped
{
public void BattleButton()
{
//Output is 0 here
Debug.Log("Battle Counter is" + ClicksCounter);
}
}
My goal is when the user clicks on the button (FableScript) it will increment and be read by BattleSystem.
One option is the make the click counter static in the base class. This is if it is ok that it shares that property between all child classes.
class Program
{
static void Main(string[] args)
{
var childClass1 = new ChildClass1();
var childClass2 = new ChildClass2();
childClass1.IncrementCounter();
childClass2.ConsoleLogCounter();
}
}
public class BaseClass
{
public static int counter;
}
public class ChildClass1 : BaseClass
{
public void IncrementCounter()
{
counter++;
}
}
public class ChildClass2 : BaseClass
{
public void ConsoleLogCounter()
{
Console.WriteLine(counter);
}
}

Override the implementation of a base class such that calls from inherited base class functions also call the overidden function

I have to override a function in a base class in such a way that calls to inherited functions also lead to calls to this overridden function instead of the base implementation.
class base_class
{
string abc;
public int get_1()
{
return 1;
}
public int get_number()
{
return get_1()+1;
}
}
class der_class : base_class
{
public int get_1()
{
return 2;
}
}
class Program
{
static void Main(string[] args)
{
der_class abc = new der_class();
Console.WriteLine(abc.get_number());
Console.ReadKey();
}
}
This prints 2. How can I get the output to be 3 by making the get_number to call overridden get_1?
You need the override keyword to actually override a method, otherwise you are hiding it.
class der_class : base_class
{
// note the word override here!
public override int get_1()
{
return 2;
}
}
Also, you need to make the method virtual in the base class:
class base_class
{
string abc;
// note the word virtual here!
public virtual int get_1()
{
return 1;
}
public int get_number()
{
return get_1()+1;
}
}

Nested class that inherits from its generic parent class

is this possible to somehow, have this scenario, where A.N inherits code from A with this code example?
The reason for setting it up like this, is that I need multiple classes that inherit from Base<TType> and the Nested : Base<TType> where the server has the base only, and the client has the extended Nested. This way, it would be easy to use the code, where they would have some shared code between themselves & each other.
The problem is that I would have to write identical code inside the
A and A.N
B and B.N
C and C.N
etc.
I have solved this temporarily, by replacing the Nested abstract class, with an Interface and doing
A.N : A, INested, but now I have to rewrite the Base<TType>.Nested code again inside all the Nested classes. For now, the nested class is small & managable.
hope this isn't a confusing question...
public abstract class Base<TType> where TType : class
{
public TType data;
internal void CommonCodeForAll() { }
public abstract void Update();
public abstract class Nested : Base<TType>
{
public abstract void Input();
}
}
public class A : Base<someClass>
{
public float Somevariable;
public void SpecificFunctionToA() { }
public override void Update()
{
// code that gets executed on server & client side that is unique to A
}
public class N : A.Nested
{
public override void Input()
{
if (data.IsReady()) { Somevariable *= 2; }
SpecificFunctionToA();
}
}
}
public class B : Base<anotherClass>
{
public float Somevariable;
public int index;
public int[] Grid;
public void SomethingElse() { }
public override void Update()
{
// code that gets executed on server & client side that is unique to B
}
public class N : B.Nested
{
public override void Input()
{
if (Grid[index] == -1) { SomethingElse(); }
data.Somevariable = Grid[index];
}
}
}
Edit:
I updated the code example to show what I'm trying to achieve.
Why I am trying to do this, is to keep the physics, networking & User input seperate.
There are multiple different controllers where each one has their own pack & unpacking functions, controller identity & access to the physics engine.
I have a solution using ecapsulation of classes instead of inheritance.
public abstract class BaseGeneric<T>
{
T data;
// ctor
protected BaseGeneric(T data)
{
this.data=data;
}
// methods
public abstract void Update();
// properties
public T Data
{
get { return data; }
set { data=value; }
}
// base nested class
public abstract class BaseNested<B> where B : BaseGeneric<T>
{
protected B #base;
// ctor
protected BaseNested(B #base)
{
this.#base=#base;
}
// methods
public abstract void Input(T data);
public void Update() { #base.Update(); }
// properties
public T Data
{
get { return #base.data; }
set { #base.data=value; }
}
}
}
// implementation base
public class Base : BaseGeneric<int>
{
// ctor
protected Base(int data) : base(data) { }
//methods
public override void Update()
{
this.Data+=1;
}
// implemented nested class
public class Nested : Base.BaseNested<Base>
{
// ctor
public Nested(int data) : base(new Base(data)) { }
public Nested(Base #base) : base(#base) { }
// methods
public override void Input(int data)
{
this.Data=data;
}
}
}
class Program
{
static void Main(string[] args)
{
// new implemented class with value 0
var nested=new Base.Nested(0);
// set value to 100
nested.Input(100);
// call update as implemented by `Base`.
nested.Update();
}
}

How do i change the call order of nested constructors (child before abstract parent)

The code below throws an exception because the abstract constructor is called before the child constructor.
I need to provide an abstract class to capsule some logic from a different part of the program. However i also need to check if the abstract members are initialised correctly rigth after creation without the childclass having any influence over this.
the compiling example below should illustrate my question.
using System;
namespace Stackoverflow
{
class Program
{
static void Main(string[] args)
{
var x = new Thing(5);
var y = new Child(x);
}
}
class Child : AbstractParent
{
Thing childthing;
public Child(Thing provided) : base(){
childthing = provided;
}
public override void Initialise(){
//Exception is thrown here - childthing is still null
parentthing = childthing.Add(1);
}
}
abstract class AbstractParent
{
protected Thing parentthing;
public AbstractParent(){
Initialise();
AssertThingyNotNull();
}
private void AssertThingyNotNull(){
if (parentthing == null) throw new Exception("Waaa");
}
public abstract void Initialise();
}
class Thing
{
private int i;
public Thing(int i){
this.i = i;
}
public Thing Add(int b){
i += b;
return new Thing(i);
}
}
}
Edit #1:
Is there some way to do this by reflecting into the caller (should be the creator of child rigth?) and then reacting on the end of that call?
Edit #2:
Getting the .ctor that creates the child is easy. Manipulating the methods seems something between impossible and a bad idea.
foreach (StackFrame frame in new StackTrace().GetFrames())
{
Console.WriteLine(frame.GetMethod().Name);
}
You can't, basically. This is why you should avoid calling virtual (or abstract) members from a constructor as far as possible - you could end up with code which is running with an incomplete context. Any variable initializers are executed before the base class constructor is called, but none of the code within the constructor body is.
If you need to perform initialization and only want to do that when the derived class constructor is running, then just call Initialise from the derived class constructor to start with.
You can do something similar to what Microsoft did with InitializeComponent()
then let the children call it whenever it can.
Try this.
Edited = cleaner version.
using System;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
var x = new Thing(5);
var y = new Child(x);
}
}
class Child : AbstractParent
{
public Child(Thing provided)
: base()
{
parentthing = provided;
base.Initialise();
}
}
abstract class AbstractParent
{
protected Thing parentthing;
public AbstractParent()
{
}
private void AssertThingyNotNull()
{
if (parentthing == null) throw new Exception("Waaa");
}
public void Initialise()
{
AssertThingyNotNull();
}
}
class Thing
{
private int i;
public Thing(int i)
{
this.i = i;
}
public Thing Add(int b)
{
i += b;
return new Thing(i);
}
}
}

Is there a way to specify scope?

Consider this code sample:
public abstract class Parent
{
public int val;
public Parent()
{
val = 0;
}
public virtual void foo()
{
inc();
}
public virtual void inc()
{
val = val + 10;
}
}
public class Child : Parent
{
public override void foo()
{
base.foo();
}
public override void inc()
{
val++;
}
}
static void Main(string[] args)
{
Parent p = new Child();
Console.WriteLine("p.val = " + p.val); //Output: p.val = 0
p.foo();
Console.WriteLine("P.val = " + p.val); //Output: p.val = 1
}
I am assuming the inc() of the Parent class did not get called because {this} pointer is actually pointing to a Child object so the Child's version of inc() will be called from the Parent object's function foo(). Is there a way to force the Parent's function foo() to always call parent's function inc() Like you could in C++ with :: operator?
No, the only way you can call a virtual method non-virtually is with base.Foo. Of course, you could write a non-virtual method in Parent, and make Parent.foo() call that, as well as the default implementation of Parent.inc().
You're over-thinking the problem.
If you want non-virtual dispatch then don't make the methods virtual in the first place.
If you want both virtual and non-virtual dispatch then make two methods, one virtual and one static
For example:
class Base
{
protected static void NonVirtualFoo(Base b)
{
// Whatever
}
public virtual void Foo()
{
Base.NonVirtualFoo(this);
}
}
class Derived : Base
{
protected new static void NonVirtualFoo(Derived d)
{
// Whatever
}
public override void Foo()
{
Derived.NonVirtualFoo(this);
Base.NonVirtualFoo(this);
}
}
Use the right tool for the job. If you want virtual dispatch then call a virtual method. If you want static dispatch then call a static method. Don't try to take a hammer to a virtual method and make it statically dispatched; that's working against the entire purpose of the tool.
The Child instance will call its own type implementation.
foo() calls base.foo() and base.foo() calls inc(), which in this case inc() is from the Child, since the instance is Child type, and will use this implementation.
Well, it is actually possible as said here:
This does the trick:
public abstract class Parent
{
public int val;
public Parent()
{
val = 0;
}
public virtual void foo()
{
MethodInfo method = typeof(Parent).GetMethod("inc");
DynamicMethod dm = new DynamicMethod("BaseInc", null, new Type[] { typeof(Parent) }, typeof(Parent));
ILGenerator gen = dm.GetILGenerator();
gen.Emit(OpCodes.Ldarg_1);
gen.Emit(OpCodes.Call, method);
gen.Emit(OpCodes.Ret);
var BaseInc = (Action<Parent>)dm.CreateDelegate(typeof(Action<Parent>));
BaseInc(this);
}
public virtual void inc()
{
val = val + 10;
}
}
But it's only a proof of concept: it's horrible and totally breaks the polymorphism.
I don't think you can have a valid reason to write this.

Categories

Resources