Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 685
user2925526 Avatar asked Oct 27 '13 17:10

user2925526


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


2 Answers

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();
}
like image 153
Sam Leach Avatar answered Sep 30 '22 01:09

Sam Leach


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.

like image 25
Sriram Sakthivel Avatar answered Sep 29 '22 23:09

Sriram Sakthivel