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)");
    }
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
ssk239fk
  • 29
  • 3
  • 4
    I'm voting to close this question as off-topic because I believe it belongs to [Code Review](https://codereview.stackexchange.com). – Yurii Jan 03 '18 at 07:54
  • 2
    One-liner: return "0123456789abcdefghijklmnopqrstuvwxyz".IndexOf(character); – Hans Kilian Jan 03 '18 at 08: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'`) – Peter Duniho Jan 03 '18 at 08: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`. – Peter Duniho Jan 03 '18 at 08:07
  • @ssk239fk I write a answer regarding this question https://stackoverflow.com/a/48073572/713789 – Anirudha Gupta Jan 03 '18 at 08:17
  • 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. – Hans Kilian Jan 03 '18 at 08:21
  • @Adrian don't do that, ever. SO isn't a discussion forum and posting answers to irrrelevant questions doesn't help anyone. – Panagiotis Kanavos Jan 03 '18 at 08:31
  • @PanagiotisKanavos removed – Anirudha Gupta Jan 03 '18 at 08:31

3 Answers3

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;
Hans Kilian
  • 18,948
  • 1
  • 26
  • 35
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

Adelin
  • 7,809
  • 5
  • 37
  • 65
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)");
    }
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Anirudha Gupta
  • 9,073
  • 9
  • 54
  • 79