Results 1 to 8 of 8

Thread: enum's numerical value

  1. #1
    Lively Member drdress's Avatar
    Join Date
    Jul 09
    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
    Frenzied Member kfcSmitty's Avatar
    Join Date
    May 05
    Location
    Kingston, Ontario
    Posts
    1,789

    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 04
    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 11
    Posts
    585

    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.
    <<<------------
    < Please rate my post if this helped you out. Any kind of thanks is gladly appreciated >



    VB Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  5. #5
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 04
    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 11
    Posts
    585

    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.
    <<<------------
    < Please rate my post if this helped you out. Any kind of thanks is gladly appreciated >



    VB Programming (2012 - Present)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

  7. #7
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 04
    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 09
    Location
    Watch Window(Shift+f9)
    Posts
    1,431

    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
  •