Answered Please critique my method

WB1975

Well-known member
Joined
Apr 3, 2020
Messages
87
Programming Experience
Beginner
This method asks for a string between min and max characters

and then asks for confirmation on entering a valid string

please let me know if it can be done more efficiently etc etc
C#:
public static string GetLimitedStringWithConfirmation(string prompt, int min, int max)
{
    do
    {
        Console.WriteLine(prompt);

        var userString = Console.ReadLine();

        if (userString.Length >= min && userString.Length <= max)
        {
            Console.Write($"You entered {userString} Do you accept [Y/N]: ");

            char ch = Console.ReadKey(true).KeyChar;
            ch = char.ToUpper(ch);
            switch (ch)
            {
                case 'Y':
                    return userString;
                case 'N':
                    Console.WriteLine("\nThe entry was rejected...");
                    break;
            }
        }
    } while (true);
}
 
Last edited by a moderator:
I'm sorry...what does "begin to tire passing the same prompt to these methods" actually mean? just calling them?
 
I'm sorry...what does "begin to tire passing the same prompt to these methods" actually mean? just calling them?
Yea, like if you start to find it tedious calling these functions with the "prompt" parameter in your program. But I'm now realizing that my understanding what "prompt" meant in your code is not what you mean by it... So disregard my comment there.
 
Back
Top Bottom