Enum to int type cast

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
 
N

Nicholas Paldino [.NET/C# MVP]

Andrey,

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

int Priority = (int) op;

Hope this helps.
 
N

Nicholas Paldino [.NET/C# MVP]

Andrey,

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

int Priority = (int) Operator.op;
 
C

Chris A. R.

This thread needs a little bit of clarification ...

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

Chris A.R.
 
S

Stu Smith

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


Top