Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
how to Make sure base method gets called in DerivedClass C#.
C#
class a
{
public virtual fun1(){}
}
class b: a
{
static void mail(string[]args)
{
// here how we do call all base method must call.
// not using like base().fun1();
}
}
Posted
Updated 9-Dec-13 22:28pm
v3
Comments
BillWoodruff 10-Dec-13 5:29am    
Why do you not want to call 'base.fun()' directly, which you can do if you make A's 'fun not virtual ?

class A
{
public virtual void Fun()
{
Console.Write("Class A!");
}
}

class B : A
{
public override void Fun()
{
// call the base method which will print Class A!
base.Fun();
Console.Write("Class B!");
}
}
Now when you do this:

B b = new B();
b.Fun();

Check this once...........
 
Share this answer
 
Comments
rajacsharp5 10-Dec-13 4:35am    
Thanks for reply,subbuksr123.
this i had done im looking for another way.
Have a look at this Stack Overflow question: "Automatically call base method"[^].
 
Share this answer
 
Comments
rajacsharp5 10-Dec-13 4:40am    
Ok, got it,

Thans CPallini
CPallini 10-Dec-13 4:42am    
You are welcome.
Why do you not want to call Class A's 'base.fun();' directly in Class 'B, which you can do whether or not you make A's 'fun virtual ?

Remember that a function marked virtual has the option to provide an implementation.
C#
public class A
{
    public virtual void fun() { Console.WriteLine("Class A fun");}
}

public class B: A
{
    public void fun()
    {
        base.fun();
        Console.WriteLine("Class B fun");
    }
}
No use of 'override, or 'new, required, no compiler warnings.

You are never going to get automatic calling of inherited ancestor methods because .NET does not implement event-bubbling as part of its inheritance facility.
 
Share this answer
 
Comments
rajacsharp5 10-Dec-13 6:03am    
that ok,
want to know using the other way.
thanks for reverting.interface IFunClass {
void Fun();
}

class A : IFunClass {
public void IFunClass.Fun() {
Console.Write("Class A!");
}
}

class B : IFunClass {
public B(IFunClass f) {
this.m_SomethingFun = f;
}

public void IFunClass.Fun() {

this.m_SomethingFun.Fun();

Console.Write("Class B!");
}

private IFunClass m_SomethingFun;
}

A a = new A();
B b = new B(a);
b.m_somethingfun.fun();
http://stackoverflow.com/questions/4118925/automatically-call-base-method
that is the source we can do
BillWoodruff 10-Dec-13 9:11am    
If you got any benefit from my response, I am happy :)

I am aware of that StackOverFlow thread, and all the examples there are doing is moving around the way that the base method is called ... there's really no qualitative difference between those methods, and the simpler one I show here.

Even though one method may seem "sexier" than the other :) ... .NET simply does not implement automatic calling of base methods.
rajacsharp5 17-Dec-13 10:11am    
i got you,
i just got thought so searching when iam free.
till now i just telling people using sealed method we can use.
after practising sample example i came to know this.

thans for your responce.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900