Enum to int type cast

  • Thread starter Thread starter MuZZy
  • Start date Start date
M

MuZZy

Hi again,

Can anybody help me figure out how do i cast from enum to int?

public enum Operator {opPlus=1, opMinus=2, opDivide=3,...};

Operator op = opPlus;
int Priority = op; // It gives error

So if i want to assign a numeric value of an enum variable to an integer
variable ?

Thnak you,
Andrey
 
Andrey,

If you want to do this, you need to cast it explicitly, like so:

int Priority = (int) op;

Hope this helps.
 
Andrey,

Oops, forgot something, you need to declare the enumeration type as
well. This should read:

int Priority = (int) Operator.op;
 
This thread needs a little bit of clarification ...

Operator op = Operator.opPlus;
int Priority = (int)op;

Chris A.R.
 
If all you're using the enum for is integer constants, you may want to
consider constant integers:

const int opPlus = 1;

No cast required.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Enum type def 1
enum with underlying type 1
enum is int 2
Problem with casting integer values to enum 4
Enum Extentions 7
Enums and casting 4
Enums & Constructors? 4
Q: Why casting an enum? 15

Back
Top