Results 1 to 8 of 8

Thread: enum's numerical value

  1. #1

    Thread Starter
    Lively Member drdress's Avatar
    Join Date
    Jul 2009
    Location
    Copenhagen, Denmark
    Posts
    76

    enum's numerical value

    items in an enum are assigned a value, right? If I try to get the value of an enum variable, it only gives me the name of the variable as a string. For saving and loading purposes it would be nice if I could get the numerical value instead.

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: enum's numerical value

    To get the numerical value of the enum, just cast it as an int;

    C# Code:
    1. public enum Test
    2. {
    3.   Zero,
    4.   One,
    5.   Two
    6. }

    C# Code:
    1. Console.WriteLine((int)Test.Two);

  3. #3
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Re: enum's numerical value

    It is worth noting that it is good practise to initialise your first enum value to 0.

    Code:
    public enum Test2
    {  
        Zero = 0,  
        One,  
        Two
    }

  4. #4
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: enum's numerical value

    Quote Originally Posted by x-ice View Post
    It is worth noting that it is good practise to initialise your first enum value to 0.

    Code:
    public enum Test2
    {  
        Zero = 0,  
        One,  
        Two
    }
    Not exactly, that's not necessary all the time. If you want the first value not to start from 0 then you don't have to set it to 0. You don't really need to set anything if you're just going to use the value names. It's all sequential.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  5. #5
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Re: enum's numerical value

    If you would like your enum values to start at n>0 you have to specify the value of your first element as it defaults to 0

  6. #6
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: enum's numerical value

    Quote Originally Posted by x-ice View Post
    If you would like your enum values to start at n>0 you have to specify the value of your first element as it defaults to 0
    That was my point though. You don't have to set it to 0.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  7. #7
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Re: enum's numerical value

    The main reason i always set it to zero as above is for the sake of readability

  8. #8
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Re: enum's numerical value

    Enum is nothing but readable variable
    Code:
    public enum Test2
    {  
        Zero = 0,  
        One=1,  
        Two=2,
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width