473,383 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

the question of C# inherited

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") ;
}

}
Nov 16 '05 #1
5 1680
ad <ad@wfes.tcc.edu.tw> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
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();
}
}

"ad" <ad@wfes.tcc.edu.tw> wrote in message
news:uk**************@TK2MSFTNGP11.phx.gbl...
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") ;
}

}

Nov 16 '05 #3
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

"ad" <ad@wfes.tcc.edu.tw> wrote in message
news:uk**************@TK2MSFTNGP11.phx.gbl...
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") ;
}

}

Nov 16 '05 #4
ad
Thanks a lot!
"Sean Hederman" <us***@blogentry.com> ¼¶¼g©ó¶l¥ó·s»D
:cs**********@ctb-nnrp2.saix.net...
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

"ad" <ad@wfes.tcc.edu.tw> wrote in message
news:uk**************@TK2MSFTNGP11.phx.gbl...
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") ;
}

}


Nov 16 '05 #5
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();
}
}
"ad" <ad@wfes.tcc.edu.tw> wrote in message
news:uk**************@TK2MSFTNGP11.phx.gbl...
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") ;
}

}

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jeff Levinson [mcsd] | last post by:
I guess I would have to know what you mean by "not being able to edit the forms". Does this mean you get an error in the designer when you try to display an inherited form? Does this mean the...
2
by: Terrance | last post by:
Hello all I have a question I'm hoping someone can shed some light on. I have 2 user Inherited Controls on a form. When I click 1 inherited control I would like to change the text of the other...
4
by: Dan | last post by:
I have a need to make a set of classes that all share the same public methods, some implementation and some data. So, I made an abstract base (BaseClass) with an interface (IBaseClass) and a...
13
by: Lorne Smith | last post by:
Hi, First, sorry for the crosspost, but it seemed appropriate... :) I've come accross what I consider to be a bug, but I don't know if it's already known or not. (VS .Net 2003 Pro - VB.Net) ...
8
by: Spam Trap | last post by:
I am getting strange resizing problems when using an inherited form. Controls are moving themselves seemingly randomly, but reproducibly. "frmBase" is my base class (a windows form), and...
3
by: Jeff User | last post by:
Hello I am using C#, .net1.1 Vis Studio 2003 I am using homeBase.aspx.cs page as a base for several other aspx/aspx.cs web pages. The base page handles some operations that are common to all...
4
by: asad.naeem | last post by:
hi to all this is the problem about inheritence. I have designed a form with some essential controls which are required for every form which will inherited from it. for example i have Button1 on...
14
by: lovecreatesbea... | last post by:
Could you tell me how many class members the C++ language synthesizes for a class type? Which members in a class aren't derived from parent classes? I have read the book The C++ Programming...
3
by: Dan Holmes | last post by:
Say i have: public class A { } public class B : A {
12
by: Janaka Perera | last post by:
Hi All, We have done a object oriented design for a system which will create a class multiply inherited by around 1000 small and medium sized classes. I would be greatful if you can help me...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.