quick constructors question

  • Thread starter Thread starter francois
  • Start date Start date
F

francois

If I have a class name Foo

Is it possible to call an signature of the constructor inside one
constructor?

Like is it possible to do the following?

public class Foo
{
public Foo()
{
int defaultValue = 0;
Foo(defaultValue);
}

public Foo(int id)
{
// do stuff
}
}

Then if I can call a constructor from an other constructor, how can I
achieve it, what is the syntax?

Best regards

Francois
 
Hi,

You can't, the only way is to call another constructor is in declaration,
like:
public Foo(): this(0)
 
Does it mean that in my exemple, the constructor Foo(int id) will be called
before the constructor Foo() ? Which would make this kind of design quite
useless if I need to execute some logic to initialize the int id.

(This was an over simplified version, it could be any kind of other object
instead of an int and it could require code or logic to instanciate it)

Thank you and I hope you can tell me about the order that the constructors
are called.

francois



Miha Markic said:
Hi,

You can't, the only way is to call another constructor is in declaration,
like:
public Foo(): this(0)

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

francois said:
If I have a class name Foo

Is it possible to call an signature of the constructor inside one
constructor?

Like is it possible to do the following?

public class Foo
{
public Foo()
{
int defaultValue = 0;
Foo(defaultValue);
}

public Foo(int id)
{
// do stuff
}
}

Then if I can call a constructor from an other constructor, how can I
achieve it, what is the syntax?

Best regards

Francois
 
Back
Top