Working an enum

K

K Viltersten

I've create a class Clock for entering
handling directions. It has twelve
values, 0 through 11, and methods for
jumping back and fort in time.

As it's now, i use a method for
controlling the pass over midnight.

void doJumpAhead ()
{
this.time++;
if (this.time > 11)
this.time = 0;
}

It just stroke me that i should be
able to use enum type. However, i've
seen no info on how to make

enum Direction : byte
{
One, Two, ... Twelve
}

to a circular entity. I believe, the
way it's shown above, it'll only keep
on increasing if i use "++" on it. Is
there a work-around?
 
G

Gilles Kohl [MVP]

I've create a class Clock for entering
handling directions. It has twelve
values, 0 through 11, and methods for
jumping back and fort in time.

As it's now, i use a method for
controlling the pass over midnight.

void doJumpAhead ()
{
this.time++;
if (this.time > 11)
this.time = 0;
}

You could use the modulus operator "%":

time = (time + 1) %12;

(would save the if statement)
It just stroke me that i should be
able to use enum type. However, i've
seen no info on how to make

enum Direction : byte
{
One, Two, ... Twelve
}

to a circular entity. I believe, the
way it's shown above, it'll only keep
on increasing if i use "++" on it. Is
there a work-around?

You could again use %, but as you can't use it on enums directly, some casting
is required, e.g.

time = (Direction)(((int)time + 1) % 12);

Regards,
Gilles.
 
K

K Viltersten

Gilles Kohl said:
You could use the modulus operator "%":

time = (time + 1) %12;

(would save the if statement)


You could again use %, but as you can't
use it on enums directly, some casting
is required, e.g.

time = (Direction)(((int)time + 1) % 12);

Yes, but i was looking for a sytax to
define the struct in such a way that the
"warping" is "already there".

Plus, i try to avoid modulo as plague due to
performance resons.
 

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

Top