Default value of an enum

V

veerleverbr

Suppose having define an enum like this:

public enum SomeEnum
{
[DefaultValue("1")]
Something,
[DefaultValue("2")]
SomethingElse
}

Having a variable of the type ETransactionOwnerType, say:

SomeEnum test = SomeEnum.SomethingElse;

How can get this variable's default value?

string defaultValue = test. ??? // defaultValue should be "2"

I already tried looping through the values of the enum, but there
doesn't seem to be a method there that gives me the default value
either:

Type testEnum = Type.GetType("SomeEnum");
foreach(int value in Enum.GetValues(testEnum))
{
string name = Enum.GetName(testEnum, value);
}
// no method in the style of: Enum.GetDefaultValue(...)
 
N

Nicholas Paldino [.NET/C# MVP]

The values of an enumeration are really constants that are on the type
itself. If you get the type of the enum, then look for the static field
with the same name on the type. You can then call GetCustomAttributes on
the FieldInfo to get the attribute you have assigned to the enumeration.

Also, I hope that DefaultValue isn't tied to the value of the
enumeration, since Something in this example has a value of 0.

Hope this helps.
 
R

Rodney J. Woodruff

Moreover,

I hope you don't intend on changing the value of an enum at runtime.
This will definitely create havoc for you in the future as well as
anyone who uses the class, library or framework that has this kind of
construct in it.

Enums are ordered serially, unless you explicitly specify their values.
In your case:

public enum SomeEnum
{
[DefaultValue("1")]
Something,
[DefaultValue("2")]
SomethingElse
}

is seen by the CLR as

public enum SomeEnum
{
[DefaultValue("1")]
Something = 0,
[DefaultValue("2")]
SomethingElse = 1
}

Explicit declaration would be

public enum SomeEnum
{
Something = 1,
SomethingElse = 2
}

What are you trying to accomplish?

-- Rodney
 
J

james.curran

Well, first thing we should note is that DefaultValue is intended to be
used on properties on user-defined WebForm & WinForm countrol. It has
no predefine meaning when placed on an enum value. All you are doing
is stuffing some extra data into the enum's Type object.

Now, if you want to set the value for the items in the enum, there is
an easier way:
public enum SomeEnum
{
Something = 1,
SomethingElse= 2
}

IF these DefaultValues have some other meaning, you could use the
method described in this article to retrieve them:
http://www.codeproject.com/useritems/EnumDescriptionAttribute.asp
 
V

veerleverbr

(e-mail address removed) schreef:
Well, first thing we should note is that DefaultValue is intended to be
used on properties on user-defined WebForm & WinForm countrol. It has
no predefine meaning when placed on an enum value. All you are doing
is stuffing some extra data into the enum's Type object.

Now, if you want to set the value for the items in the enum, there is
an easier way:
public enum SomeEnum
{
Something = 1,
SomethingElse= 2
}

IF these DefaultValues have some other meaning, you could use the
method described in this article to retrieve them:
http://www.codeproject.com/useritems/EnumDescriptionAttribute.asp

Thanks a lot, that's what I was looking for.

It would indeed be a lot easier if these default values were the actual
values of the items in the enum, like you suggest. The problem is that
my enum is the result of a code generator over which I have no control.
It generates the enums based on the content of a database table. And I
needed the default value in my code to use it as a parameter for
calling some stored procedure. So I need to able to retrieve the
default value, and now I know how to :).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top