Getting enumerate value [duplicate] - c#

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;

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.

I can't properly convert string to Int in C# [duplicate]

This question already has answers here:
C# converting string to int goes wrong
(5 answers)
Closed 3 years ago.
The type conversion doesn't function as expected
I broke down the problem but it still occurs
string a = "123";
int i = Convert.ToInt32(a[0]);
Console.WriteLine(i);
I expect the result of 1 but I get 49. I can't imagine how.
When you do the indexer a[0] you get a char which for 1 is char code number 49. Do Convert.ToInt32(a[0].ToString()) or subtract 48 from the result you get instead to get the numerical representation.

Boolean expression involving Enum and Int [duplicate]

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)

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

Categories