?
Solved

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

Posted on 2013-10-27
8
Medium Priority
?
2,478 Views
Last Modified: 2013-10-27
Hi Guys

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?
0
Comment
Question by:Spareheadone
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 39604076
you should call  
Form3 b = new Form3(lastNametextBox.Text); 

Open in new window

with 2 parameters (not 1 like above)
your constructor expect 2 parameters:
public Form3(string a, string b)

Open in new window

0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39604080
your form3 should have 2 constructors, with 1 parameter and with 2 parameters:
public Form3(string fname)

    {
       InitializeComponent();
       firstNameLbl.Text = fname;
}

public Form3(string fname, string lname)

    {
       InitializeComponent();
       firstNameLbl.Text = fname;
       lastNameLbl.Text = lname;
}

Open in new window

0
 

Author Comment

by:Spareheadone
ID: 39604083
Well I assumed as much but where and how?

I don't understand exactly how to do that.

Sorry, was a bit quick posting a reply - didn't see the rest of your comment.

I will try it.....
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:Spareheadone
ID: 39604104
Ok.

sort of works but I am now getting 2 instances of Form3 opening. On the 1st instance I get the first name added to the firstNameLbl and on the second instance I get the last name added to firstNameLbl. On both forms lastNameLbl remains unchanged.

This is how I have changed the code:

Form1 has:
public partial class Form1 : Form
    {
        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();

Form 3 has:

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

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



        private void Form3_Load(object sender, EventArgs e)
        {



        }

        private void lastNameLbl_Click(object sender, EventArgs e)
        {

        }
    }
}
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39604109
your form3 should have 2 constructors, with 1 parameter and with 2 parameters:
Or you could simply pass an empty string as the second argument:

 Form3 a = new Form3(firstNameTxtBox.Text, string.Empty);

Open in new window


It all depends on what the business goal here is.
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 2000 total points
ID: 39604116
that is because the 1st parameter passed to the constructor always updates firstNameLbl.Text (string a).
if u want to distinguish between first name and last name when passing a single parameter u need to twick the constructor definition, or use public propertis like:
public partial class Form3 : Form
    {

public string FirstName{get{return firstNameLbl.Text;} set{firstNameLbl.Text=value;}}
public string LastName{get{return lastNameLbl.Text;} set{lastNameLbl.Text=value;}}

Open in new window

then u instance the Form 3 like this:
Form3 a = new Form3{
FirstName = firstNameTxtBox.Text;
LastName = lastNameTxtBox.Text;
}
            a.Show();

            Form3 b = new Form3(lastNametextBox.Text);
            b.Show();

Open in new window

0
 

Author Closing Comment

by:Spareheadone
ID: 39604160
Great stuff - thanks for the help.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39604188
All that work for what was accomplished in 14 characters (4 if you cheat and use a literal empty string). C'est la vie.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question