0

I have an enum like this:

enum myEnum
{
    a = 0101,
    b = 2002,
    c = 0303
}

I try to get enum value with casting but the 0 at the begin of my enums was removed.

For example I tried this:

var val = (int)myEnum.a;

How can get enum value as string when we have 0 at the beginning of it?

3
  • 9
    How should the system know whether you want to have zero, one or more leading zeros? Did you know that 1 is the same as 01 and 001? (So actually James Bond is not 007 but actually just 7). Commented Mar 28, 2018 at 5:37
  • 2
    Enum is an int, the value is saved as such and your formatting is removed, consider using a different structure to save your formatting if needed Commented Mar 28, 2018 at 5:38
  • Do you need 0101 as string or need "a" as output? Commented Mar 28, 2018 at 6:03

6 Answers 6

1

You should rethink your design, but if you want to check your enum integers to a given string, you can use .ToString("0000") to get the string "0101" out of the integer 101.

Sign up to request clarification or add additional context in comments.

Comments

1

First, enums are integers, since as their name says, they are enumerations and an enumeration, they are numbers, so enum is integer.

Secondly, you must bear in mind that zero is a null value, since the system is a 01 or 001 like 1, since (basic mathematics) a zero to the left is worthless, so this code is incorrect.

enum myEnum
{
   a=0101,
   b=2002,
   c=0303,
}

The correct way is

enum myEnum
{
   a = 0,
   b = 1,
   c = 2
}

Where the zero is alone, so the system sees it as an index

Now with this, you should only use one of the conversion processes of C#

string strOne = ((myEnum)0).ToString();
string strTwo = ((myEnum)1).ToString();
string strThree = ((myEnum)2).ToString();

Read the MSDN reference https://msdn.microsoft.com/en-us/library/16c1xs4z(v=vs.110).aspx

Comments

1

Enumeration values are always integers. If you need to associate a string with an enumeration value, you can use a dictionary:

enum myEnum { a, b, c }

Dictionary<myEnum, string> lookup = new Dictionary
{
    { a, "0101" },
    { b, "2002" },
    { c, "0303" }
};

To get the string associated with a particular value just use this:

var s = lookup[myEnum.a];  // s = 0101

Another common way to handle this sort of problem is simply to use constants.

class MyConstants
{
    public const string a = "0101";
    public const string b = "2002";
    public const string c = "0303";
}

var s = MyConstants.a; // s = 0101

Comments

1

Try using formatting: you want 4 digits and that's why you can put d4 format string. In order to hide all these implmentation details (cast and formatting) let's write an extension method:

  enum myEnum {
    a = 0101,
    b = 2002,
    c = 0303
  }

  static class myEnumExtensions {
    public static string ToReport(this myEnum value) {
      return ((int)value).ToString("d4"); // 4 digits, i.e. "101" -> "0101"
    }
  }

  ...

  myEnum test = myEnum.a;

  Console.Write(test.ToReport());

Comments

1

If you always need a specific number of digits you could use string format to get the leading zeros:

var str = String.Format("{0:0000}", (int)myEnum.a); 

Or, shorter:

var str = $"{(int) myEnum.a:D4}"; 

Alternative:

Use an attribute to add extra information to an enum

Attribute:

public class DescriptionAttribute : Attribute
{
    public string Name { get; }

    public DescriptionAttribute(string name)
    {
        Name = name;
    }
}

Enum:

enum myEnum
{
    [Description("0101")]
    a = 101,

    [Description("2002")]
    b = 2002,

    [Description("303")]
    c = 303
}

Extension Method:

public static string GetDescription(this myEnum e)
{
    var fieldInfo = e.GetType().GetField(e.ToString());
    var attribute = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault() as DescriptionAttribute;
    return attribute.Name;
}

Usage:

var name =  myEnum.a.GetDescription() //will return '0101'

Comments

-2

Assuming that the numbers in your enum always have a length of 4 you can use the following

var val = (int)myEnum.a).ToString().PadLeft(4, '0')

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.