Evil Burgers
Evil Burgers

Reputation: 25

Cannot convert system.consolekeyinfo to string

What I'am trying to achieve here is that when you press "1" it will check it against "code_1" and then if it matches it will say "key1 correct" and then check the other codes. But the compiler says

Cannot convert system.consolekeyinfo to string

so I'm wondering how I fix this. Here is the code that I use :

    static void Main(string[] args)
    {
        string first_time = null;
        string paktc = "Press any key to continue . . .\r\n";
        string code_1 = "1";
        string code_2 = "2";
        string code_3 = "3";
        string code_4 = "4";
        if (first_time == null)
        {
            Console.WriteLine("\r\nYour code is 1234\r\n");
            Console.WriteLine(paktc);
            Console.ReadKey();
            Console.WriteLine("Insert Code Now\r\n");

            ConsoleKeyInfo key1 = Console.ReadKey();
            if (code_1 = key1)
            {
                ConsoleKeyInfo key2 = Console.ReadKey();
                if (code_2 = key2)
                {
                    ConsoleKeyInfo key3 = Console.ReadKey();
                    if (code_3 = key3)
                    {
                        Console.WriteLine("Key3 Correct\r\n");
                        ConsoleKeyInfo key4 = Console.ReadKey();
                        if (code_4 = key4)
                        {
                            Console.WriteLine("Key4 Correct\r\n");
                            Console.ReadKey();
                            Console.WriteLine(paktc);
                        }
                        else
                        {

                        }
                    }
                    else
                    {

                    }
                }
                else
                {

                }
            }
            else
            {

            }
        }
    }
}

Upvotes: 2

Views: 7544

Answers (3)

Alomoes
Alomoes

Reputation: 11

ToString() would work. Say we have ConsoleKeyInfo j.

It'll look like string k = j.KeyChar.ToString();

This will do exactly what you want.

So the code will look like:

ConsoleKeyInfo key1 = Console.ReadKey();

if (code_1 == key1.KeyChar.ToString())
{
//Other stuff here as follows.  
}

You could even do this.

if (key1.KeyChar.ToString() == "1")
{
//Other stuff here as follows
}

Upvotes: 1

BradleyDotNET
BradleyDotNET

Reputation: 61349

The error you are currently getting is because you forgot that:

= and == are NOT the same thing. The first is assignment, the second is comparison.

And you can't assign a string to a ConsoleKeyInfo, or vice versa, and definitely not in an if statement. Even if you had fixed that however, you still can't compare a string to a ConsoleKeyInfo. You can get its KeyChar property and compare that to a char though:

if (keyInfo.KeyChar == myString[0])

is valid (as string can be indexed to get its chars). In your case, you can just use a char and make it much simpler:

if (keyInfo.KeyChar == '1')

Upvotes: 1

Phreebird
Phreebird

Reputation: 1

Use Console.Read(); instead, it returns a int witch can be typecasted into a char. Also instead of having 4 strings with one character in them, you can have one string with the full code in it and use it as an array, See the example below

    static void Main(string[] args)
    {
        string pw = "123";
        Console.WriteLine("Enter the first digit of the password");
        char toTest = (char) Console.Read();
        Console.Read();
        Console.Read();
        if (toTest == pw[0])
        {
            Console.WriteLine("Enter the second digit of the password");
            toTest = (char)Console.Read();
            Console.Read();
            Console.Read();
            if (toTest == pw[1])
            {
                Console.WriteLine("Enter the third digit of the password");
                toTest = (char)Console.Read();
                Console.Read();
                Console.Read();
            }
        }
    }

The extra Console.Read(); commands are to catch the invisible characters that are inputted when pressing enter.

Upvotes: 0

Related Questions