App Interfaces - Todos array


I have a question related to point no 13 in project called “App Interfaces”. One of the additional points is:

" If you add more than five to-dos to the TodoList , it throws an error! Extend the Add() method so that it doesn’t add any more than five items to the Todos array."
I added validation to the Add method so that it is not possible to add more than five items:

    public void Add(string todo)
      if(Todos.Length < 5)
        Todos[nextOpenIndex] = todo;

And after that I’m calling this function 7 times with these arguments:

      tdl.Add("Invite friends");
      tdl.Add("Buy decorations");
      tdl.Add("Invite friends");
      tdl.Add("Task 6");
      tdl.Add("Task 7");

However, after calling this function, an empty list is displayed, containing no items that should be in the list as if the list already contained the maximum number of items, but it was declared as an empty list.

Todos = new string[5];


What should I do to ensure that the method displays the added items correctly?

Thank you for respond, if needed, I can put the whole application code here.

Ok, I figured it out myself

“Extending TodoList.Add(): To extend the Add() method, use an if-else statement that checks if the Todos.Length is less than 5.”

Todos.Length should be replaced by “nextOpenIndex”, because declaring the list in this way: Todos = new string[5]; the if condition I mentioned before will always evaluate to false and no values ​​will be added to the list.

1 Like