✅ enumeration help
can you call on the “index” of an entry in an enumeration? like say theres 3 options,
red
, blue
, green
, and i have an array with 1
, 2
, 3
.
if a random int is used to select between the array, can i also use that same random int to call upon the corresponding enumeration? i.e. red
being called at the same time/in a similar way as 1
, blue
and 2
, etc
sorry if the question doesnt make sense, i can rephrase11 Replies
if mean with enumeration something like
that is possible. under the hood these are just numbers as well.
the above enum could also be written as
the values are implicitly added.
and then u can cast between the enum and int,
Color color = (Color)1;
would be the same as Color color = Color.Blue;
and vice versa
int num = (int)Color.Green;
would be the same as int num = 2;
so actually you would not even need the array here at alloh lovely, thats so cool! thanks so much
so for instance what if i just wanted to debug log the randomized color to string
u would have it as
Color
value already then, its ToString()
method will do the right thing, and stuff like Console.WriteLine();
automatically calls that methodcap5lut
REPL Result: Success
Console Output
Compile: 386.570ms | Execution: 21.133ms | React with ❌ to remove this embed.
hey @mack, there is one thing about the numbers and enum stuff i forgot to mention.
its entirely possible to get "undefined" enums.
eg for
enum Color { Red, Blue, Gree }
where the values are respectively 0, 1 and 2
u can still do stuff like Color color = (Color)255;
, tho its not a defined enum.
to check if it is a valid/defined enum, there is the Enum.IsDefined() method.Enum.IsDefined Method (System)
Returns a Boolean telling whether a given integral value, or its name as a string, exists in a specified enumeration.
cap5lut
REPL Result: Success
Console Output
Compile: 480.216ms | Execution: 25.329ms | React with ❌ to remove this embed.
thanks for this extra information :) youre very helpful
if your questions are answered, please $close the thread, btw 🙂
If you have no further questions, please use /close to mark the forum thread as answered
$close
If you have no further questions, please use /close to mark the forum thread as answered