Juliana Liman
Juliana Liman

Reputation: 9

"There is no argument given that corresponds to the required formal parameter" on constructor

Form 1 is the Main Form, while in Form 2, there is an input to be used in Form 1.

Form 1:

public partial class Form1 : Form
{
    public Form1(string size)
    {
        InitializeComponent();
        lblsize.Text = size;

    }
}

Form 2:

public void btnok_Click(object sender, EventArgs e)
    {

        //string ukuran = txtsize.Text;
        Form1 frm1 = new Form1(txtsize.Text);
        this.Hide();

    }

I then get this error:

"There is no argument given that corresponds to the required formal parameter 'size' of 'Form1.Form1(string)'

Upvotes: 0

Views: 3248

Answers (1)

CodingYoshi
CodingYoshi

Reputation: 27019

In a windows form application project you will have a file named Program.cs. In there, you will find a line of code similar to this:

Application.Run(new Form1());

That is the entry point into your application. Now that you have introduced another constructor in your Form1 class, the default parameterless constructor is nowhere to be found. Therefore, you get that error.

Fix

To fix the error, you may add a parameterless constructor to your Form1 class like this:

public Form1()
{
}

That will allow the Program.cs class to construct Form1 during entry, and you will have your other constructor for your needs to pass the size to it.

Additional Explanation

In C#, if you have a class like this:

public class Foo
{ 
    // no constructor
} 

The compiler will generate one for you by default like this:

public class Foo
{ 
    public Foo() { }  
} 

But as soon as you add a constructor (which you did), then the compiler will not create the default constructor for you anymore (which clearly it did not). Hope the explanation helps.

Upvotes: 2

Related Questions