Solved

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

Posted on 2013-10-27
8
2,093 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now