6

I have an enum type defined like so:

public enum Status { Active=1, InActive=0 } 

in my method can I cast the parameter into the enum like this:

public string doSomething(string param1, int status) { //will this work? Account.Status = (Status) status; //or do i need to test one by one like this if(status == 0) { Account.Status = Status.Inactive; //and so on... } // end if } // end doSomething 
7
  • 4
    Why not just run the code and find out. Commented Aug 22, 2013 at 14:10
  • @Kaf Possible reason: he's using a database that supports int, but not enum. Commented Aug 22, 2013 at 14:14
  • Yes, it will work. However, It's best to check if int is in the range of declared enum first. Commented Aug 22, 2013 at 14:16
  • i ran the code...the compiler didnt complain.what happens when a value thats not in the enum is passed in? Commented Aug 22, 2013 at 14:17
  • 1
    @sirbombay you should be able to try that yourself quite easy... Commented Aug 22, 2013 at 14:20

3 Answers 3

2

Yes, you can do a straight cast from int to enum (given that an enum representation of that integer exists).

If you need to check whether the enum exists before parsing use Enum.IsDefined i.e.

if (Enum.IsDefined(typeof(Status), status)) { Account.Status = (Status)status; } 
Sign up to request clarification or add additional context in comments.

2 Comments

what happens when it doesnt exist?
The variable being assigned reverts to int - both types are interchangeable so you won't get an exception.
1

Just check if the int is a valid value for Status and then do a conversion.

public string doSomething(string param1, int status) { if (IsValidEnum<Status>(status)) { Account.Status = (Status)status; } ... } private bool IsValidEnum<T>(int value) { var validValues = Enum.GetValues(typeof(T)); var validIntValues = validValues.Cast<int>(); return validIntValues.Any(v => v == value); } 

In de else of the if you can throw an exception if you wish.

3 Comments

i guess this is the magic i was looking for...thanks
Enum.IsDefined already does this - see my answer.
@James, Ok, it was fun to create the method but unneseccary...
1

well of course you can do that. just try and see.

you can also cast the other way

(int)Account.Status 

it's possible to cast Enum to int and vice versa, because every Enum is actually represented by an int per default. You should manually specify member values. By default it starts from 0 to N.

if you try to convert an enum value that does not exist it'll work, but wont give you an enum value if you try to compare it the any value you have in the enum

4 Comments

I actually tried it..it compiled fine.but my worry is what happens when a value is passed in that doesnt exist in the enum?
@sirbombay You can try that 2 :)
@sirbombay read my edited answer
it now makes sense...i guess i shd find that out myself too :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.