Click here to Skip to main content
15,940,430 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how call function (method) in class parent in child class?
Posted
Updated 3-Mar-12 2:24am
v3
Comments
Sergey Alexandrovich Kryukov 3-Mar-12 19:23pm    
What do you mean as child and parent? If could be nested classes, composition... what?
Some code sample, please.
And what is the problem. Just call it... Access? Understanding of types and members, instance vs. static? What?
--SA

You need a reference to the parent class of course. Without a MUCH better description, we can't help much beyond that.
 
Share this answer
 
Hi,

Based on what you are asking (it wasn't very clear but I will give it a bash), you will be looking at a scenario like this:
C#
public class MyParentClass
{
    public virtual void SomeMethod()
    {
        /* do parent class stuff here */
    }
}

public class MyChildClass : MyParentClass
{
    public override void SomeMethod()
    {
        /* do child class stuff here */
        base.SomeMethod(); // <--- This will call the parent class method
    }
}


At any point in the overridden method in the child class you can call "base.SomeMethod()".

Hope that helps :)...
 
Share this answer
 
Comments
ghahreman sayyali 4-Mar-12 11:00am    
very thanks
nortee 4-Mar-12 12:00pm    
Glad to have helped :)
If you want to call a none static method of the parent in a child class, the method must be public or protected:

C#
base.MethodName();


If you want to call a none static method of the child in a parent class:
you have to have an instance of the child class and the method must be a public method:

C#
ChildClass instance = new ChildClass();
instance.MethodName();


Or

C#
(new ChildClass()).MethodName();


It's better If you explain why do you want to do so.
 
Share this answer
 
v3

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