-1

I'm new to C# and have set myself a mini project of creating a To Do List type application where there's a set menu and you choose an option from the menu etc etc.

I'm having trouble with calling the methods in the main class. Here is my code:

class ToDo
{
    public ToDo()
    {
        actions = new List<string>();
    }

    public static int Menu()
    {
        Console.WriteLine("Welcome to your To Do List!\n");
        Console.WriteLine();
        Console.WriteLine("\n1. View the current list?");
        Console.WriteLine("\n2. Add to list?");
        Console.WriteLine("\n3. Delete from the list?");
        Console.WriteLine("\n4. Clear the list?");
        Console.WriteLine("\n5. Exit \n");
        Console.Write("\nWhat Would you like to do?: ");

        var selection = Console.ReadLine();
        return Convert.ToInt32(selection);

    }

    public void AddToList()
    {

            Console.WriteLine("What would you like to add to the list?");
            string userInput = Console.ReadLine();

            actions.Add(userInput);

    }

and my main method:

class Program
{
    static void Main(string[] args)

    //Keep displaying menu till user chooses option 5. Exit
    {
        int userInput = 0;
        do
        {
            userInput = ToDo.Menu();
        } while (userInput != 5);

        //If input = 2, call the Add to List method
        if (userInput == 2)
        {
            ToDo.AddToList();
        }

    }
}

The problem im having is that in the main method, its not calling the "ToDo.AddToList();".

Could anyone help? Or if i'm approaching this completely wrong, any advice would be appreciated!

Thanks

Adam
  • 7
  • 1
  • 7
  • In `Main`, create an instance of `ToDo` and store it in a variable, f.e. `ToDo todo = new Todo();`. Now you can use `todo.AddToList();` – Tim Schmelter Mar 21 '18 at 14:00
  • 5
    Step through your logic. Your loop continues while the input is not equal to 5, your `if` statement is outside of that loop and it checks if input is equal to 2. How could it ever be equal to 2 at that point? – maccettura Mar 21 '18 at 14:01
  • 1
    You use static method when its logic does not impact the state of the object. Static methods called directly by ClassName. While non static members of the class are accessible only via instance of the class – Chetan Mar 21 '18 at 14:02
  • How does this even compile? `AddToList` isn't static. The main issue with this is what @maccettura points out. The block relating to `AddToList` is never reached. – Lee Mar 21 '18 at 14:04
  • @Lee It doesn't compile – Camilo Terevinto Mar 21 '18 at 14:05
  • Then how would you know it doesn't work as expected o_O – Lee Mar 21 '18 at 14:05
  • 1
    Yes I know, how would the OP know this doesn't work if it doesn't compile! – Lee Mar 21 '18 at 14:07
  • I must be doing something wrong lol. Its my first C# program so i'm just trying to figure everything out! @Lee - i was getting an error because of the "ToDo.AddToList();" in the main method, hence my question. If i was to delete this, it would compile and show the menu i created. – Adam Mar 21 '18 at 14:07
  • There's also a problem that "actions" isn't a global variable. "public void AddToList()" should show error – MaxB Mar 21 '18 at 14:08
  • @MaxB - how would i fix that? – Adam Mar 21 '18 at 14:10
  • Honestly, there're just too many problems with this code. It looks like you wrote something, then deleted it, then wrote many things again etc. I suggest you write the Main function from scratch. And there's no point in using static methods here, but if you wish, just make AddToList static too and it will be fine. Regarding "actions" not being global variable, the point is you don't really have a property of "actions"... Simply a word "actions" in 2 places. I wonder if your IDE marks you errors with red line – MaxB Mar 21 '18 at 14:24
  • Thanks. I'll start again lol. I use VisualStudio so it does mark errors but actions isn't underlined just now. Strange. I'll start from scratch.. – Adam Mar 21 '18 at 14:29
  • @Adam Ah I see, intellisense was complaining. If you hover over it it should tell you that you need to instantiate the class first. – Lee Mar 21 '18 at 16:40

1 Answers1

1
public void AddToList()

missing the static keyword based on your implementation of your main method. If the method should not be static create a object instance of your ToDo class first. Further, you need to add the user choice check inside your loop. Otherwise the logic / implementation won't be hit. This would look like following (not tested):

 public enum Choices
 {
    Default, // 0
    ViewCurrentList, // 1
    AddToList, // 2
    DeleteFromList, // 3
    ClearList, // 4
    Exit // 5
 }

static void Main(string[] args)
{
    int userInput = 0;
    var toDo = new ToDo();

    do
    {
        userInput = ToDo.Menu();
        //If input = 2, call the Add to List method
        if (userInput == (int)Choices.AddToList)
        {
            toDo.AddToList();
        }


    } while (userInput != (int)Choices.Exit);   
 }

Further: Rather then comparing and working with ints I would suggest using an Enum at all places to remove and avoid "magic numbers"

  • 1
    Your answer does not solve the entire problem. Take a look at the code OP posted again.. – maccettura Mar 21 '18 at 14:05
  • 1
    I wouldn't create `toDo` there. Because then the next question of OP will be why all actions are always empty in `ToDo` when he executes this code in a loop. Instead use the same instance of `ToDo` and store it in a variable which is declared before the loop. – Tim Schmelter Mar 21 '18 at 14:05
  • @maccettura you are right. Thats not the answer of the question. Saw things nobody asked bout' – Daniel Rafael Wosch Mar 21 '18 at 14:10