简体   繁体   中英

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

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:

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

On Form3 I have:

    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?

Here

Form3 a = new Form3(firstNameTxtBox.Text);

you are calling the Form3 constructor with one argument.

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.

You have two options:

1) Remove the second constructor argument.

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

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

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

For example:

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

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

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.

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.

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.

You are not suppling the second value. It takes 2 parameters.

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

Lets have a new Class 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

GlobalClass.firstNameTxtBox="This is From 1stForm";

@Form2

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

Make firstNameLbl and lastNameLbl public Then initiate the new form like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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