C#“不包含带有‘1’参数的构造函数”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19620852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# “does not contain a constructor that takes '1' arguments”
提问by user2925526
I have read through some of the posts on this site relating to this error but I still can't work out how to do this - I'm quite new to C#.
我已经阅读了本网站上与此错误相关的一些帖子,但我仍然不知道如何执行此操作 - 我对 C# 还是很陌生。
I am trying to pass multiple text box data (only 2 to start with) from Form1 to Form3 (Form2 will be an intermediary added after I get this working) The idea being to create several forms which pass data to the last form and display using labels, Form3 at the moment, and then Form3 will save everything to a file or database. Hope that makes sense.
我正在尝试将多个文本框数据(只有 2 个开始)从 Form1 传递到 Form3(Form2 将是我开始工作后添加的中介)这个想法是创建几个将数据传递给最后一个表单并使用显示的表单标签,此时 Form3,然后 Form3 将所有内容保存到文件或数据库中。希望这是有道理的。
So, here's a couple of snippets from my code:
所以,这是我的代码中的几个片段:
On Form1 I have:
在 Form1 上,我有:
public Form1()
{
InitializeComponent();
}
private void nextBtn_Click(object sender, EventArgs e)
{
Form3 a = new Form3(firstNameTxtBox.Text);
a.Show();
Form3 b = new Form3(lastNametextBox.Text);
b.Show();
this.Hide();
}
On Form3 I have:
在 Form3 上,我有:
public partial class Form3 : Form
{
public Form3(string a, string b)
{
InitializeComponent();
firstNameLbl.Text = a;
lastNameLbl.Text = b;
}
}
Now, if I take out string b, it works fine so what am I doing wrong please?
现在,如果我取出字符串 b,它可以正常工作,所以我做错了什么?
回答by Sam Leach
Here
这里
Form3 a = new Form3(firstNameTxtBox.Text);
you are calling the Form3
constructor with one argument.
您正在Form3
使用一个参数调用构造函数。
As the error explains, Form3
does not contain a constructor that takes a single argument. This is why when you remove the second argument from the constructor the error goes away.
正如错误所解释的,Form3
不包含采用单个参数的构造函数。这就是为什么当您从构造函数中删除第二个参数时,错误就会消失。
You have two options:
您有两个选择:
1) Remove the second constructor argument.
1) 删除第二个构造函数参数。
public Form3(string a)
{
InitializeComponent();
firstNameLbl.Text = a;
}
2) Add the second argument to all the places where you call the Form3
constructor.
2) 在所有调用Form3
构造函数的地方添加第二个参数。
If you need the second constructor argument I suggest option 2.
如果您需要第二个构造函数参数,我建议使用选项 2。
For example:
例如:
Form3 a = new Form3(firstNameTxtBox.Text, lastNametextBox.Text);
Your final Form1 code would look something like:
您最终的 Form1 代码如下所示:
public Form1()
{
InitializeComponent();
}
private void nextBtn_Click(object sender, EventArgs e)
{
Form3 a = new Form3(firstNameTxtBox.Text, lastNametextBox.Text);
a.Show();
this.Hide();
}
回答by Sriram Sakthivel
I think you mean this
我想你是这个意思
Form3 a = new Form3(firstNameTxtBox.Text, lastNametextBox.Text);
a.Show();
Compiler says Form3
doeesn't have a contructor with 1 argument. It is true.
编译器说Form3
没有带 1 个参数的构造函数。是真的。
public Form3(string a, string b)
This takes two parameters. So you'll have to pass two arguments.
这需要两个参数。所以你必须传递两个参数。
When you say new Form3(firstNameTxtBox.Text);
you're passing argument to parameter string a
compiler says you have to pass string b
also.
当你说new Form3(firstNameTxtBox.Text);
你将参数传递给参数时,string a
编译器说你也必须传递string b
。
As a side note: Don't name variables and types names like a
, b
, Form1
etc. Purpose of the variable should be exposed by name itself.
作为一个侧面说明:不要将其命名变量和类型的名称,如a
,b
,Form1
等变量的目的应该由名称本身暴露出来。
回答by OneFineDay
You are not suppling the second value. It takes 2 parameters.
您没有提供第二个值。它需要2个参数。
Form3 a = new Form3(firstNameTxtBox.Text,lastNametextBox.Text);
回答by internals-in
as you said if you have N forms then the date Exchange may be , i think ,More than saving it in a file u can use static class with get/set something like
正如你所说,如果你有 N 个表单,那么日期交换可能是,我认为,不仅仅是将它保存在一个文件中,你可以使用静态类和 get/set 类似
Lets have a new Class GlobalClass
让我们有一个新的类 GlobalClass
public static class GlobalClass
{
public static string firstNameTxtBox
{ set; get; }
public static string SecondNameTxtBox
{ set; get; }
}
and u can set from any form (Namespace should b be noted)
并且您可以从任何形式进行设置(应注意命名空间)
@Form1
@Form1
GlobalClass.firstNameTxtBox="This is From 1stForm";
@Form2
@Form2
GlobalClass.SecondNameTxtBox="This is From Second Form";
回答by Niels Schmidt
Make firstNameLbl
and lastNameLbl
public
Then initiate the new form like this:
MakefirstNameLbl
和lastNameLbl
public 然后像这样启动新的表单:
var f3= new Form3();
f3.firstNameLbl.Text = firstNameTxtBox.Text;
f3.lastNameLbl.Text = lastNametextBox.Text;
f3.Show();