1

So what I'm attempting to do is have this program ignore a user's letter case when entered. I see how to use .ToLower(); however I'm not understanding how to do this the right way.

Here's what I have now, am I close? I've read a bunch of tutorials online however they are mostly just standalone programs that convert user input to lower. Is there a way to enable this globally?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Choose_Your_Color
{
class Program
{
    enum Color
    {
        red,
        orange,
        blue,
        black,
        white,
        green,
        purple,
        yellow
    }
    static void Main(string[] args)
    {

        Color favorite;
        while (true)
        {
            Console.WriteLine("What color do you choose?");
            if (Enum.TryParse(Console.ReadLine(), out favorite))
                if (string.compare(favorite , Enum , true) == 0){
                    Continue;
                }



            {
                switch (favorite)
                {
                    case Color.red:
                        Console.WriteLine("You chose red!");
                        break;
                    case Color.orange:
                        Console.WriteLine("you chose orange!!!!!!");
                        break;
                    case Color.blue:
                        Console.WriteLine("YOU CHOSE BLUEEEE!!");
                        break;
                    case Color.black:
                        Console.WriteLine("you chose black");
                        break;
                    case Color.white:
                        Console.WriteLine(" you chose white!");
                        break;
                    case Color.green:
                        Console.WriteLine("you chose green!!!!!");
                        break;
                    case Color.purple:
                        Console.WriteLine("you chose purple!!");
                        break;
                    case Color.yellow:
                        Console.WriteLine("you chose yellow!!!");
                        break;

                }
            }

            else
            {
                Console.WriteLine("That's not a color!");
            }





        }
    }
}

}

IAbstract
  • 19,551
  • 15
  • 98
  • 146
user3554677
  • 35
  • 1
  • 6

5 Answers5

3

Just change this;

if (Enum.TryParse(Console.ReadLine(), out favorite))

to

if (Enum.TryParse(Console.ReadLine().ToLower(), out favorite))

Console.ReadLine() returns a string, this will call to lower on that value ensuring that all the input is lower cased.

Why do you have this line?

            if (string.compare(favorite , Enum , true) == 0){
                Continue;
            }

I don't think there is any reason for it. Enum.TryParse should either return false meaning the input wasn't one of the enums and you won't go into the switch statement OR favorite will be one of the enum values and you'll go into one of the cases in your switch statement.

evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115
  • Hmmm, It still returns it as it not being recognized. – user3554677 Apr 25 '14 at 20:53
  • Is this because of the while loop its in? – user3554677 Apr 25 '14 at 20:55
  • @user3554677 perhaps the input doesn't match any of the enum values? You might also want to call `Trim()` on the input as it could have whitespaces which would prevent a match. Also, one last thing I'll edit into my answer. – evanmcdonnal Apr 25 '14 at 20:56
  • I removed the second if statement. The code is exactly as the way it was minus the second if statement and the addition of "if (Enum.TryParse(Console.ReadLine().ToLower(), out favorite))" – user3554677 Apr 25 '14 at 21:02
  • @user3554677 with that being the case you code is very similar to the examples on msdn. Can you set a break point in the body of the if and see what's happening? The use of `Enum.TryParse` is correct so I'm not sure what would be causing the problem. – evanmcdonnal Apr 25 '14 at 21:05
  • Ok sorry about that. For some reason, my compiler wasn't updating the source code for the build when I went to compile it. VS 2013 is a bit buggy =P – user3554677 Apr 25 '14 at 21:10
  • Yes, works flawlessly. Thank you for the help! Is there a guide of reference manual that contains information about functions and their accepted arguments? I'm new to this so i didn't even realize you can just tack on arguments to things like that. – user3554677 Apr 25 '14 at 21:12
  • @user3554677 everything in .NET is pretty well documented by Microsoft. If you search "enum C# .net msdn" the top result will be the docs for the type. You can also search the method names to get directly to say `TryParse` here's a link for example http://msdn.microsoft.com/en-us/library/System.Enum(v=vs.110).aspx – evanmcdonnal Apr 25 '14 at 21:14
  • @user3554677 ps if my answer worked for you accept it :p – evanmcdonnal Apr 25 '14 at 21:24
3

You just need to do:

if (Enum.TryParse(Console.ReadLine().ToLower(), out favorite))

You don't need that nested if, you can just remove it.Also you have to add a break to the end of your if block, so it will break your loop after user type a valid value otherwise the loop will never end.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
3

Enum.TryParse accepts a parameter to ignore case:

Enum.TryParse(Console.ReadLine(), true, out favorite);
Big McLargeHuge
  • 14,841
  • 10
  • 80
  • 108
2

Note that this overload of TryParse allows you to ignore the case of the input string, so you could just write this as:

if (Enum.TryParse(Console.ReadLine(), true, out favorite))
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
1

String.ToLower() will return the string value as lowercase.

if (Enum.TryParse(Console.ReadLine().ToLower(), out favorite))
Crono
  • 10,211
  • 6
  • 43
  • 75