-5

I have this enum

public enum EstadoPeticion : int
{
    PeticionCreada = 1,
    PorValidar = 2,
    Validada = 3,
    Error = 0      
}

Using the enum I have to do

(int)EstadoPeticion.PeticionCreada

Why I have to cast type and How can I create an enum type without use cast ?, I think cast could be a bad practice

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • 3
    *“I have to do”* – To do what? You certainly don’t have to cast enums down to ints when used properly. – poke Aug 01 '17 at 16:29
  • Why do you have to do that? Why do you think it is a bad practice? – Jon Hanna Aug 01 '17 at 16:31
  • If I want to do something like: IdEstadoPeticion =EstadoPeticion.PeticionCreada it's always necessary cast IdEstadoPeticion = **(int)**EstadoPeticion.PeticionCreada, – Fabian Rodriguez Aug 01 '17 at 16:33
  • We need more context. What is IdEstadoPeticion? Is it a local variable or a type's property or field? What's its declared type? Can it be changed to a type of EstadoPeticion? – TylerBrinkley Aug 01 '17 at 16:43
  • an example, int value = (int)EstadoPeticion.PeticionCreada, 'value' is stored in database as int – Fabian Rodriguez Aug 01 '17 at 16:45
  • In that case since it's stored in a database as an int and you don't have an ORM setup to convert between the two then you'll need to cast. Casting isn't such a bad thing. If you'd rather call a method you could call Convert.ToInt32(EstadoPeticion.PeticionCreada) instead. – TylerBrinkley Aug 01 '17 at 16:51

3 Answers3

1

There's nothing wrong with casting (it's not bad practice). If you want to use an enum as an integer, you must always cast, that's just how enums work. Although you should not necessarily have to cast enums, because you should mostly compare enums amongst each other.


Like this for example:

if (myenum == PeticionCreada) {

}
travisjayday
  • 784
  • 6
  • 16
1

Casting is not directly a bad practice. It is a smell... a sign there might be a potential bad practice somewhere nearby.

In this case, the potential bad practice is comparing an Enum with an int at all. That's often (not always) a sign you're doing something else wrong with your design, that the int value should perhaps also be an Enum, or you should be using a different mechanism to represent this data.

And, again, it might be just fine.

One example where this is perfectly okay is working with a database, where you need to store an Enum in a database table as an int. You'll cast Enum to int when saving the data, and cast int to Enum when retrieving it. And in these situations a cast is both appropriate and required.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
0

You don't need to cast enums. If you want the Error to be 0, you can do this:

public enum EstadoPeticion    
{
  Error = 0,
  PeticionCreada,
  PorValidar,
  Validada   
}

The rest of the values will increment by 1 automatically.

Devin L.
  • 437
  • 2
  • 12
  • but if I want to assign this value some int it's always necessary use a cast ? – Fabian Rodriguez Aug 01 '17 at 16:41
  • If you want to use the integer value associated with an enum member, then you do have to cast. However, you don't need the ": int" part in your enum declaration. You can refer to here about casting: https://stackoverflow.com/questions/943398/get-int-value-from-enum-in-c-sharp – Devin L. Aug 01 '17 at 16:45