Boolean expression involving Enum and Int [duplicate] - c#

This question already has answers here:
How to compare enum and int values?
(7 answers)
What's the simplest way to compare an enum to a integer value return from a DB
(5 answers)
C# Enum - How to Compare Value
(5 answers)
Closed 5 years ago.
I have a simple enum with a few values
Enum Status{
Available,
Taken,
Sold//and many more
}
I want to check whether the status is somewhere in between the possible values(assuming the values are written in such an order that the lowest are the beginning of a process, and the last ones are the final steps of the process)
Something like
Status s1;//Some value
if(s1<5 && s1>3)
//The value is one of the enum values in those range
//(for example the job has already been accepted, but has still not been
//shipped)
Is it possible?
Thanks.

You can assign integer values to your enum and then check.
enum Status
{
Available = 1,
Taken = 2,
Sold = 3
//and many more
}
Status s1; // any value
if ((int)s1 <= 5 && (int)s1 >= 3)

Related

How can I convert a string to an int array in C#? [duplicate]

This question already has answers here:
Convert string numbers to Array C#
(3 answers)
Closed 2 years ago.
for example suppose that I have a string like "231143" and I wanna convert it to an int[] array which I can easily access 2,3,1,1,4,3 as an int. What should I do?
Without any error checking you can do:
var value = "231143";
var array = value.Select(c => c - '0').ToArray();
This makes use of a trick whereby you can subtract '0' from the value of a single character holding a number to get its integer value.

How can C# Enums be improved [duplicate]

This question already has answers here:
Validate Enum Values
(12 answers)
Closed 6 years ago.
If I create an enum like this
public enum ImportType
{
Direct,
Indirect,
InBond
}
and I have a method that takes an ImportType as parameter as follows
public bool ProcessValidImport(ImportType type)
{
// Process ImportType variable here
}
I can go and call the method as follows
bool blnProcessed = ProcessValidImport((ImportType)7);
But the ImportType variable value of 7 passed in to the method is not valid at all because any integer will work if cast. The enum defaults to an int type, so what is the best way to validate that an enum in this case is in fact a valid ImportType.
I don't know if I understood you correctly but you can validate enum easily using:
int value = 7;
bool isDefined = Enum.IsDefined(typeof (ImportType), value);

How to determine the no of enum items declared in c# [duplicate]

This question already has answers here:
Total number of items defined in an enum
(10 answers)
Closed 8 years ago.
I need to know the number of enums that have been declared.
So far I am using the following which works but I wonder if there is a better way?
enum MyEnum
{
foo = 1,
bar = 2
}
int noOfEnums = Enum.GetNames(typeof(MyEnum)).Count();
noOfEnums will be 2;
You may try to use:
enum MyEnum
{
foo = 1,
bar = 2
}
var noOfEnums = Enum.GetNames(typeof(MyEnum)).Length;
The length property of this array equals the number of items defined in the enum
Also check Count Items in a C# Enum

Test that only a single bit is set in Flags Enum [duplicate]

This question already has answers here:
How do I check if more than one enum flag is set?
(5 answers)
Closed 2 years ago.
So I have a flags Enum
public Enum test
{
test1 = 1,
test2 = 2,
test3 = 4,
etc.
}
How can I test that one bit, and only one bit is set?
I've 100% done this before but my mind is not working this am!
To check that only a single bit is set in a number, the number must (by definition) be a power of two. As such, you can use the following to test:
int intVal = ((int)myEnumFlags);
bool singleBitIsSet = intVal != 0 && (intVal & (intVal-1)) == 0;
My favorite reference for this kind of thing:
http://aggregate.org/MAGIC

Getting enumerate value [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Enums returning int value
I want to numerate "Stack", "Overflow" and "Stackoverflow". Therefore I use enumerate.
enum Status : int
{
Stack = 1,
Overflow = 2,
StackOverflow = 3
}
But when I want to get value of Stack, I get "Stack".
int status = 0;
status = Status.Stack; //I want to assign 1 to status but Status.Stack returns "Stack"
How can I do it?
Just cast to int:
status = (int) Status.Stack;

Categories