Solved

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

Posted on 2013-10-27
8
2,023 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 74

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 74

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

759 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

17 Experts available now in Live!

Get 1:1 Help Now