Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Working an enum

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
K Viltersten - 19 Mar 2008 05:56 GMT
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?

--
Regards
Konrad Viltersten
--------------------------------
sleep    - a substitute for coffee for the poor
ambition - lack of sense to be lazy
Gilles Kohl [MVP] - 19 Mar 2008 07:42 GMT
>I've create a class Clock for entering
>handling directions. It has twelve
[quoted text clipped - 10 lines]
>      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
[quoted text clipped - 9 lines]
>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 Viltersten - 19 Mar 2008 08:11 GMT
>>I've create a class Clock for entering
>>handling directions. It has twelve
[quoted text clipped - 36 lines]
>
>   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.

--
Regards
Konrad Viltersten
--------------------------------
sleep    - a substitute for coffee for the poor
ambition - lack of sense to be lazy
Peter Duniho - 19 Mar 2008 08:42 GMT
> Yes, but i was looking for a sytax to
> define the struct in such a way that the
> "warping" is "already there".

I don't really get the question.  Is it a requirement that you use the  
++ syntax?  If so, then you can't do what you are asking, not without  
special code like you and Gilles have posted.  Enums in C# are really  
little more than a wrapper around some integral type other than char, and  
so they pretty much only do the things you could do with an integral type.

If you can use alternative syntax, I suppose one solution would be to  
provide a mapping array:

    Direction[] dirNext = { Two, Three, Four, Five, Six, Seven
        Seven, Eight, Nine, Ten, Eleven, Twelve, One };

Then the next Direction is just:

    Direction dirCur = Direction.One;

    dirCur = dirNext[(int)dirCur];

(I'm sitting here wondering if extension methods word on enums...if so,  
you could even make an extension method so that the code actually looked  
something like: "dirCur = dirCur.Next()", putting the mapping array lookup  
into the "Next()" extension method)

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

Now _that_ I think isn't sensible.  I doubt that modulo is all that  
expensive anyway as compared to the conditional (it certainly has the  
advantage of avoiding branching, so there's no branch-prediction to go  
wrong), and even if it were, your code would have to be doing basically  
_only_ modulo for that to be a concern.

Pete

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.