views:

1102

answers:

3

Im using Console.ReadKey() to choose from a number of options that varies from time to time. Before this initial code snippet there is a for loop that counts occurances into counter variable of type int.

The point is to use Console.ReadKey() to get an int.

int choice = ReadKey();
Console.WriteLine("");

if (choice < counter)
{
    mail.to = result[counter-1].email;
}

By using the following methods

static int ReadKey()
{
    ConsoleKeyInfo choice = Console.ReadKey();
    char convertedchoice = choice.KeyChar;
    string convertedchoice2 = convertedchoice.ToString();
    int result = TryInt(convertedchoice2);
    return result;
}

static int TryInt(string totry)
{
    while (true)
    {
        int result;
        if (int.TryParse(totry, out result))
        {
            return result;
        }
        Console.WriteLine("Sorry, you need to enter a number. Try again.");
    }
}

I tried using ToString() but this was the way that it would let me do it in the end. So this looks kind of inneffective to me and hence I would really appreciate some guidance as what to do differently?

Edit:

I ended up with a combination of all the good answers below. Thanks alot guys.

static int ReadKey()
{
    while (true)
    {
        ConsoleKeyInfo choice = Console.ReadKey();
        if (char.IsDigit(choice.KeyChar))
        {
            int answer = Convert.ToInt32(choice.KeyChar);
            return answer - 48; //-48 because 0 is represented in unicode by 48 and 1 by 49 etc etc
        }
        Console.WriteLine("\nSorry, you need to input a number");
    }
}
A: 

You can just call Convert.ToInt32(choice.KeyChar); directly.

That would simplify it a bit.

Reed Copsey
That just gives me the error.'char' does not contain a definition for 'ToInt' and no extension method 'ToInt' accepting a first argument of type 'char' could be found
Patrik Björklund
It should be int result = Convert.ToInt32(choice.KeyChar); I will update my answer to be more explicit.
Reed Copsey
+1  A: 

For a menu system with coiches 0..9 this is reasonably OK. Not for reading larger numbers though.

Your whole checking logic can be made a lot easier with char.IsDigit() :

if char.IsDigit(convertedchoice)
{
  int result = convertedchoice - '0';
  return result;
}
else ...
Henk Holterman
the use of char.IsDigit() was an eye opener for me beoynd this problem. Thanks for doing preemptive work on questions from me!
Patrik Björklund
A: 

There are lots of ways to simplify your code, but for a start try to avoid putting everything into a variable. In general, things like:

(a + b + c) / 2

are much easier to read than things like:

int A_plus_B = a + b
int A_plus_B_plus_C = A_plus_B + c
int answer = A_plus_B_plus_C / 2

With this in mind, you could write:

static int ReadKey()
{
    while (true)
    {
        char ch = Console.ReadKey().KeyChar;
        int result;
        if (int.TryParse(ch.toString(), out result))
        {
            return result;
        }
    }
}
MarkusQ
Thanks for the advice but my professor actually prefers us to write like that for clarity when turning in assignments.
Patrik Björklund
That's not clarity. And FYI, when you post homework, you're supposed to tag it as such.
MarkusQ