Working an enum

  • Thread starter Thread starter K Viltersten
  • Start date Start date
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?
 
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.
 
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.
 
Back
Top