Simple question on constructor

D

DBC User

I have 2 constructors in my class, first one with no paramter and the
second one is with a paramter. I want the later constructor to use the
first constructor and do some more with the parameter. How can I do
this?

eg:

public const()
{
//do base work
}

public const(string data)
{
//do base work --- Here is where I want to reuse the default
constructor
path = data;
}

It is a simple one but somehow I am not getting it correct.

Thanks.
 
J

James Swindell

You can do this in one to two ways:

1. Call the base constructor (parameterless) one from the additional one:

public const(string data) : this()
{
}

or,

2. Call the additional constructor from the base one with a default value
(probably null ???):

public const() : this(null)
{
}

James
 
P

Peter Duniho

DBC User said:
I have 2 constructors in my class, first one with no paramter and the
second one is with a paramter. I want the later constructor to use the
first constructor and do some more with the parameter. How can I do
this?

IMHO, it is better to have the parameterless constructor call the one with
parameters, passing a default value. For example:

public const() : this("default string")
{
}

public const(string data)
{
}

(ignoring for the moment that I would never call a class "const" :) ).

Pete
 

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