|
-
Aug 30th, 2012, 06:59 AM
#1
Thread Starter
Lively Member
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.
-
Aug 30th, 2012, 08:28 AM
#2
Re: enum's numerical value
To get the numerical value of the enum, just cast it as an int;
C# Code:
public enum Test
{
Zero,
One,
Two
}
C# Code:
Console.WriteLine((int)Test.Two);
-
Aug 30th, 2012, 09:32 AM
#3
Fanatic Member
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
}
-
Aug 31st, 2012, 11:49 PM
#4
Re: enum's numerical value
 Originally Posted by x-ice
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.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Sep 1st, 2012, 04:24 PM
#5
Fanatic Member
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
-
Sep 1st, 2012, 04:51 PM
#6
Re: enum's numerical value
 Originally Posted by x-ice
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.
<<<------------
.NET Programming (2012 - 2018)
®Crestron - DMC-T Certified Programmer | Software Developer <<<------------
-
Sep 1st, 2012, 04:53 PM
#7
Fanatic Member
Re: enum's numerical value
The main reason i always set it to zero as above is for the sake of readability
-
Sep 5th, 2012, 05:32 PM
#8
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|