1
public enum Question
{
    A= 02,
    B= 13,
    C= 04,
    D= 15,
    E= 06,
}

but when i use

int something = (int)Question.A;

i get 2 instead of 02. How can i get 02?

2
  • Oh well if you do a 2 == 02 it will equal. 2 and 02, int-wise, are the same. Commented Oct 11, 2011 at 11:54
  • you simply can't: it's an integer, so the correct result is 2!! 02 is a string and you can't use it as result for enums Commented Oct 11, 2011 at 11:54

6 Answers 6

7

Integers are not strings. It sounds like you want to format a string with at least two digits, which you can do with:

string text = value.ToString("00"); // For example; there are other ways

It's important to distinguish between the text representation of a value, and the information which is actually stored in a value.

For example, consider:

int base10 = 255;
int base16 = 0xff;

Here both variables have the same value. They were initialized using different forms of numeric literal, but they're the same value. Both could be formatted in decimal, hex, binary, whatever you want - but the values themselves are indistinguishable.

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

Comments

2

02 is a string representation of 2 with a precending zero. If you need to output this value somewhere try to use custom string format:

String.Format("{0:00}", (int)Question.A)

or

((int)Question.A).ToString("00")

For more details regarding this format string see on MSDN "The "0" Custom Specifier"

1 Comment

@senzacionale : sorry, I've updated the answer. Forgot to add first parameter placeholder
1

Integers are numbers - just like with money, $1 equals $1.00 and $000001. If you want to display a number in a certain way, you can use:

string somethingReadyForDisplay = something.ToString("D2");

Which converts the number into a string containing "02".

Comments

1

2 is exactly the same as 02, and 002 and 0002 etc.

Comments

1

Check this post : Enum With String Values In C# by this you can satisfy the needs easily realated to string

You can also try this option

 public enum Test : int {
        [StringValue("02")]
        Foo = 1,
        [StringValue("14")]
        Something = 2       
 } 

new custom attribute class, the source is below:

/// <summary>
/// This attribute is used to represent a string value
/// for a value in an enum.
/// </summary>
public class StringValueAttribute : Attribute {

    #region Properties

    /// <summary>
    /// Holds the stringvalue for a value in an enum.
    /// </summary>
    public string StringValue { get; protected set; }

    #endregion

    #region Constructor

    /// <summary>
    /// Constructor used to init a StringValue Attribute
    /// </summary>
    /// <param name="value"></param>
    public StringValueAttribute(string value) {
        this.StringValue = value;
    }

    #endregion

}

created a new Extension Method which I will use to get the string value for an enums value:

    /// <summary>
    /// Will get the string value for a given enums value, this will
    /// only work if you assign the StringValue attribute to
    /// the items in your enum.
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static string GetStringValue(this Enum value) {
        // Get the type
        Type type = value.GetType();

        // Get fieldinfo for this type
        FieldInfo fieldInfo = type.GetField(value.ToString());

        // Get the stringvalue attributes
        StringValueAttribute[] attribs = fieldInfo.GetCustomAttributes(
            typeof(StringValueAttribute), false) as StringValueAttribute[];

        // Return the first if there was a match.
        return attribs.Length > 0 ? attribs[0].StringValue : null;
    }

finally read value by

Test t = Test.Foo;
string val = t.GetStringValue();

- or even -

string val = Test.Foo.GetStringValue();

1 Comment

thx Rana. I found original link: codeproject.com/KB/cs/stringenum.aspx
0

02( the index ) stored as integer so you can't get it as 02 !!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.