如何使用包含公共变量的类在C#中将项添加到列表中(How to add items to a list in C# using a class containing public variables)

系统教程 行业动态 更新时间:2024-06-14 17:01:34
如何使用包含公共变量的类在C#中将项添加到列表中(How to add items to a list in C# using a class containing public variables)

我希望你能帮助一个程序员。 基本上,我希望将富文本框(taskNameRTB)中的用户输入分配给taskName; 我的类taskStructure字符串变量,如下所示:

public partial class Form1 : Form { public class taskStructure { public string taskName; public string taskDescription; public int Priority; public string dateAndTime; } public List<taskStructure> TasksArray = new List<taskStructure>(); //Declared a list data structure

在我的第二种形式中,用户输入与任务相关的所有内容,我想在单击“创建任务”按钮后将此信息发送到列表:

private void createTaskBtn_Click(object sender, EventArgs e) { Form1 welcomeForm = new Form1(); welcomeForm.TasksArray[0].taskName = taskNameRTB.Text; welcomeForm.TasksArray[0].taskDescription = taskDescRTB.Text; }

但是,当我这样做时,我得到一个ArgumentOutOfRangeException ,我不明白为什么。 我也试过这些:

welcomeForm.TasksArray[0].Add(taskDescRTB.Text); welcomeForm.TasksArray.Insert(0, taskNameRTB.Text); welcomeForm.TasksArray.Add(taskDescRTB.Text); taskNameRTB.Text = welcomeForm.TasksArray[0].taskName;

但运行的那些会出现相同的错误ArgumentOutOfRangeException ,其中一些不起作用,例如:

welcomeForm.TasksArray[0].Add(taskDescRTB.Text);

我知道该列表尚未初始化,但如果不允许我使用用户输入初始化它,如何初始化它...

你能解决的任何亮点都会非常有用

亲切的问候,

基兰

I hope you can help out a fellow programmer. Basically, I want the user input from the Rich Text Box (taskNameRTB) to be assigned to the taskName; string variable in my class taskStructure which is in form1 shown below:

public partial class Form1 : Form { public class taskStructure { public string taskName; public string taskDescription; public int Priority; public string dateAndTime; } public List<taskStructure> TasksArray = new List<taskStructure>(); //Declared a list data structure

In my second form which is where the user enters everything related to the task, I want to send this information to the list after the 'Create Task' button has been clicked:

private void createTaskBtn_Click(object sender, EventArgs e) { Form1 welcomeForm = new Form1(); welcomeForm.TasksArray[0].taskName = taskNameRTB.Text; welcomeForm.TasksArray[0].taskDescription = taskDescRTB.Text; }

However, when I do this I get a ArgumentOutOfRangeException and I do not understand why. I have also tried these:

welcomeForm.TasksArray[0].Add(taskDescRTB.Text); welcomeForm.TasksArray.Insert(0, taskNameRTB.Text); welcomeForm.TasksArray.Add(taskDescRTB.Text); taskNameRTB.Text = welcomeForm.TasksArray[0].taskName;

But the ones that run come up with the same error ArgumentOutOfRangeException and some of them don't work, such as:

welcomeForm.TasksArray[0].Add(taskDescRTB.Text);

I'm aware that the list has not been initialized, but how can I initialize it when it doesn't allow me to initialize it with user input...

Any light you can shed on this will be really helpful

Kind Regards,

Kieran

最满意答案

您需要向列表中添加新的taskStructrue 。

welcomeForm.TasksArray.Add(new taskStructure { taskName = taskDescRTB.Text, taskDescription = taskDescRTB.Text });

但就个人而言,我会重写该类以遵循命名约定并使用属性而不是公共字段。

public class TaskStructure { public string TaskName { get; set; } public string TaskDescription { get; set; } public int Priority { get; set; } public string DateAndTime { get; set; } }

You need to add a new taskStructrue to the list.

welcomeForm.TasksArray.Add(new taskStructure { taskName = taskDescRTB.Text, taskDescription = taskDescRTB.Text });

But personally I'd rewrite that class to follow naming conventions and to use properties instead of public fields.

public class TaskStructure { public string TaskName { get; set; } public string TaskDescription { get; set; } public int Priority { get; set; } public string DateAndTime { get; set; } }

更多推荐

本文发布于:2023-04-20 18:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/bffd6bc80177ebaf69f1b30b441bd4ff.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   中将   变量   列表中   add

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!