Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
how call function (method) in class parent in child class?
Posted 3-Mar-12 3:07am
Edited 3-Mar-12 3:24am
v3
Comments
SAKryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You need a reference to the parent class of course. Without a MUCH better description, we can't help much beyond that.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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:
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 Smile | :) ...
  Permalink  
Comments
ghahreman sayyali at 4-Mar-12 11:00am
   
very thanks
nortee at 4-Mar-12 12:00pm
   
Glad to have helped :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

If you want to call a none static method of the parent in a child class, the method must be public or protected:
 
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:
 
ChildClass instance = new ChildClass();
instance.MethodName();
 
Or
 
(new ChildClass()).MethodName();
 
It's better If you explain why do you want to do so.
  Permalink  
v3

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



Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 3 Mar 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100