C# case statement error cannot implicitly convert string to char

Okay so the assignment I am working on is supposed to take 7 characters letters and numbers only no symbols and turn them into a 7 digit phone number. My assignment says I need to use switch statements and have a case statement for each digit. I am just trying to get this to at least work with the first character before moving on to the rest. The conversions are based on a standard phone keypad. A, B, or C would be 2, D,E,F would be 3 and so on. This is my first time using case statements, so I am not even sure if I am doing this right. The error I am receiving is “Cannot implicitly convert type string to char”. My questions is am I even doing this case statement right? If so how to I get this to compile properly and If not how to I make is so that when someone enters an A, B, or C it converts to a “2”?

static void Main(string[] args)
    {
        Console.WriteLine("****Phone Dialing Program****n");

        char char1 = default(char);
        char char2 = default(char);
        char char3 = default(char);
        char char4 = default(char);
        char char5 = default(char);
        char char6 = default(char);
        char char7 = default(char);


        GetInput(ref char1, char2, char3, char4, char5, char6, char7);
        ProcessInput();
        //ToDigit(ref char1, char2, char3, char4, char5, char6, char7);
        ShowResults();


        Console.Read();
    }

    static void GetInput(ref char char1, char char2, char char3, char char4, char char5, char char6, char char7)
    {
        Console.WriteLine("Enter your first character:");
        char1 = Console.ReadKey().KeyChar;

        Console.WriteLine("nEnter your second character:");
        char2 = Console.ReadKey().KeyChar;

        Console.WriteLine("nEnter your third character:");
        char3 = Console.ReadKey().KeyChar;

        Console.WriteLine("nEnter your fourth character:");
        char4 = Console.ReadKey().KeyChar;

        Console.WriteLine("nEnter your fifth character:");
        char5 = Console.ReadKey().KeyChar;

        Console.WriteLine("nEnter your sixth character:");
        char6 = Console.ReadKey().KeyChar;

        Console.WriteLine("nEnter your seventh character:");
        char7 = Console.ReadKey().KeyChar;

        ToDigit(ref char1, char2, char3, char4, char5, char6, char7);

    }

    static void ProcessInput()
    {
    }

    static void ToDigit(ref char char1, char char2, char char3, char char4, char char5, char char6, char char7)
    {
        switch(char1)
        {

            case "A": //all of these cases come up with the error
            case "a":
            case "B":
            case "b":
            case "C":
            case "c":
            case "2": Console.WriteLine("test");
                break;
            default: Console.WriteLine("this test did not work :(");
                break;
        }

    }

    static void ShowResults()
    {
    }

}

}

Answer

In C#, "A" means one letter string. To get char, use ': 'A'.

Your switch cases should be:

switch(char1)
{
    case 'A':
    case 'a':
    case 'B':
    case 'b':
    case 'C':
    case 'c':
    case '2': Console.WriteLine("test");
        break;
    default: Console.WriteLine("this test did not work :(");
        break;
}

Leave a Reply

Your email address will not be published. Required fields are marked *