inheritence question

M

^MisterJingo^

Hi all,

If I call the base class from a derived classes constructor, is the
base class constructor executed before the calling derived classes
constructor? :

public someclass( someparam foo) : base( foo)
 
G

Guest

The base class constructor is ALWAYS called before a derived class constructor.

WhiteWizard
aka Gandalf
MCSD.NET, MCAD, MCT
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Yes

Anyway It's very easy to test it:
class A{
string s;
A(string s){this.s = s;}
}
class B:A{
B(string s):base(s)
{
Console.Write(s);
}
}
 
V

Vadym Stetsyak

Hello, ^MisterJingo^!

M> If I call the base class from a derived classes constructor, is the
M> base class constructor executed before the calling derived classes
M> constructor? :

I suppose code sample below can answer your question

public class Base
{
public Base()
{
Console.WriteLine("Base");
}
}
public class Child : Base
{
public Child() : base()
{
Console.WriteLine("Child");
}

}
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
M

^MisterJingo^

Thanks for the quick replies all.

I've inherited a system which consists of a data layer base class.
Classes derive from this and pass the connection info to the base
constructor as such:

base(conn object, string table)

child(conn object) : base (object, table)


The problem is that I need to inherit from Child class, but I need a
way of passing my derived classes table string to the base class, else
it will be looking at the child classes table in the DB, rather than my
new classes.
Is there any way I can do this? I've thought about making that class
an interface, but i'm not sure what I have to do will work like that.
For example:

base(conn object, string table)

child(conn object) : base (object, table)

myclass(conn object)


myclass needs to be inherited from, but on occasion, myclass will also
have to be instantiated. The system I have been given shows DB tables
as represented by classes.
Each class at some point needs to be instantiated so it can be used to
interface with certain tables in the DB.

Anyone know how I can go about doing this? If it's not clear what I
mean, I can post some code examples.

Thanks :).
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

^MisterJingo^ said:
Thanks for the quick replies all.

I've inherited a system which consists of a data layer base class.
Classes derive from this and pass the connection info to the base
constructor as such:

base(conn object, string table)

child(conn object) : base (object, table)

Where does table comes from in the child class??


The above code does not compile (unless table is a constant).
 
M

^MisterJingo^

Ignacio said:
Hi,



Where does table comes from in the child class??


The above code does not compile (unless table is a constant).

Hi Ignacio,

That was a bit of pseudo code. I'll give a better example below:

public class Base
{
public Base(string _blah)
{
Console.WriteLine(_blah);
}
}

public class Child : Base
{
static string blah = "Child";
public Child(string _blah) : base(blah)
{
blah = _blah;
Console.WriteLine(blah);
}
}

public class Adult : Child
{
static string blah = "Adult";
public Adult()
: base(blah)
{
Console.WriteLine(blah);
Console.ReadLine();
}

static void Main()
{
Adult c = new Adult();
}
}

Because the child constructor calls base first, it passes the child
'blah' string to base, I need to get the adult 'blah' string down to
base. I guess I could have a method which passes this down and sets it
in the constructor. I wasn;t sure if that was the ideal solution though.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi Ignacio,

That was a bit of pseudo code. I'll give a better example below:

And again the code below is a pseudo code too

Note that you are ALWAYS passing to Base the same string ( Child.blah )

Usually you declare a constructor in a derived class that has ALL the needed
values than the base class needs (or assume constants). You are not doing
this and this is where your problems come from.
 
M

^MisterJingo^

Ignacio said:
And again the code below is a pseudo code too

The code I posted should compile.
Note that you are ALWAYS passing to Base the same string ( Child.blah )

Usually you declare a constructor in a derived class that has ALL the needed
values than the base class needs (or assume constants). You are not doing
this and this is where your problems come from.

I can't make changes to the base, and I think I must have been tired
yesterday not to see adding another constructor to the child would
solve this issue. Thanks for the help :).
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

^MisterJingo^ said:
The code I posted should compile.

Yes, but I mean that you are always passing the same string to Base, when
your original problem was how to pass a string defined in the Adult class.
 
M

^MisterJingo^

Ignacio said:
Hi,



Yes, but I mean that you are always passing the same string to Base, when
your original problem was how to pass a string defined in the Adult class.

Ah, I see what you mean :). The code was a very simplified version of
the project I've been handed to extend. But I've solved it now.

Thanks.
 

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