3

A colleague of mine asked me today if it's possible to get the value of an enum at design time. I pointed out it's easy to do this at runtime using a cast and the immediate window.

Apparently in VB6 it's possible to do this using quick info but is there a way to do this in C# at design time?

The enum is large and stores it's value in a database as an int. We have a bug were we suspect the values in the database are no longer in sync with the enum (presumably due to someone editing the enum) and we need to know what the value in the database is actually referring to.

James
  • 9,774
  • 5
  • 34
  • 58
  • Not an answer but if you're depending on integer values of your enums then it's high time you start manually entering these values. – empi May 03 '12 at 11:34

6 Answers6

2

Why not writing a little program that takes your enum and iterates over all fiels of the enum to print out something like "EnumEntry = EnumValue". That you get a explicit description for all fields of your enum and can do further design time analysis.

Array values = Enum.GetValues(typeof(myEnum));

foreach( MyEnum val in values )
{
   Console.WriteLine (String.Format("{0}: {1}", Enum.GetName(typeof(MyEnum), val), val);
}

from here: C# Iterating through an enum? (Indexing a System.Array)

Community
  • 1
  • 1
Tarion
  • 16,283
  • 13
  • 71
  • 107
  • 1
    So if you are asking for an IDE feature do give your the version you can search for VS addins. There is no such feature by default. – Tarion May 03 '12 at 11:30
1

If you have a database table with enums and you want to line that up with an enum in c#, I suggest you do the following:

enum Whatever
{

  Value1 = 1, 
  Value2 = 2, 
  Value3 = 3
}

Doing it this way, you line up your primary key (auto incremented probably) with the enum and it will not likely change. If someone comes along and wants to add some random enum value, it will not affect the existing ones.

Chris Gessler
  • 22,727
  • 7
  • 57
  • 83
1

You can use Immediate window in design time, unless it is Web application and few others. Something down the line: String.Format("{0:D}", enumValue)

0

If no explicit values are given, enums members start from 0 incrementing by 1. You can use this to determine the numeric value of the enum.

For example:

enum Days
{
Sunday,
Monday,
Tuesday,
}

In the above, Sunday=0, Monday=1, and so on....

logicnp
  • 5,796
  • 1
  • 28
  • 32
  • this is true but becomes a little impracticle when there are more than about 15 in an enum. – James May 03 '12 at 12:49
0

Yes it is Possible in c# too, if you do not explicitly define value it assign values starting from 0 but you can assign your values too;

enum Days
{
Sunday=10,
Monday=20,
Tuesday=30,
}

and you can get values

console.WriteLine((int)Days.Sunday); //print 10
console.WriteLine(Days.Sunday); //print Sunday
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
0

I guess you want to see the enum integer values directly in the debugger instead of their names.

You can achieve this using the DebuggerDisplayAttribute (be aware that raw object visualization must be off). Simply decorate your enum declaration as follow:

[DebuggerDisplay("{ToString()} ({ToString(\"d\")})")]
enum MyEnumToDebug
{
}

When you'll hover your variable of type MyEnumToDebug inside the debugger you'll see both the name and the integer value.

Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208