-3
class ToDoList { 
    public static void toDO() {

        List<string[]> addEntry = [];
        bool toDoLoop = true;

        while (toDoLoop) {
            Clear(); 
            WriteLine("\n\t\tHere is your to do list!");

            for (int i = 1; i < addEntry.Count; i++)
            {
                WriteLine($"\n\t\tTo Do {i}: {addEntry[i][0]}");
            }
            Write("\n\t\t[N]ew Entry\t[E]xit\t");

            string? userChoice = ReadLine();
            switch (userChoice)
            {
                case "n":
                case "N":
                    Write("\n\t\tAdd New Entry: ");
                    string?[] newToDo = new string[1];

                    newToDo[0] = ReadLine();

                    if (newToDo.Length == 0) { 
                        WriteLine("\n\t\tEntries can't be null!"); 
                    }

                    else { 
                        addEntry.Add(newToDo); 
                    }
                    break;

                case "e":
                case "E":
                    Clear();
                    WriteLine("\n\n\t\tClosing To Do!");
                    toDoLoop = false;
                    break;

                default:
                    Clear();
                    WriteLine("\n\t\tInvalid Entry!");
                    break;
            }
        }
    }
}

I have tried a different variants with having the add entry inside both the if and the else, but i don't get it to work. Am i thinking completely wrong or am i at least on the right path?

if (newToDo[0] == "")

if (newToDo[0] != )

if (newToDo[0] is null)

if (newToDo[0] is not null)

if (!string.IsNullOrEmpty(newToDo[0]))
2
  • 1
    Since you are apparently dealing with "entries" and "Todos" create classes that model an "Entry" and a "Todo". That should result in code much easier to read than using multiple levels of nested collections. Commented Jan 4, 2024 at 14:59
  • What is even the question here? Commented Jan 4, 2024 at 16:05

1 Answer 1

2

Arrays always have a fixed length. Anything that changes this is actually a new array. So when you have this line:

string?[] newToDo = new string[1];

we can know that newToDo.Length will always be 1, whether or not you've ever set a value for any of the items, unless you assign a whole new array to the reference. And therefore this check:

if (newToDo.Length == 0) { 

will never be true. We already know we set it to an array with a .Length property of 1.

So which of the alternatives should you use? In my experience, the last option is most correct:

if (!string.IsNullOrEmpty(newToDo[0]))

but sometimes this is also useful:

if (!string.IsNullOrWhiteSpace(newToDo[0]))

But one wonders why you have a list of string[] at all, instead of a list of string (no array), which would be much simpler and still seems to meet your needs here.


While I'm here, indexer ([]) properties in C# start at 0, not 1, so this code is suspect:

for (int i = 1; i < addEntry.Count; i++)

You may want something more like this:

for (int i = 0; i < addEntry.Count; i++)
{
    WriteLine($"\n\t\tTo Do {i++}: {addEntry[i][0]}");
}

And then also make a corresponding -1 adjustment when reading an input of which line number to use.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.