473,408 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Default value of an enum

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
4 10807
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: dumboo | last post by:
hi there, i was looking for some way to give default values to enum, when ever i m creating an enum variable it shuld be INITIALIZED to certain default values is it possible ? or do i have to...
7
by: maf | last post by:
Using reflection, I'm trying to get the value for a constant in an enum. Getting the contant name works fine using: FieldInfo fieldInfos = TYPE.GetFields(); foreach(FieldInfo fi in fieldInfos...
3
by: Scott Liu | last post by:
HI, All, I have a web service doing a string search. It has an operator and a searchValue field. The operator is defined as an attribute and required. The xml is as below. <!--...
0
by: ASP Developer | last post by:
I have a web service that returns a class when a web method is called. This class has a enum property with four values. These four values have default numbers. For example, Apple = 5 Orange...
2
by: Lucky | last post by:
Hi guys, i was working with Enum and one Question strike, i thought it would be good idea to ask more experience guys around. the scenerio is like this : public enum check {
7
by: Travis | last post by:
I'm curious, if you have an enum say... enum Days { Mon, Tue, Wed, Thu, Fri, Sat, Sun }; I understand the default will be Mon=0, Tue=1, Wed=2, etc. What I'm curious about is if there is a...
0
by: Dave Raskin | last post by:
I am trying to specify a logical default value for a in a WCF Web Service using basicHttpBinding. I realize that the language defaults are: int - 0 string - null bool - false enum - ?
0
by: Dave Burns | last post by:
Hello, I am trying to specify a logical default value for a in a WCF Web Service using basicHttpBinding. I realize that the language defaults are: int - 0 string - null bool - false
4
by: Dave Burns | last post by:
Hello, I am trying to specify a logical default value for a in a WCF Web Service using basicHttpBinding. I realize that the language defaults are: int - 0 string - null bool - false
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.