By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,871 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,871 IT Pros & Developers. It's quick & easy.

Default value of an enum

P: n/a
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(...)

Jun 29 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<ve*********@hotmail.com> wrote in message
news:11*********************@i40g2000cwc.googlegro ups.com...
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(...)

Jun 29 '06 #2

P: n/a
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

Nicholas Paldino [.NET/C# MVP] wrote:
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.

Jun 29 '06 #3

P: n/a
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...nAttribute.asp
ve*********@hotmail.com wrote:
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(...)


Jun 29 '06 #4

P: n/a
ja**********@gmail.com 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...nAttribute.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 :-).

Jun 30 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.