Constructor calls for other Constructor?  
Author Message
Olgage





PostPosted: .NET Base Class Library, Constructor calls for other Constructor? Top

My class have 2 C'tors:

MyClass(int x)

{actionA();

actionB(x);}

and

MyClass (int x,boolean b)

{ actionA();

actionB(x);

actionC(b);}

I want to call from the second C'tor to the first one:like

MyClass (int x,boolean b)

{ MyClass(x);

actionC(b);}

But compiler doesn't agree to get it. How should I write it Thank you.




.NET Development20  
 
 
Geert Verhoeven





PostPosted: .NET Base Class Library, Constructor calls for other Constructor? Top

Hi,

Here is an example of the class how it should be:

public
class MyClass
{
   public MyClass(int
x)
   {
      ActionA();
      ActionB(x);
   }

   public MyClass(int x, bool b) : this
(x)  // The this part, calls the first constructor
   {
      ActionC(b);
   }

   private void ActionA() {}
   private void ActionB(int
x) { }
   private void ActionC(bool
b) { }
}

Greetz,

Geert

 

Geert Verhoeven
Ausy Belgium

My Personal Blog



 
 
Sean Hederman





PostPosted: .NET Base Class Library, Constructor calls for other Constructor? Top

MyClass (int x, bool b) : this(x)

{

actionC(b);

}