if derived class adds an argument to ctor, does that need to carry up to the base classes?

H

hazz

I have a class/constructor hiearchy that functions correctly as illustrated
below (thanks to Ken Kolda's earlier newsgroup assistance.) If a client
instantiates object D, eg. D m_D = new D(str_arg), it climbs up the ctor
stack until B(string str) handles it.

A();

B();
B(string str)
{
do_string_Stuff; }

C();
C(string str) : base(str);

D();
D(string str) : base(str);

Now for the change. I have a class that has this ctor signature.
D ( long n_arg, string str) // :base(str) won't work anymore. will
it have to be :base(n_arg, str)

D m_D = new D(long n_arg2, string str_arg ) would work, just adding an
additional string argument, but I want to get up to the overloaded ctor in
B. How do I change this object design in the least invasive manner? Do I
have to add new constructors with the additional n_arg to C and B for this
to work as per the following?

A();

B();
B(string str)
{ do_string_stuff; }
B (long n_arg, string str)
{ do_string_stuff; }

C();
C(string str) : base(str);
C( long n_arg, string str) : base(n_arg, str);

D();
D(string str) : base(str);
D (long n_arg, string str) : base(n_arg, str);

Thank you very much,
-greg
 
J

Jon Skeet [C# MVP]

hazz said:
I have a class/constructor hiearchy that functions correctly as
illustrated below (thanks to Ken Kolda's earlier newsgroup
assistance.) If a client instantiates object D, eg. D m_D = new
D(str_arg), it climbs up the ctor stack until B(string str) handles
it.

A();

B();
B(string str)
{
do_string_Stuff; }

C();
C(string str) : base(str);

D();
D(string str) : base(str);

Now for the change. I have a class that has this ctor signature.
D ( long n_arg, string str) // :base(str) won't work anymore. will
it have to be :base(n_arg, str)

base(str) should work absolutely fine. Why do you think it wouldn't?
 
I

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

hazz said:
I have a class/constructor hiearchy that functions correctly as illustrated
below (thanks to Ken Kolda's earlier newsgroup assistance.) If a client
instantiates object D, eg. D m_D = new D(str_arg), it climbs up the ctor
stack until B(string str) handles it.

A();

B();
B(string str)
{
do_string_Stuff; }

C();
C(string str) : base(str);

D();
D(string str) : base(str);

Now for the change. I have a class that has this ctor signature.
D ( long n_arg, string str) // :base(str) won't work anymore. will
it have to be :base(n_arg, str)


It does has to work !
It will also work :this( str) which call the other constructor of D


Cheers,
 
H

hazz

thanks for responding Jon.
base(str) DID work absolutely fine as you correctly pointed out.
However, D (long n_arg, string str) has been added to the mix.
So should D (long n_arg, string str) : base(n_arg, str) work absolutely
fine as per my best attempt to try to explain how I 'thought' it might work
as per the psuedocode that follows immediately;

A();

B();
B(string str)
{ do_string_stuff; }
B (long n_arg, string str)
{ do_string_stuff; }

C();
C(string str) : base(str);
C( long n_arg, string str) : base(n_arg, str);

D();
D(string str) : base(str);
D (long n_arg, string str) : base(n_arg, str);

If I am missing the obvious, please let me know.

Appreciatively,
-greg
 
H

hazz

Thank you Ignacio. You are mentioning the 'this' keyword. Like this;

A();

B();
B(string str)
{ do_string_stuff; }

C();
C(string str) : base(str);

D();
D(string str) : base(str);
D ( long n_arg, string str) : this

D m_D = new D(n_arg, str_arg) would then instantiate properly and
additionally would pass the string up to B. Correct?

thx, -greg
 
J

Jon Skeet [C# MVP]

hazz said:
thanks for responding Jon.
base(str) DID work absolutely fine as you correctly pointed out.
However, D (long n_arg, string str) has been added to the mix.
So should D (long n_arg, string str) : base(n_arg, str) work absolutely
fine as per my best attempt to try to explain how I 'thought' it might work
as per the psuedocode that follows immediately;

Yes, it should work fine. So long as you either explicitly call a
constructor of the base class with appropriate arguments, or call
another constructor of the current class, or don't do anything and
leave the compiler to add an implicit base() call (which obviously has
to be valid) you should be okay.

See http://www.pobox.com/~skeet/csharp/constructors.html for more
information.
 

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