lundi 23 mars 2015

How to add items to a list in C# using a class containing public variables

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


Aucun commentaire:

Enregistrer un commentaire