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.
using System;

namespace MyApplication
{
class Program
{
enum BikeType
{
Road,
Mountain
}
static void Main(string[] args)
{
var bikeTypeValues = Enum.GetValues(typeof(BikeType));

if (ModelState.IsValid && bike.BikeType.Equals(bikeTypeValues) && bike.File != null) {
// code here
}

Console.WriteLine(bikeTypeValues);
}
}
}
using System;

namespace MyApplication
{
class Program
{
enum BikeType
{
Road,
Mountain
}
static void Main(string[] args)
{
var bikeTypeValues = Enum.GetValues(typeof(BikeType));

if (ModelState.IsValid && bike.BikeType.Equals(bikeTypeValues) && bike.File != null) {
// code here
}

Console.WriteLine(bikeTypeValues);
}
}
}
15 Replies
Thinker
Thinker3y ago
.Equals here will always return false An array of integers will never be equal to an enum value
CoreVisional
CoreVisionalOP3y ago
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
Thinker
Thinker3y ago
Yes, you'll want to check if the array contains the enum value
ero
ero3y ago
is the concern here that bike.BikeType may not actually be a value in the range of the BikeType enum?
CoreVisional
CoreVisionalOP3y ago
No, the values the same.
ero
ero3y ago
then what are you checking? or attempting to check?
CoreVisional
CoreVisionalOP3y ago
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
ero
ero3y ago
yeah you definitely just want a switch here
CoreVisional
CoreVisionalOP3y ago
Compared to using .Parse(), switch is better?
ero
ero3y ago
if (ModelState.IsValid && bike.File is not null)
{
switch (bike.BikeType)
{
case BikeType.Road:
{
// ...
break;
}
// ...
}
}
if (ModelState.IsValid && bike.File is not null)
{
switch (bike.BikeType)
{
case BikeType.Road:
{
// ...
break;
}
// ...
}
}
Parse has nothing to do with this
CoreVisional
CoreVisionalOP3y ago
Hmm, I was thinking to have it check within the if statement
ero
ero3y ago
check what?
CoreVisional
CoreVisionalOP3y ago
Like if (ModelState.IsValid && bike.BikeType != BikeType.Road && bike.File != null)
ero
ero3y ago
sure that can work too
CoreVisional
CoreVisionalOP3y ago
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.
Want results from more Discord servers?
Add your server