multiple constructors - can they call each other?

  • Thread starter Thread starter Mark
  • Start date Start date
M

Mark

public class myClass
{
public myClass(string param1)
{
//HOW DO I CALL myClass() below??
myClass(); // THIS DOES NOT WORK
}

public myClass()
{
//Do something here that ALL constructors should do. Hence, I want
to call it from
}
}

NOTE: I could just make a different method that the constructors call, but
I'd like to avoid this. Is there a way to do what I'm trying to do?
It also doesn't make sense to do "new myClass()" in the first constructor
because that just creates an entirely new instance of the class.

Thanks in advance!
Mark
 
public class myClass
{
public myClass(string param1)
{
//HOW DO I CALL myClass() below??
myClass(); // THIS DOES NOT WORK
}

public myClass()
{
//Do something here that ALL constructors should do. Hence, I want
to call it from
}
}

NOTE: I could just make a different method that the constructors call, but
I'd like to avoid this. Is there a way to do what I'm trying to do?
It also doesn't make sense to do "new myClass()" in the first constructor
because that just creates an entirely new instance of the class.

Thanks in advance!
Mark


You can do this using


public myClass(string param1) : this() <------------- here
{
//HOW DO I CALL myClass() below??
myClass(); // THIS DOES NOT WORK
}

Even when the other constustors take parameters you can pass them using
public myClass(string param1, int param2) : this(param2) .........

if you had a construcor like

public myClass(int param2)
 
Hello Mark,

Yes in there own kind of way, you do something like this;

public class MyClass
{
public MyClass() //One of your constructors
{
//Something(s) you want this constructor to do fundamentally...
}

public MyClass(string param1) : this()
{
//Other things you want done but need to to or want to do other
things from the constructor above
}

public MyClass(int nAge, string param1) : this(param1)
{
//Get the picture???
}

} //class MyClass

Hope this helps you Mark go well.

SpotNet ;~?
 
Back
Top