Enumeration howto...

  • Thread starter Thread starter Andrew Connell
  • Start date Start date
A

Andrew Connell

I'm trying to use enumerations to hardcode or mask (can't find the exact word) for my database table fields. For example:

public enum authors
{
FirstName = "FirstName",
LastName = "LastName"
}

What I'm trying to do is get the actual underlying value of my enum... and I'm having a hard time. Starting to think I need to use structs instead of enums. I've tried using the Parse method, but it just isn't working. Honestly, I just don't understand how to work with them.

Can someone provide a little assistance?

Thanks in advance.
-AC
 
Hi Andrew,

If the enum name is same as name you want, just declare the enum members.
When you need string from them, use ToString() method.

public enum authors
{
FirstName,
LastName
}

string s = FirstName.ToString();

HTH,

--
Miha Markic - RightHand .NET consulting & development
miha at rthand com

I'm trying to use enumerations to hardcode or mask (can't find the exact
word) for my database table fields. For example:

public enum authors
{
FirstName = "FirstName",
LastName = "LastName"
}

What I'm trying to do is get the actual underlying value of my enum... and
I'm having a hard time. Starting to think I need to use structs instead of
enums. I've tried using the Parse method, but it just isn't working.
Honestly, I just don't understand how to work with them.

Can someone provide a little assistance?

Thanks in advance.
-AC
 
Andrew Connell said:
What if I wanted to get the underlying value?

You mean the number beneath it?
Just cast it.

int i = (int)authors.FirstName;

Btw, the previous example should be:
string s = authors.FirstName.ToString();
 
Back
Top