KieranLowe
KieranLowe

Reputation: 81

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

Upvotes: 0

Views: 808

Answers (3)

matrixanomaly
matrixanomaly

Reputation: 6967

Your TaskStructure is a class, and you are putting all TaskStructure objects into a list,

public List<taskStructure> TasksArray = new List<taskStructure>(); //Declared a list data structure

Does your Form1() have a constructor that calls InitializeComponents()?

If so, you could try adding TasksArray = new List<taskStructure>() right below InitializeComponents(), because it looks like you're trying to access the list data structure that hasn't been initialized with new.

Alternatively

As another user noted, you can create a constructor class for TaskStructure like this:

public TaskStructure(RTB rtb1, rtb2, rtb3) //where RTB is the rich text box type
{
   taskName = rtb1.text;
   taskDescription = rtb2.text;
   //and so on.
}

Then you can do TaskArray.add(new TaskStruture(rtb1,rtb2,rtb3).

Thrid Edit

Just realized your TaskArray is actually a List, which in C# (and Java), you cannot access it with an index like TaskArray[0], you have to use getter and setter methods, which in this case is TaskArray.add(), and TaskArray.get(0), you're getting ArgumentOutOfRangeException because you're trying to access a List using square indexes like this --> [0]. You can actually access a list doing list1, as pointed out by another user.

Here's a good tutorial on C# lists, by DotNetPerls

Upvotes: 1

juharr
juharr

Reputation: 32296

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; }
}

Upvotes: 2

XenoPuTtSs
XenoPuTtSs

Reputation: 1284

have you tried

welcomeForm.TasksArray.Add(new taskStructure(taskDescRTB.Text));

I don't know what taskStructure is, but you need to fill TasksArray with types of it.

Upvotes: 1

Related Questions