Get number used for Enum
-
Got an enum. Got 2 get methods for it. One returns the instance of the enum itself, one intends to return the int used in the enum.
public enum elementTypes
{
normalText = 0,
hidden = 1,
programData = 2,
}There are more but this is an idea. As I said I'd like to be able to get the actual number, not the name or an instance.
Ninja (the Nerd)
Confused? You will be... -
Got an enum. Got 2 get methods for it. One returns the instance of the enum itself, one intends to return the int used in the enum.
public enum elementTypes
{
normalText = 0,
hidden = 1,
programData = 2,
}There are more but this is an idea. As I said I'd like to be able to get the actual number, not the name or an instance.
Ninja (the Nerd)
Confused? You will be... -
The only way I know of doing this is: elementTypes et; //This will give you the number (Int32)et.hidden; Hope that helps. Ben
Fantastic. Out of lack of skills, what the heck does the (Int32) prefix do? Is it some sort of implicit conversion?
Ninja (the Nerd)
Confused? You will be... -
Fantastic. Out of lack of skills, what the heck does the (Int32) prefix do? Is it some sort of implicit conversion?
Ninja (the Nerd)
Confused? You will be... -
Right. It casts the Enum as a number. It is kind of silly, but it the only way I know of to get the enums numerical value. I am sure someone else will post a different way of doing it, but that is the way I normally do it. Ben
OK... Thanks.
Ninja (the Nerd)
Confused? You will be... -
The only way I know of doing this is: elementTypes et; //This will give you the number (Int32)et.hidden; Hope that helps. Ben
I think a slightly better solution in this case is to use the int type rather than the Int32 object. I have not really used the Int32 object but I assume that it acts in the same way as other objects in that it passes itself by reference instead of value. This may then catch out other developers who are used to integers being value types and not objects. Instead use:
TestEnum te = new TestEnum(); int enumNumber = (int) te;
Hope this helps. -
Fantastic. Out of lack of skills, what the heck does the (Int32) prefix do? Is it some sort of implicit conversion?
Ninja (the Nerd)
Confused? You will be...Ninja-the-Nerd wrote:
what the heck does the (Int32) prefix do? Is it some sort of implicit conversion?
No, it's an explicit conversion. An implicit conversion is when the conversion is done without specifying the data type, this is allowed when widening types, like from a byte to an int. Example:
byte data = 42; int moreData = data;
An explicit conversion is when you specify the data type. This is used to tell the compiler that you want the conversion eventhough the compiler doesn't know if it will always work, or when an implicit conversion isn't allowed, like with an enum. Example:int data = 42; byte lessData = (byte)data;
--- "Anything that is in the world when you're born is normal and ordinary and is just a natural part of the way the world works. Anything that's invented between when you're fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it. Anything invented after you're thirty-five is against the natural order of things." -- Douglas Adams
-
I think a slightly better solution in this case is to use the int type rather than the Int32 object. I have not really used the Int32 object but I assume that it acts in the same way as other objects in that it passes itself by reference instead of value. This may then catch out other developers who are used to integers being value types and not objects. Instead use:
TestEnum te = new TestEnum(); int enumNumber = (int) te;
Hope this helps.MCEdwards wrote:
I think a slightly better solution in this case is to use the int type rather than the Int32 object.
There is no difference.
MCEdwards wrote:
I have not really used the Int32 object
Yes, you have. The
int
keyword is an alias for the System.Int32 data type.MCEdwards wrote:
I assume that it acts in the same way as other objects in that it passes itself by reference instead of value.
Assume away... ;) Int32 is a structure, not a class, so it's a value type, not a reference type.
--- "Anything that is in the world when you're born is normal and ordinary and is just a natural part of the way the world works. Anything that's invented between when you're fifteen and thirty-five is new and exciting and revolutionary and you can probably get a career in it. Anything invented after you're thirty-five is against the natural order of things." -- Douglas Adams
-
Right. It casts the Enum as a number. It is kind of silly, but it the only way I know of to get the enums numerical value. I am sure someone else will post a different way of doing it, but that is the way I normally do it. Ben
Yeah here's another way:
int number = enum as int;
XDMy current favourite word is: Waffle Cheese is still good though.
-
Yeah here's another way:
int number = enum as int;
XDMy current favourite word is: Waffle Cheese is still good though.
The Undefeated wrote:
int number = enum as int;
that will not be accepted, for one "as" requires a reference type, not a value type. :)
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google
-
The Undefeated wrote:
int number = enum as int;
that will not be accepted, for one "as" requires a reference type, not a value type. :)
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google
*shakes fist angrily at Luc* You ruin everything :rolleyes: :rose: int Waffle(ref Enum myEnum) { return myEnum as int; }
My current favourite word is: Waffle Cheese is still good though.
-
*shakes fist angrily at Luc* You ruin everything :rolleyes: :rose: int Waffle(ref Enum myEnum) { return myEnum as int; }
My current favourite word is: Waffle Cheese is still good though.
no no. Error: The as operator must be used with a reference type ('int' is a value type) :suss:
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google
-
no no. Error: The as operator must be used with a reference type ('int' is a value type) :suss:
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google
*cries* Perhaps if i actually tried to use the code before i posted it. *cries some more*
My current favourite word is: Waffle Cheese is still good though.
-
*cries* Perhaps if i actually tried to use the code before i posted it. *cries some more*
My current favourite word is: Waffle Cheese is still good though.
Yeah, the sensible sequence is: - try to understand the problem at hand - read the documentation - try a couple of things - search for a solution for the remaining problems - only then post at CodeProject :)
Luc Pattyn [Forum Guidelines] [My Articles]
this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google