namespace To_Do_List_Application
{
public partial class Create_new_task : Form
{
public Create_new_task()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Data[] add_item = new Data[10];
add_item[1] = new Data(Title_Input);
// I get the error Data does not contain constructor that takes 1 argument
}
// Data Class on program.cs ////////////////////////////////////////////////////////////////
public class Data
{
// Menu (form)
public int Priority;
public string Title;
public string Description;
// Create new task (form)
public DateTime Calender;
public string Description_2;
public int Tick;
public Data(int priority, string title, string description, DateTime calender, string description_2, int tick)
{
// Menu (form)
Title = title;
Description = description;
// Create new task (form)
Calender = calender;
Description_2 = description_2;
Tick = tick;
Priority = priority;
}
答案 0 :(得分:0)
当你尝试创建新的Data对象时,你必须使用带有6个参数的构造函数。在你的情况下,你的代码看起来应该是这样的:
add_item[1] = new Data(5,"title", "description", new DateTime(2015, 4, 25), "desc2",3);
通过构造函数创建一个新对象:
public Data(int priority,string title,string description,DateTime calender,string description_2,int tick)
是您的构造函数,您只能使用这6个参数创建一个新的数据对象。
答案 1 :(得分:0)
您获得的错误是因为当您未提供接受单个参数的签名时,您尝试使用单个参数初始化新的Data对象:
add_item[1] = new Data(Title_Input);
将重载的构造函数添加到Data类,该类接受一个Title_Input类型的参数,您不应该再遇到该问题。
EG。
// Default constructor (This constructor is provided automatically by
// default if not explicitly defined. Including it in the class definition
// allows you to initialize the object with default parameters when creating
// a new Data object without providing any parameters.)
public Data()
{
}
// Overloaded constructor that accepts a single parameter
public Data(Title_Input input)
{
//Do something with the parameter input
...
}
// Overloaded constructor that accepts 6 arguments
public Data(int priority, string title, string description, DateTime calender, string description_2, int tick)
{
// Menu (form)
Title = title;
Description = description;
// Create new task (form)
Calender = calender;
Description_2 = description_2;
Tick = tick;
Priority = priority;
...
}