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

前端 未结 5 1021
深忆病人
深忆病人 2020-12-11 18:59

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 mult

相关标签:
5条回答
  • 2020-12-11 19:29

    You are not suppling the second value. It takes 2 parameters.

    Form3 a = new Form3(firstNameTxtBox.Text,lastNametextBox.Text);
    
    0 讨论(0)
  • 2020-12-11 19:35

    I think you mean this

    Form3 a = new Form3(firstNameTxtBox.Text, lastNametextBox.Text);
    a.Show();
    

    Compiler says Form3 doeesn't have a contructor with 1 argument. It is true.

    public Form3(string a, string b)
    

    This takes two parameters. So you'll have to pass two arguments.

    When you say new Form3(firstNameTxtBox.Text); you're passing argument to parameter string a compiler says you have to pass string b also.

    As a side note: Don't name variables and types names like a, b, Form1 etc. Purpose of the variable should be exposed by name itself.

    0 讨论(0)
  • 2020-12-11 19:43

    Make firstNameLbl and lastNameLbl public Then initiate the new form like this:

    var f3= new Form3();
    f3.firstNameLbl.Text = firstNameTxtBox.Text;
    f3.lastNameLbl.Text = lastNametextBox.Text;
    f3.Show();
    
    0 讨论(0)
  • 2020-12-11 19:45

    Here

    Form3 a = new Form3(firstNameTxtBox.Text);
    

    you are calling the Form3 constructor with one argument.

    As the error explains, Form3 does not contain a constructor that takes a single argument. This is why when you remove the second argument from the constructor the error goes away.

    You have two options:

    1) Remove the second constructor argument.

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

    2) Add the second argument to all the places where you call the Form3 constructor.

    If you need the second constructor argument I suggest option 2.

    For example:

    Form3 a = new Form3(firstNameTxtBox.Text, lastNametextBox.Text);
    

    Your final Form1 code would look something like:

    public Form1()
    {
        InitializeComponent();
    }
    
    private void nextBtn_Click(object sender, EventArgs e)
    {
        Form3 a = new Form3(firstNameTxtBox.Text, lastNametextBox.Text);
        a.Show();
    
        this.Hide();
    }
    
    0 讨论(0)
  • 2020-12-11 19:47

    as you said if you have N forms then the date Exchange may be , i think ,More than saving it in a file u can use static class with get/set something like

    Lets have a new Class GlobalClass

      public static class GlobalClass
        {
        public static string firstNameTxtBox
        { set; get; }
    
    
        public static string SecondNameTxtBox
        { set; get; }
        }
    

    and u can set from any form (Namespace should b be noted)

    @Form1

    GlobalClass.firstNameTxtBox="This is From 1stForm";
    

    @Form2

    GlobalClass.SecondNameTxtBox="This is From Second Form";
    
    0 讨论(0)
提交回复
热议问题