For example System.Net.HttpStatusCode Enum, I would like to get the HTTP Status Codes instead of the HTTP Status Text. System.Net.HttpStatusCode.Forbidden
should return 403 instead of "Forbidden".
How can I extract the value?
Can one assign custom numeric values to enum elements in Java? Not directly as you've written, i.e., where an enum value equals a number, but yes indirectly as shown in Ben S's link.
The enum can be of any numeric data type such as byte, sbyte, short, ushort, int, uint, long, or ulong. However, an enum cannot be a string type.
Numeric enums are number-based enums i.e. they store string values as numbers. Enums are always assigned numeric values when they are stored. The first value always takes the numeric value of 0, while the other values in the enum are incremented by 1.
For the majority of Enum's simply cast to the base type which is int32.
int value = (int)System.Net.HttpStatusCode.Forbidden;
You can just cast it to an integer!
int code = (int)enumVariable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With