项目不包含采用 0 个参数的构造函数 [英] Item does not contain a constructor that takes 0 arguments
问题描述
这段代码的新 Form1() 部分(这段代码在我的 Main.cs 中)不断给我这个错误 - HomeInventory2.Form1 不包含一个采用 0 个参数的构造函数.
The new Form1() part of this code (this code is in my Main.cs) keeps giving me this error - HomeInventory2.Form1 does not contain a constructor that takes 0 arguments.
private void cDsToolStripMenuItem_Click(object sender, EventArgs e)
{
var form = new Form1();
// show the form
form.Show();
}
Form1代码如下
namespace HomeInventory2
{
public partial class Form1 : Form
{
public Form1(IEnumerable<string> prepopulated)
{
InitializeComponent();
IEnumerable<String> lines = prepopulated;
textBoxAmount.Text = lines.ElementAtOrDefault(0);
textBoxCategories.Text = lines.ElementAtOrDefault(1);
textBoxProperties.Text = lines.ElementAtOrDefault(2);
textBoxValue.Text = lines.ElementAtOrDefault(3);
}
private void label1_Click(object sender, EventArgs e)
{
}
private void submitButton_Click(object sender, EventArgs e)
{
CreateInventory create = new CreateInventory();
create.ItemAmount = textBoxAmount.Text;
create.ItemCategory = textBoxCategories.Text;
create.ItemProperties = textBoxValue.Text;
create.ItemValue = textBoxValue.Text;
InventoryMngr invtryMngr = new InventoryMngr();
invtryMngr.Create(create);
}
}
}
我尝试在其中放置一个空白的构造函数 - 但是当然,当调用该按钮或菜单项时,它只会显示一个空白屏幕.
I tried just putting a blank constructor in there - but then of course when that button or menu item is called it just brings up a blank screen.
推荐答案
.net 中的所有类都有不带任何参数的默认构造函数.当您实现自己的构造函数时,框架不会为您的类生成那个空的构造函数.同样的事情在这里发生.在您的 cDsToolStripMenuItem_Click
方法中,您需要传递 IEnumerable
参数.
All classes in .net have default constructors which don't take any arguments. When you are implementing your own constructor then the framework doesn't generate that empty constructor for your class. Same thing is happening here. in you cDsToolStripMenuItem_Click
method you need to pass IEnumerable<string>
parameter.
这篇关于项目不包含采用 0 个参数的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!