Konrad Viltersten
Konrad Viltersten

Reputation: 39068

Custom names on enum elements

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";
}

Upvotes: 0

Views: 633

Answers (4)

Marc Wittmann
Marc Wittmann

Reputation: 2362

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;
}

Upvotes: 1

huMpty duMpty
huMpty duMpty

Reputation: 14460

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

Upvotes: 2

SchautDollar
SchautDollar

Reputation: 348

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

Upvotes: 1

Bill
Bill

Reputation: 1479

You could just cast the enum to an int:

return (int)wrong;

Upvotes: 1

Related Questions