how to use a value of enmu to do Mathematics operation with int type ? ( In Javascript )

help!! how to use a value of enmu to do Mathematics operation with int type ? ( In Javascript ) like:

enum aaa { ONE, TWO, Three}

var i = 5 ; var j = 6 ; var sum = i + j * aaa.TWO;

but it's wrong~

Error message is BCE0051: Operator '*' cannot be used with a left hand side of type 'int' and a right hand side of type 'aaa'.

2 Answers

2

Use parseInt() to convert an enum to its integer value.

Firstly, Enums start from 0, so calling it ONE will still yield 0 as it's value. To start your Enum incrementing from 1 you would have to define your Enum like so:

enum aaa
{
   ONE = 1,
   TWO,
   THREE
};

As for doing maths with them, you will need to cast it to an int like this:

EDIT: Corrected cast for JavaScript.

var sum = i + j * parseInt(aaa.TWO);

This will work for how you want to use it. Though I'm curious to ask why you're doing this?

You can't cast like that in JS.

Oh damn it. My bad, thanks. I will update my post.