Custom names on enum elements

我只是一个虾纸丫 提交于 2020-02-08 07:31:29

问题


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

回答1:


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




回答2:


You could just cast the enum to an int:

return (int)wrong;



回答3:


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




回答4:


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


来源:https://stackoverflow.com/questions/23062683/custom-names-on-enum-elements

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!