0

Suppose there's an enum like so.

[Flags]
public enum Crazies
{
  One = 2,
  Two = 3,
  Three = 4
}

Now, when I force the different values to be stringified, I get the text in the beautiful tongue, which differ considerably from the usual meaning of the numerics. Suppose now that the users prefer a more conventional definition of the natural numbers and wish to see the text "Two", although the variable value is One (NB it's Crazies.One).

What'd be the simplest way to map the stringification of the predefined names of the elements? Right now I'll go with something like the following but I don't really like it.

private static String DecrazyfyCrazies(Crazies wrong)
{
  switch(wrong)
  {
    case Crazies.One: return "Two";
    case Crazies.Two: return "Three";
  }
  return "Four";
}
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438

4 Answers4

2

If you need int just cast to int

return (int)Crazies.One;

For more custom string display you can use display name

i.e

public enum Crazies
{
  [Display(Name="Custom Name for One")]
  One = 2,
  [Display(Name="Custom Name for Two")]
  Two = 3     
}

See more about Power Up Your Enum

huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99
1

You could just cast the enum to an int:

return (int)wrong;
Bill
  • 1,431
  • 10
  • 13
1

I'd recommend setting the type of your enum.

[Flags]
public enum Crazies : int
{
   One = 2,
   Two = 3,
   Three = 4
}

Now you can just return the enum with return Crazies.One.

There are a several types you can use.

The approved types for an enum are byte, sbyte, short, ushort, int, uint, long, or ulong. http://msdn.microsoft.com/en-us/library/sbbt4032.aspx

SchautDollar
  • 348
  • 3
  • 13
1

Create an int getter which returns

return (int) enumvar + 1;

if you really need words you can just add a static class.. but you don`t need to alter it for every mutation of you crazy enum

public static string NumberToWords(int number)
{
    if (number == 0)
        return "zero";

    if (number < 0)
        return "minus " + NumberToWords(Math.Abs(number));

    string words = "";

    if ((number / 1000000) > 0)
    {
        words += NumberToWords(number / 1000000) + " million ";
        number %= 1000000;
    }

    if ((number / 1000) > 0)
    {
        words += NumberToWords(number / 1000) + " thousand ";
        number %= 1000;
    }

    if ((number / 100) > 0)
    {
        words += NumberToWords(number / 100) + " hundred ";
        number %= 100;
    }

    if (number > 0)
    {
        if (words != "")
            words += "and ";

        var unitsMap = new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
        var tensMap = new[] { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };

        if (number < 20)
            words += unitsMap[number];
        else
        {
            words += tensMap[number / 10];
            if ((number % 10) > 0)
                words += "-" + unitsMap[number % 10];
        }
    }

    return words;
}
Marc Wittmann
  • 2,286
  • 2
  • 28
  • 41