问题描述
公共局部类Fom1:表格
{
个人= new Personal();
SqlConnection conn =新的SqlConnection();
公共Fom1(字符串personName)
{
InitializeComponent();
但是当使用这个
Fom1 fl =新的Fom1();
fl.Close();
我收到错误消息
Godswill.Fom1''不包含带有``0''参数的构造函数
请关闭表单
public partial class Fom1 : Form
{
Personal person = new Personal();
SqlConnection conn = new SqlConnection();
public Fom1(string personName)
{
InitializeComponent();
but when use this
Fom1 fl = new Fom1();
fl.Close();
i get the error message
Godswill.Fom1'' does not contain a constructor that takes ''0''arguments
please how do i close the form
推荐答案
Fom1 fl = new Fom1("");
fl.Close();
您将没有问题.
您可以为类定义多个构造函数:假设我以这种方式定义了一个名为"Form2"的窗体:
You would not have a problem.
You can define multiple constructors for classes: suppose I define a Form, named ''Form2, this way:
public partial class Form2 : Form
{
private string PersonName;
private string EmployeeId;
public Form2()
{
InitializeComponent();
}
public Form2(string personName): base()
{
PersonName = personName;
}
public Form2(string personName, string employeeId): this(personName)
{
EmployeeId = employeeId;
}
}
然后,此代码...以其他某种形式执行...将创建零,一个或两个构造函数参数方面的Form2的所有三种可能变体:
Then, this code ... executed in some other Form ... will create all three possible variations of Form2 in terms of zero, one, or two, constructor arguments:
Form2 f2NOParams = new Form2();
f2NOParams.Text = "Made with no parameters in the Constructor";
Form2 f2ONEParams = new Form2("Some Person''s Name");
f2ONEParams.Text = "Made with one parameter in the Constructor";
Form2 f2TWOParams = new Form2("Another Person''s Name", "Employee XQ-45");
f2TWOParams.Text = "Made with two parameters in the Constructor";
// seeing is believing
f2NOParams.Show();
f2ONEParams.Show();
f2TWOParams.Show();
这些备用构造函数被引用作为构造函数重载".
请注意,重载"是如何在第二个示例中调用基(无构造函数参数),或者在第三个示例中使用特殊的表示法在构造函数参数列表的末尾调用具有一个构造函数参数的构造函数!
这就是为什么第三个示例不必在``employeeId''中显式分配传入参数的原因:它调用了第二个示例,而第二个示例调用了保证"InitializeComponent被调用"的基本构造函数. />
欢迎来到构造函数重载"的奇妙世界:)
These alternate constructors are referred to as "Constructor Overloads."
Note how the "overloads" invoke either the base (no constructor parameter) in the second example, or the constructor with one contstructor parameter in the third example using a special notation following the end of the constructor parameter list !
That''s why the third example does not have to explicitly assign the incoming parameter in ''employeeId: it calls the second example which does that, and the second example invokes the base constructor which guarantees that ''InitializeComponent is called.
Welcome to the wonderful world of "Constructor Overloads" :)
这篇关于运行时错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!