0

I've recently took over a project as a personal hobby in a challenge to improve my code quality. I had made this method but I feel like I'm definitely over working it in some way, I feel like there's a really simple way to do it without having to run through a large switch like this??

I know the 0-9 could just be .ToString()'d and then parsed to int and returned, but I'm unsure how to merge that idea whilst also considering the rest of the switch cases.

public static short ParseHeight(char input)
{
    switch (input)
    {
        case '0':
            return 0;
        case '1':
            return 1;
        case '2':
            return 2;
        case '3':
            return 3;
        case '4':
            return 4;
        case '5':
            return 5;
        case '6':
            return 6;
        case '7':
            return 7;
        case '8':
            return 8;
        case '9':
            return 9;
        case 'a':
            return 10;
        case 'b':
            return 11;
        case 'c':
            return 12;
        case 'd':
            return 13;
        case 'e':
            return 14;
        case 'f':
            return 15;
        case 'g':
            return 16;
        case 'h':
            return 17;
        case 'i':
            return 18;
        case 'j':
            return 19;
        case 'k':
            return 20;
        case 'l':
            return 21;
        case 'm':
            return 22;
        case 'n':
            return 23;
        case 'o':
            return 24;
        case 'p':
            return 25;
        case 'q':
            return 26;
        case 'r':
            return 27;
        case 's':
            return 28;
        case 't':
            return 29;
        case 'u':
            return 30;
        case 'v':
            return 31;
        case 'w':
            return 32;
        default:
            throw new FormatException("The input was not in a correct format: input must be between (0-k)");
    }
}
8
  • 4
    I'm voting to close this question as off-topic because I believe it belongs to Code Review. Commented Jan 3, 2018 at 7:54
  • 2
    One-liner: return "0123456789abcdefghijklmnopqrstuvwxyz".IndexOf(character); Commented Jan 3, 2018 at 8:05
  • There's no reason to call ToString() on a char and parse the result, when you can just subtract the base value of 0 from the char value. See marked duplicate. Likewise, all the other letter ranges can be handled by subtracting the appropriate base value and adding an appropriate offset (e.g. (ch - 'a') + 10 for values where ch >= 'a' && ch <= 'z') Commented Jan 3, 2018 at 8:06
  • 1
    I do find it amusing that the exception thrown will tell the caller that they have to provide a value between 0 and k (without being specific as to whether other characters like !, %, A, etc. that may be between those values are legal), and yet the method apparently handles letter characters up to w. Commented Jan 3, 2018 at 8:07
  • 2
    It's a little sad that people are so quick to mark a question as duplicate when it is in fact not a duplicate of the question you link to. Commented Jan 3, 2018 at 8:21

3 Answers 3

9

You could do it like this

var value = "0123456789abcdefghijklmnopqrstuvwxyz".IndexOf(input);
if (value == -1)
    throw new FormatException("The input was not in a correct format: input must be between (0-k)");
return value;
Sign up to request clarification or add additional context in comments.

2 Comments

That's probably the best solution for this (at least in temrs of simplicity)
Thank you, this is much simpler, definitely works :)
2

For letters:

You could make use of ascii code

Lets take letter a for example, according to this table hex code for a is 61, and increments for each letter (b is 62 and so on). You want 10, so you need to substract 51 from each such code.

You can also use the decimal representation of a character from that table. This answer tells you how to get the decimal representation of a char

For numbers

Return directly.


I'm merely suggesting the algorithm I would use. I suppose you can implement the code for this yourself

Comments

1

Try this code,

public static short ParseHeight(char input)
{
    var res = input - 0;
    if (res <= 57)
        return (short)(res - 48);
    else if (res >= 97)
        return (short)(res - 87);
    else
    {
        throw new FormatException("The input was not in a correct format: input must be between (0-k)");
    }
}

Comments