1

I'm working on a project that use different methods to get input from the user and I want to add the user input to a list.

My code looks like this:

public string GetValidName()
    {
        Console.WriteLine("Let's get you registered!");
        Console.WriteLine("Enter your full name: ");

        string input = Console.ReadLine();
        bool validString = false;
        while (validString != true)
        {
            if (String.IsNullOrEmpty(input))
            {
                Console.WriteLine("Not a valid input.");
                validString = false;
            }
            else
            {
                validString = true;
            }
        }

        return input;
    }

The list exists in the same class and is declared public List<string> students = new();

I have tried return students.Add(input); but that is not possible and gives the error

Cannot implicitly convert type 'void' to 'string'

Edit This method is called through this method and has been edited with changes so it now looks like this;

    public void Register()
    {
        students.Add(GetValidName());
    }

To print all items in the list I use this piece of code:

    public void AttendenceList()
    {
        Students list = new();

        Console.WriteLine("All students attending class today:\r\n");
        foreach (var item in list.students)
        {
                Console.WriteLine(item);
        }
    }

When the foreach loop runs, it only prints the Console.WriteLine though, and not any items in the list.

DarkBee
  • 16,592
  • 6
  • 46
  • 58
Shubban478
  • 31
  • 7
  • What exactly do you want to return from your method? The error message is because `List – Lasse V. Karlsen Nov 10 '21 at 10:51
  • 1
    btw. If you use `string.IsNullOrWhiteSpace` and the user enters only spaces it will return true as well. [link](https://stackoverflow.com/questions/6976597/string-isnulloremptystring-vs-string-isnullorwhitespacestring) – Jochem Van Hespen Nov 10 '21 at 10:57
  • @LasseV.Karlsen I want to return the name that the user types in to keep track of which users are in school. So basically I want the users input in the list and I also have a foreach loop to print all values inside the list. – Shubban478 Nov 10 '21 at 11:07
  • 1
    @JochemVanHespen Thank you, I did not think of this and will make changes in my code! – Shubban478 Nov 10 '21 at 11:07

1 Answers1

1

if you do Students list = new(); you reintialize your list dont do that.

Like your list students is defined in the class, you could write:

public void AttendenceList()
{
    Console.WriteLine("All students attending class today:\r\n");
    foreach (var item in students)
    {
            Console.WriteLine(item);
    }
}
Frenchy
  • 16,386
  • 3
  • 16
  • 39
  • So when the method is called, I call it using `students.Add(GetValidName))` ? – Shubban478 Nov 10 '21 at 11:08
  • no you write students.Add(GetValidName)) and not students.Add(GetValidName()) – Frenchy Nov 10 '21 at 11:11
  • I'm not sure I understand what you mean. I get another error saying "Cannot convert from 'method group' to 'string' – Shubban478 Nov 10 '21 at 11:16
  • you do a typo in your precedent comment.. you write GetValidName and not GetValidName()....so if its not the problem i suggest you to show your complete code with the call of the function and the definition of your list – Frenchy Nov 10 '21 at 11:19
  • i noticed, I have updated my original post with more information. The code gives no errors when typing it like you said in my `Register()`but won't show up when trying to print the items in list. – Shubban478 Nov 10 '21 at 11:34
  • you create a new list! dont do that – Frenchy Nov 10 '21 at 12:02
  • yeah thanks for your help! I removed that code that created a new list and changed the placement of my list and it now works as I intended! – Shubban478 Nov 10 '21 at 12:29