Emil Järudd
Emil Järudd

Reputation: 1

How do I clear this list/array? Forms

I am making a To Do program in windows forms where I want to save events, what day/hour they happen and the priority.

I am filling a listbox with information, then I want to start over and have a clean slate. The listbox looks cleared but once another input is made all the old ones show up as well. I think this is because I havent cleared the list/array.

I've tried using Array.Clear(), but I dont know whether to make a new method for it or put it in my InitalizeGUI(). I also don't know if I am clearing a list or an array as it is a list to start with but is converted to a string array.

class TaskManager
    {
        private List<Task> todo;

        public TaskManager()
        {
            todo = new List<Task>();
        }

        public Task GetItem (int index)
        {
            if (!CheckIndex(index))
                return null;

            return todo[index];
        }

        public int Count
        {
            get { return todo.Count; }
        }

        public bool AddItem (Task itemIn)
        {
            bool ok = false;
            if (itemIn != null)
            {
                todo.Add(itemIn);
                ok = true;
            }
            return ok;
        }

        private bool CheckIndex (int index)
        {
            return(index >= 0) && (index < todo.Count);
        }

        public string [] ListToStringArray()
        {
            string[] taskArray = new string[Count];
            for (int i = 0; i < Count; i++) taskArray[i] = todo[i].ToString();
            return taskArray;
        }
    }
}

This is my Taskmanager class. Do i make a method to clear this list/array, and should it be made in TaskManager or Mainform?

I've tried all the ways I could from googling online but I can't figure it out.

Hopefully someone knows how to help!

Best regards

Upvotes: 0

Views: 67

Answers (1)

GeorgeF12
GeorgeF12

Reputation: 11

Add a new public method in TaskManager class that clears the 'todo' private variable.:

public void ClearList()
{
todo.Clear();
}

Call it when you need your list empty. Eg.: At the InitGui() method.

Upvotes: 1

Related Questions