the question of C# inherited

A

ad

After runing the codes below, it will excute the parent's code first, and
then the child's code.
The result is

I am father
I am Child

How can I let it to execute the child's code first then the parent's? like
I am Child
I am father



----------------------------------------------------------------------
using System;

class UsingBase
{
static void Main()
{
aChild myaChild = new aChild() ;
Console.Read() ;
}
}
class aFather
{

public aFather()
{
Console.WriteLine("I am Child") ;
}

}
class aChild : aFather
{

public aChild()
{
Console.WriteLine("I am father") ;
}

}
 
J

Jon Skeet [C# MVP]

ad said:
After runing the codes below, it will excute the parent's code first, and
then the child's code.
The result is

I am father
I am Child

How can I let it to execute the child's code first then the parent's? like
I am Child
I am father

The parent constructor code always runs before the child constructor
code. Some options, neither of which are nice:

1) Write a virtual method in the parent, overriding it in child. Call
that method in the parent.

2) Create an instance variable in the child with an initializer which
calls a method.
 
S

Sean Hederman

You can't do that on a constructor AFAIK. The base classes constructor must
be called before the childs constructor. However, what you can do is the
following:
class aFather
{
public aFather()
{
OutputString();
}

public virtual void OutputString()
{
Console.WriteLine("I am Child") ;
}
}

class aChild : aFather
{
public override void OutputString()
{
Console.WriteLine("I am Child") ;

base.OutputString();
}
}
 
S

Sean Hederman

AFAIK there is no way to get a base class constructor to run after the child
classes constructor. However you can acheive the result you're aftter by
using an overloaded function:
class aFather
{
public aFather()
{
OutputString();
}

public virtual void OutputString()
{
Console.WriteLine("I am father");
}
}

class aChild : aFather
{
public override void OutputString()
{
Console.WriteLine("I am child");
base.OutputString();
}
}

Please note that I have switched the output strings around from your
example, since I think you made an oops there ;D
 
A

ad

Thanks a lot!
Sean Hederman said:
AFAIK there is no way to get a base class constructor to run after the child
classes constructor. However you can acheive the result you're aftter by
using an overloaded function:
class aFather
{
public aFather()
{
OutputString();
}

public virtual void OutputString()
{
Console.WriteLine("I am father");
}
}

class aChild : aFather
{
public override void OutputString()
{
Console.WriteLine("I am child");
base.OutputString();
}
}

Please note that I have switched the output strings around from your
example, since I think you made an oops there ;D
 
S

Sean Hederman

I don't think that this can be achieved in the constructor. Have a look at
the following code, which should do what you want. Please note that I moved
your output string around, since your aFather class was outputting "I am
Child", and your aChild class was outputting "I am Father".

using System;

class aFather
{
public aFather()
{
OutputString() ;
}

public virtual void OutputString()
{
Console.WriteLine("I am Father") ;
}

}
class aChild : aFather
{
public override void OutputString()
{
Console.WriteLine("I am Child") ;

base.OutputString();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top