简体   繁体   English

C#“不包含带'1'参数的构造函数”

[英]C# “does not contain a constructor that takes '1' arguments”

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. 我试图从Form1到Form3传递多个文本框数据(只有2个开头)(Form2将是我工作后添加的中介)这个想法是创建几个表单,将数据传递到最后一个表单并使用标签,此刻Form3,然后Form3将一切保存到文件或数据库。 Hope that makes sense. 希望有道理。

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,它工作正常,那么我做错了什么呢?

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. 正如错误所解释的那样, Form3不包含采用单个参数的构造函数。 This is why when you remove the second argument from the constructor the error goes away. 这就是为什么当你从构造函数中删除第二个参数时,错误消失了。

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();
}

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. 编译器说Form3没有一个带有1个参数的构造函数。 It is true. 是真的。

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); 当你说new Form3(firstNameTxtBox.Text); you're passing argument to parameter string a compiler says you have to pass string b also. 你将参数传递给参数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. 作为旁注:不要将变量和类型名称命名为abForm1等。变量的目的应该通过名称本身公开。

You are not suppling the second value. 你没有提供第二个价值。 It takes 2 parameters. 它需要2个参数。

Form3 a = new Form3(firstNameTxtBox.Text,lastNametextBox.Text);

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个表格,那么Exchange可能是日期,我认为,不仅仅是将它保存在一个文件中你可以使用静态类与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 @窗体2

GlobalClass.SecondNameTxtBox="This is From Second Form";

Make firstNameLbl and lastNameLbl public Then initiate the new form like this: make firstNameLbllastNameLbl public然后像这样启动新表单:

var f3= new Form3();
f3.firstNameLbl.Text = firstNameTxtBox.Text;
f3.lastNameLbl.Text = lastNametextBox.Text;
f3.Show();

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

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