Get values from an Enum class and perform checking
Hi, I am trying to get the values from an enum and perform a conditioning checking with the values retrieved. I was told that the way I am trying to check is fine but the way I am checking it is incorrect. Here's an example of what I am trying to get. It was hinted that the if statement is incorrect.
15 Replies
.Equals here will always return false
An array of integers will never be equal to an enum value
ya, he told me because I am comparing an enum with an array of enum, so it'll always return false. But then after he took a closer look, he said it's fine, but the way I am checking using this method is wrong
Can't seem to find out why. I've refactored the code using switch-case as well
I found that getting the values of enum returns me integer
Yes, you'll want to check if the array contains the enum value
is the concern here that
bike.BikeType
may not actually be a value in the range of the BikeType
enum?No, the values the same.
then what are you checking?
or attempting to check?
I'm checking for a value inside the enum class, like "Road" here for example. So if "BikeType" is equals to "Road", then I'll do something
but ofc, the way I am checking this is wrong cuz it's checking for eveyrthing
yeah you definitely just want a switch here
Compared to using .Parse(), switch is better?
Parse has nothing to do with this
Hmm, I was thinking to have it check within the if statement
check what?
Like
if (ModelState.IsValid && bike.BikeType != BikeType.Road && bike.File != null)
sure
that can work too
Ohh man, my bad. I actually want to check for both values, hence the use of .Equals() to check if the values are not equals to the values contained in BikeType
Okay, I can't find any solution to get values out from enum without converting it into a list or loop it.