❔ Compare Enum Values with submitted value
Hi, how can I compare the submitted value with values in an enum without using switch-case? For example,
44 Replies
I hope this gives more context than my previous post on what I'm trying achieve
You can use compare with values i suppose
But why you are avoiding using switch case ?
I'm not, I'm just looking to see if it's possible to use it within the if statement rather than having another switch-case block just for that one comparison, like
if (Model.State.IsValid && day.Day != values && day.File != null)
, so that means I have a switch-case block just for day.Day != values
the compareto()
?Basicly Enum values are int actually
underlying type is int
If you count Monday as a 0
5 and 6 is weekends
I'm guessing it's not pretty to do it within the if statement, but do something like?
Toshiya Joshima#3472
REPL Result: Success
Console Output
Compile: 727.529ms | Execution: 111.930ms | React with ❌ to remove this embed.
you can check index number it's 5 or 6 etc
with switch
it should be look something like this
Hmm, another reason why I didn't wanna use switch case is because there's a chunk of code under the if body, so moving them inside the case won't look neat.
er... if I'm understanding this right, you're just looking for
if (day.Day == Days.Saturday)
No, both values. The above except the post itself were just examples
Then put an or in
Yeah, something like
if (Model.State.IsValid && day.Day != Saturday or day.Day != Sunday && day.File != null)
, I was also hoping if it was possible to just extract the values and assign them to a variablebut really, make a
Day[] Weekends = new() { Days.Saturday, Days.Sunday }
and check if (Weekends.Contains(day.Day))
I'm assuming this one checks against the values in
enum Weekends
? Or is it creating a new enum?It is assuming there is
enum Days
, and replaces enum Weekends
Also I'd rename that variable, day
- it's not a Day, day.Day is a Day...if (day.Day is Days.Saturday or Days.Sunday)
is fine too
after handling weekends
else part is weekdays
Assumes there is
enum Days
...hmm, so, it is creating a new enum then? If not, I still don't get this part
I'm assuming it should be if (day.Day is Days.Saturday or day.Day is Days.Sunday)
?no
Btw, this wouldn't really work as
day
gets value from user selection. So Weekends.Contains(day.Day))
checks for the value in that enum, when it should be the other way aroundThe user selects a Day, from enum Days. Weekends is a collection of Days. If the user-selected Day is in Weekends, it's a weekend
Apparently I get errors saying that Day[] cannot be used as a context of type new()
Also, Days doesn't exist error as well...
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Yea but flag is checking for value existence tho, I'm not checking to see if the value exists
I'm checking if the selected value is the same as the one in enum. The dropdown value is retrieved from the enum, so in other words, values already exist, just need to check what the user selects
Plus, I'm not looking to modify my model class. It seems like Flags requires some modifications to it
If im understanding correctly you just want to make the expression shorter?
Maybe something like
Hmm, how about this?
and in my if statement, since I don't want to accept these two values, I just negate like
if (!weekendValues)
. I tried it and it works, but I'm not sure if there's some underlying caveat here
Getting error "Type must be an enum that does not have an associated FlagsAttribute. (Parameter 'TEnum')"are you using the flags attribute?
Yea, I have [Flags] placed above the public enum
do you need it? you cant use it if you want to use HasFlag
Does this [Flags] modify anything? Or it's there awaiting for instruction? Like the use of "HasFlag"
Because if it does modify, this solution won't work for me, it will disrupt 80+ other references to this enum
Ohh, so no need the [Flags] right?
nope
Hmm, ill try again
Didn't work, same error
Ohh now it's working, hmm, weird
Okay yeah, tested both ways. It works.
Cool, two ways of solving it after hours of debugging lol.
Bitwise ops are a little faster i think but really its negligible. Im curious tho, is the weekends enum new or does it exist in your code base already?
already exist
But yeah, I read that HasFlag is best used when values aren't defined
Since it's checking for value existence
I ask because maybe it would be cleaner to do something like
Tho i guess this would create issues if you try to dynamically enumerate the values into a list or somethin
Yeah I didn't wanna use this cuz idk if it will cause some side-effects to the other 80+ classes referencing this class
yup thats reasonable lol
I'm assuming for this approach, I'd just need to do something like?
Well if you added the Weekends values to
Days
then all you would need to do is
bool isWeeknd = Days.Weekends.HasFlag(day.Day);
Assuming day.Day
is of enum type Days
Also, I read that HasFlag is used for FlagsAttributes, but somehow it managed to work even without defining the FlagsAttribute?
Owhh, okay....that's more cleaner. Still, can't use it without knowing it wouldn't break some other things first. Thanks tho
The flags attribute should really only be used when the enums values are base 2.
ei 0,1,2,4,8,16,etc
EDIT: meant powers of 2
The has flag works rgardless because at its core its a bitwise check (IIRC). The flags attribute markes the enum as a powers of 2 flag (under the hood it uses binary flags - 0001, 0010, 0100, 1000, etc)
I'm assuming by default the enum values are base 2 right?
I honestly dont remember the reasons for the restrictions
Actually you got me thinking and so i just tested this out. I was wrong, you would need to make the values of the enum into powers of 2. By default enums are start at 0 get assigned a value +1 greater than the previous. Has flag only works correctly if the values are non zero and power of two. The flags attribute can enforce that rule but HasFlag is part of the enum type and can be used (incorrectly) when the enum is not power of 2
So, that means the values have to be like
for
HasFlag()
to work properly? Weird, because I didn't increase the numeric value yet it works accordingly.Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.