overloading a constructor and call one from the other

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I want to overload a constructor of a class. But I want to call the other
one from the second if called. I explain with code because of my english:

public class XMLConfig
{
public XMLConfig(string FileName)
{
// here I want to do something with Filename and some init
}

public XMLConfig()
{
// this one should call the other one with a default filename
XMLConfig("default_filename");
}

Can someone explain how to do this ? If I do it as this then I get a
compiler error.
 
public class XMLConfig
{
public XMLConfig(string FileName)
{
// here I want to do something with Filename and some init
}

public XMLConfig()
: this("default_filename")
{
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi,

I want to overload a constructor of a class. But I want to call the other
one from the second if called. I explain with code because of my english:

public class XMLConfig
{
public XMLConfig(string FileName)
{
// here I want to do something with Filename and some init
}

public XMLConfig()
{
// this one should call the other one with a default filename
XMLConfig("default_filename");
}

Can someone explain how to do this ? If I do it as this then I get a
compiler error.
 
Hi Richard,
public XMLConfig(): this("default_filename")
{
}

Thanks, this compiles, but I was not clear in my question. the 'default
filename' has to be computed in the overloaded constructor. With this coding
I can only give a fixed string to it. Or again I dont see something not :(

for exampel I want to call the create method in overloaded constructor like
somethin this:

XMLConfig(Path.ChangeExtension(Application.ExecutablePath, ".xml");

rgds, Wilfried
http://www.mestdagh.biz
 
Wilfried Mestdagh said:
Thanks, this compiles, but I was not clear in my question. the 'default
filename' has to be computed in the overloaded constructor. With this coding
I can only give a fixed string to it. Or again I dont see something not :(

for exampel I want to call the create method in overloaded constructor like
somethin this:

XMLConfig(Path.ChangeExtension(Application.ExecutablePath, ".xml");

So do:

public XMLConfig() : this (Path.ChangeExtension
(Application.ExecutablePath, ".xml"))
{
}
 
Hi Jon,
public XMLConfig() : this (Path.ChangeExtension
(Application.ExecutablePath, ".xml"))
{
}

Amazing how solutions can be so simple :) Thanks !
One more question because I anger to learn :)

What if I want to have an overloaded constructor that has to execute a bunch
of extra code and then after it calls another constructor ?

eg:
public XMLConfig()
{
// here a lot of code
// after it call another overload constructor
}

thx, Wilfried
 
Wilfried Mestdagh said:
Amazing how solutions can be so simple :) Thanks !
One more question because I anger to learn :)

What if I want to have an overloaded constructor that has to execute a bunch
of extra code and then after it calls another constructor ?

eg:
public XMLConfig()
{
// here a lot of code
// after it call another overload constructor
}

No, you can't do that - but you *can* call another method in the
parameter part, as shown with the example given before. (That was
Path.ChangeExtension, but it could be any other method, including
static methods in the same class.)

Of course, that doesn't help you if you need to call another
constructor which doesn't take any parameters.

If you find constructors restrictive, you may want to think about
making static methods which return an instance.
 
Back
Top