C#“不包含带有'1'参数的构造函数” [英] C# “does not contain a constructor that takes '1' arguments”

查看:605
本文介绍了C#“不包含带有'1'参数的构造函数”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了本网站上与该错误有关的一些帖子,但是我仍然无法解决该问题-我对C#还是很陌生。

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#.

我正在尝试将多个文本框数据(从2开始)从Form1传递到Form3(在我完成此工作后,Form2将成为中介)想法是创建几个将数据传递到最后一个表单的表单表单并使用标签(当前是Form3)进行显示,然后Form3将所有内容保存到文件或数据库中。

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.

所以,这是我代码中的几段代码:

So, here's a couple of snippets from my code:

在Form1上,我有:

On Form1 I have:

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

在Form3上,我有:

On Form3 I have:

    public partial class Form3 : Form
    {
        public Form3(string a, string b)
        {
           InitializeComponent();
           firstNameLbl.Text = a;
           lastNameLbl.Text = b;
        }
    }

现在,如果我取出字符串b,就可以了很好,那么我在做什么错了?

Now, if I take out string b, it works fine so what am I doing wrong please?

推荐答案

在这里

Form3 a = new Form3(firstNameTxtBox.Text);

您正在调用 Form3 构造函数

如错误所解释, 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.

您有两个选择:

1)删除第二个构造函数参数。

1) Remove the second constructor argument.

public Form3(string a)
{
    InitializeComponent();
    firstNameLbl.Text = a;
}

2)在所有调用<$的地方添加第二个参数c $ c> Form3 构造函数。

2) Add the second argument to all the places where you call the Form3 constructor.

如果需要第二个构造函数参数,建议使用选项2。

If you need the second constructor argument I suggest option 2.

例如:

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

您最终的Form1代码如下所示:

Your final Form1 code would look something like:

public Form1()
{
    InitializeComponent();
}

private void nextBtn_Click(object sender, EventArgs e)
{
    Form3 a = new Form3(firstNameTxtBox.Text, lastNametextBox.Text);
    a.Show();

    this.Hide();
}

这篇关于C#“不包含带有'1'参数的构造函数”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆