简体   繁体   中英

C# winform with multiple non-default constructors

I need a winform with 2 non-default constructors. I found this post that explains how to call a default constructor from within the non-default constructor:

public FrmUpload(Dictionary<string, string> ft) : base()

call your own parameterless contructor:

public FrmUpload(Dictionary<string, string> ft) : this()

Please explain how to proceed with 2 non-default constructors:

public Form1(int numberOfControls, bool favorColumns)
public Form1(int numberOfControls, int numberOfRowsOfControls, int 
numberOfColumnsOfControls)

Thanks to Caius Jard for the answer. I learned from it. However, it turns out that I did not explain my needs well. I have to call one or the other of the 2 constructors, according to the user input in the main form. If the user inputs numberOfRowsOfControls, numberOfColumnsOfControls, I have to call the second constructor, otherwise the first one.

If you wrote one or more constructors (including parameterless ones) the compiler will not insert a parameterless constructor for you. Every class has to have a constructor; if you specify no constructor at all the compiler silently invisibly creates one for you (it doesn't modify your code, it inserts it during compilation) that does nothing other than call the base parameterless constructor but the presence of any constructor provided by you disabled this behaviour

Any of your constructors can call any other constructor in the current class or the parent. To clearly define which one to call is just like calling a method; it works off the supplied signature to the this(..) or the base(..) call. Put some parameters in the this/base calls to define which constructor to call

class Foo{

  Foo(string s, int i) { }

  Foo(DateTime d, int i) : this(d.ToString(), i) { } //call above constructor

}

Constructors are called in a chain, right the way back up the inheritance hierarchy all the way to object. Every constructor that doesn't call a constructor in the current class must hence call a base constructor. If you don't specify which base constructor the compiler inserts a call to base() for you. If your parent class has no parameterless constructor (because you provided a constructor that had parameters, disabling the compiler from providing one) and your child class doesn't specify which parent class constructor to call, you'll get a compiler error. To resolve it the child must specify which parent constructor to call and what its parameters shall be:

class Bar:Foo{

  //this class has no constructors
  //the compiler will try to insert Bar():base(){} for you
  //this will fail because "Foo does not contain a constructor that takes 0 arguments"
}

class Bar:Foo{

  //this class has a constructor that looks like one in Foo
  //it doesn't specify which base constructor to call
  //the compiler will try to insert base() for you
  //this will also fail because "Foo does not contain a constructor that takes 0 arguments"
  Bar(string s, int i){}
}

class Bar:Foo{

  //this class has a constructor that looks like one in Foo
  //it specifies which base constructor to call
  //the compiler will not guess that it should call base(s, i)
  Bar(string s, int i): base(s, i){}
}

However, it turns out that I did not explain my needs well. I have to call one or the other of the 2 constructors, according to the user input in the main form. If the user inputs numberOfRowsOfControls, numberOfColumnsOfControls, I have to call the second constructor, otherwise the first one.

Something like this perhaps:

//if the user chose a value for num of columns
if(_numColsNumericUpDown.Value > 0)
  new Form1(1, (int)_numColsNumericUpDown.Value, 1).Show(); //call the int int int constructor
else
  new Form1(1, true).Show(); //call the int bool constructor

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM