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.

4
  • What exactly do you want to return from your method? The error message is because List<string.Add(...) does not return a value, hence void. Perhaps you meant to first add to the list, then return input; ? Commented Nov 10, 2021 at 10:51
  • 1
    btw. If you use string.IsNullOrWhiteSpace and the user enters only spaces it will return true as well. link Commented Nov 10, 2021 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. Commented Nov 10, 2021 at 11:07
  • 1
    @JochemVanHespen Thank you, I did not think of this and will make changes in my code! Commented Nov 10, 2021 at 11:07

1 Answer 1

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);
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

So when the method is called, I call it using students.Add(GetValidName)) ?
no you write students.Add(GetValidName)) and not students.Add(GetValidName())
I'm not sure I understand what you mean. I get another error saying "Cannot convert from 'method group' to 'string'
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
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.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.