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.
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.
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);
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 }
<<<------------< 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
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
<<<------------< 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
The main reason i always set it to zero as above is for the sake of readability
Enum is nothing but readable variableCode:public enum Test2 { Zero = 0, One=1, Two=2, }