Enumeration howto...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew Connell

    #1

    Enumeration howto...

    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
  • Miha Markic

    #2
    Re: Enumeration howto...

    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.ToStr ing();

    HTH,

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

    "Andrew Connell" <spam@aNOSPAM.c om> wrote in message
    news:%23gRQCE4q DHA.2644@TK2MSF TNGP09.phx.gbl. ..
    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


    Comment

    • Andrew Connell

      #3
      Re: Enumeration howto...

      What if I wanted to get the underlying value?

      "Miha Markic" <miha at rthand com> wrote in message
      news:eMfxwg7qDH A.2188@TK2MSFTN GP11.phx.gbl...[color=blue]
      > 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.ToStr ing();
      >
      > HTH,
      >
      > --
      > Miha Markic - RightHand .NET consulting & development
      > miha at rthand com
      >
      > "Andrew Connell" <spam@aNOSPAM.c om> wrote in message
      > news:%23gRQCE4q DHA.2644@TK2MSF TNGP09.phx.gbl. ..
      > 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[/color]
      of[color=blue]
      > 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
      >
      >[/color]


      Comment

      • Miha Markic

        #4
        Re: Enumeration howto...


        "Andrew Connell" <spam@aNOSPAM.c om> wrote in message
        news:%23XBYvI8q DHA.512@tk2msft ngp13.phx.gbl.. .[color=blue]
        > What if I wanted to get the underlying value?[/color]

        You mean the number beneath it?
        Just cast it.

        int i = (int)authors.Fi rstName;

        Btw, the previous example should be:
        string s = authors.FirstNa me.ToString();


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


        Comment

        Working...