Solved

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

Posted on 2013-10-27
8
2,266 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

752 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