I am trying to make a todo application in c#. What I want to do is to be able to mark a Task as done somehow. Does anyone know how to do this?
This is my program class where i print out the list in a foreach:
using System;
namespace ToDo
{
class Program
{
static void Main(string[] args)
{
bool isRunning = true;
var collection = new TodoCollection();
while (isRunning)
{
var menu = new menu();
menu.Title();
menu.Options();
int choices;
choices = Convert.ToInt32(Console.ReadLine());
switch (choices)
{
case 1:
var inputNewTask = Console.ReadLine();
var task = new Task(inputNewTask, false);
collection.Add(task);
Console.Clear();
Console.ReadLine();
break;
case 2:
int counter = 0;
if (collection != null)
{
Console.WriteLine("Dagens uppgifter listas här nedanför");
foreach (Task newTask in collection)
{
counter++;
Console.WriteLine($"({counter}) [] {newTask.ShowTask()}");
}
int userInput = Convert.ToInt32(Console.ReadLine());
Task selectedTask = collection.GetTask(userInput);
selectedTask.Done();
}
break;
}
}
}
}
}
In my foreach loop there's a empty scuare brackets i wanna change to an "X" with user input.
Here is my class for my Todo collection containing my list:
using System.Collections;
using System.Collections.Generic;
namespace ToDo
{
public class TodoCollection : IEnumerable<Task>
{
//Deklarerar mina fält
List<Task> _CurrentTasks = new List<Task>();
//Funktion som låter användaren läga till nya tasks i listan
public void Add(Task NewTask)
{
_CurrentTasks.Add(NewTask);
}
public Task GetTask(int userInput)
{
return _CurrentTasks[userInput];
}
//Låter mig iterera igenomm listan _CurrentTasks
public IEnumerator<Task> GetEnumerator()
{
return ((IEnumerable<Task>)_CurrentTasks).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)_CurrentTasks).GetEnumerator();
}
}
}
``´
And I also have a class for my Tasks here:
namespace ToDo
{
public class Task
{
string _Name;
bool isDone = false;
public Task(string name, bool done)
{
this._Name = name;
this.isDone = done;
}
public string ShowTask()
{
return this._Name;
}
public bool Done()
{
isDone = true;
return isDone;
}
}
}