Solved

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

Posted on 2013-10-27
8
2,137 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Greetings C# program 17 107
SQL Exceptions 3 46
Diagnostics with Net and Net.Sockets 2 29
C# Comparing String Arrays. 5 40
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 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