Reputation: 65
public class ToDoServices
{
List<TaskToDo> taskToDos { get; set; }
private int number;
public ToDoServices()
{
taskToDos = new();
}
public void AddToList()
{
number++;
string text = Console.ReadLine();
string dateString = Console.ReadLine();
DateTime dateTime = DateTime.Parse(dateString);
taskToDos.Add(new TaskToDo { Id = number, Title = text, TimeToDo = dateTime });
ReadWriteFile readWriteFile = new ReadWriteFile();
readWriteFile.WriteToFiles();
}
public List<TaskToDo> ShowTheList()
{
foreach (var taskToDo in taskToDos)
{
Console.WriteLine($"To Do:{taskToDo.Id}: {taskToDo.Title}:{taskToDo.TimeToDo}");
}
return taskToDos;
}
}
public class ReadWriteFile
{
string fileName = @".\ToDo.txt";
public void WriteToFiles()
{
ToDoServices toDoServices = new ToDoServices();
var taskToDos = toDoServices.ShowTheList();
using (StreamWriter writetext = new StreamWriter(fileName, true))
{
foreach (var i in taskToDos)
writetext.WriteLine(i.Id + "-" + i.Title + " " + i.TimeToDo);
}
}
}
public class TaskToDo
{
public int Id { get; set; }
public string Title { get; set; }
public DateTime TimeToDo { get; set; }
}
This project I am creating a list but I have a problem when I want to save this list in text file, when I call the list in class ReadWriteFile
, the list be empty. I know it be new empty list because I call every time for new list.
I tried many ways but I didn't success, so I hope someone can help me to solve the problem/ thanks for help
Upvotes: 1
Views: 97
Reputation: 117154
Your problem here is that your code's logic hasn't been well thought out.
If your ReadWriteFile.WriteToFiles()
method you're calling new ToDoServices()
and then attempting to write out the file, but you never call toDoServices.AddToList()
to actually populate the list with anything - so the list is empty when you try to save it.
However, the ToDoServices.AddToList()
method actually creates a new instance of ReadWriteFile
and calls readWriteFile.WriteToFiles()
- which means that if WriteToFiles()
did call AddToList()
you'd be in an infinite loop!
You need to separate out all of the logic so that each method does one thing and one thing only. the AddToList()
method should only add an item to the list. The ShowTheList()
method should only show the list.
This is closer to what you need:
void Main()
{
string fileName = @".\ToDo.txt";
ToDoServices toDoServices = new();
toDoServices.AddToList();
ReadWriteFile readWriteFile = new();
readWriteFile.WriteToFiles(fileName, toDoServices);
}
public class ToDoServices
{
private List<TaskToDo> taskToDos = new();
private int number;
public void AddToList()
{
number++;
string text = Console.ReadLine();
string dateString = Console.ReadLine();
DateTime dateTime = DateTime.Parse(dateString);
taskToDos.Add(new TaskToDo { Id = number, Title = text, TimeToDo = dateTime });
}
public void ShowTheList()
{
foreach (var taskToDo in taskToDos)
{
Console.WriteLine($"To Do:{taskToDo.Id}: {taskToDo.Title}:{taskToDo.TimeToDo}");
}
}
public IEnumerable<TaskToDo> GetTheList() => taskToDos.AsEnumerable();
}
public class ReadWriteFile
{
public void WriteToFiles(string fileName, ToDoServices toDoServices)
{
using (StreamWriter writetext = new StreamWriter(fileName, true))
foreach (var td in toDoServices.GetTheList())
writetext.WriteLine($"{td.Id}-{td.Title} {td.TimeToDo}");
}
}
public class TaskToDo
{
public int Id { get; set; }
public string Title { get; set; }
public DateTime TimeToDo { get; set; }
}
Upvotes: 1