简体   繁体   English

具有多个非默认构造函数的C#Winform

[英]C# winform with multiple non-default constructors

I need a winform with 2 non-default constructors. 我需要一个带有2个非默认构造函数的winform。 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: 请说明如何使用2个非默认构造函数:

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

Thanks to Caius Jard for the answer. 感谢Caius Jard的回答。 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. 根据主窗体中的用户输入,我必须调用2个构造函数中的一个。 If the user inputs numberOfRowsOfControls, numberOfColumnsOfControls, I have to call the second constructor, otherwise the first one. 如果用户输入numberOfRowsOfControls,numberOfColumnsOfControls,则必须调用第二个构造函数,否则调用第一个构造函数。

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. 它可以处理为this(..)或base(..)调用提供的签名。 Put some parameters in the this/base calls to define which constructor to call 在this / base调用中放置一些参数,以定义要调用的构造函数

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. 如果未指定哪个基本构造函数,则编译器将为您插入对base()的调用。 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. 根据主窗体中的用户输入,我必须调用2个构造函数中的一个。 If the user inputs numberOfRowsOfControls, numberOfColumnsOfControls, I have to call the second constructor, otherwise the first one. 如果用户输入numberOfRowsOfControls,numberOfColumnsOfControls,则必须调用第二个构造函数,否则调用第一个构造函数。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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