Reputation: 39
I have this method
public enum Values
{
True= true,
False=false
};
public static string GetValues(bool values)
{
string status = "";
switch (values)
{
case(bool)UIHelper.Values.False:
}
}
I want to have this enum
as boolean
. It says, that:
Values cannot be casted as bool.
How can I do it so I can have it boolean
?
Upvotes: 3
Views: 34150
Reputation: 186668
If you have to stick to enum
you can implement an extension method:
public enum Values {
True,
False,
// and, probably, some other options
};
public static class ValuesExtensions {
public static bool ToBoolean(this Values value) {
// which options should be treated as "true" ones
return value == Values.False;
}
}
...
// you, probably want to check if UIHelper.Values is the same as values
if (values == UIHelper.Values.ToBoolean()) {
...
}
Upvotes: 3
Reputation: 2608
Use 0
for false
and 1
for true
instead combined with Convert.ToBoolean
true if value is not zero; otherwise, false.
public enum UnlPointValues
{
Serial = 1, //true
Service = 0 //false
};
public static string GetUnloadingPointValues(bool values)
{
string status = "";
switch (values)
{
case (Convert.ToBoolean((int)UIHelper.UnlPointValues.Serial)):
break;
case (Convert.ToBoolean((int)UIHelper.UnlPointValues.Service)):
break;
}
}
Upvotes: 0
Reputation: 32266
I don't see that you need an enum
here.
public static string GetUnloadingPointValues(bool isSerial)
{
return isSerial ? "Serial" : "Service";
}
Or whatever string
values you want to map.
Upvotes: 1
Reputation: 156918
Of course, you can map to 0 (Service
) and 1 (Serial
), but why map to that in the first place? Why not use bool from the start?
public static class UnlPointValues
{
public const bool Serial = true;
public const bool Service = false;
}
public static string GetUnloadingPointValues(bool values)
{
string status = "";
switch (values)
{
case UIHelper.UnlPointValues.Serial:
}
}
Upvotes: 7