is an Enum the right way to go?

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2009
Posts: 331
Reputation: Ryshad has a spectacular aura about Ryshad has a spectacular aura about 
Solved Threads: 61
Ryshad's Avatar
Ryshad Ryshad is offline Offline
Posting Whiz

is an Enum the right way to go?

 
0
  #1
Sep 29th, 2009
I'm currently re-writing a bit of code and i'm in two minds about which way to take it. Advice would be greatly appreciated

The application is used to control a USB circuit board. The board has 32 analog outputs. The outputs are switched on and off by sending a 32bit integer to the board. So to turn on outputs 2 and 4 you send it the number 10 (10= 0101) or to turn on 1,2 and 3 you send 7 (1110) etc.
This part is outside my control.

I currently set the outputs usign a simple math equation:
  1. Output1 = (int)(Math.Pow(2, Convert.ToDouble(Output) - 1));

Each analog output controls a single solenoid. The problem, is that every 8th output controls an LED on the board itself so isn't used, and due to space limitations i have had to skip a couple of the outputs in the middle.
The result: turning on solenoid number 17 requires activating output number 20.

I want the user to be able to enter the number of the solenoid, since the output numbers are hard to figure out unless you know how they are wired.

Sorry for the loooong background. But heres the question:

I am writing the following enum:

  1. public enum Output
  2. {
  3. _1 = 0,
  4. _2 = 1,
  5. _3 = 2,
  6. _4 = 3,
  7. _5 = 4,
  8. _6 = 5,
  9. _7 = 6,
  10. _8 = 8,
  11. _9 = 9,
  12. _10 = 10,
  13. _11 = 11,
  14. _12 = 12,
  15. _13 = 16,
  16. _14 = 17,
  17. _15 = 18,
  18. _16 = 19,
  19. _17 = 20,
  20. _18 = 21,
  21. _19 = 22,
  22. _20 = 24,
  23. _21 = 25,
  24. _22 = 26,
  25. _23 = 27,
  26. _24 = 28
  27. }

I was then going to populate a combobox with each enum option.
So when they pick Output._17 i can use (int)Output._17 to get the correct output.

Am i coming at this all wrong? Something feels a little Kludge-like about the enum : /
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,200
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: is an Enum the right way to go?

 
0
  #2
Sep 29th, 2009
No, that is a perfectly acceptable (and good) way to go about it. Its the easiest way to do a strongly-type mapping like you require in this case.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,923
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 276
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: is an Enum the right way to go?

 
2
  #3
Sep 29th, 2009
You can use enum but perhaps in this way:
  1. public enum Output
  2. {
  3. _0 = 1,
  4. _1 = 2,
  5. _2 = 4,
  6. _3 = 8,
  7. _4 = 16,
  8. _5 = 32, ....
  9.  
  10. }

This way you can set port 1 and 2 on like this ;
SetIt = _1 + _2; which is 0110 binary
Don't have to use Math.Pow that way.
Last edited by ddanbe; Sep 29th, 2009 at 12:37 pm.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 331
Reputation: Ryshad has a spectacular aura about Ryshad has a spectacular aura about 
Solved Threads: 61
Ryshad's Avatar
Ryshad Ryshad is offline Offline
Posting Whiz

Re: is an Enum the right way to go?

 
0
  #4
Sep 29th, 2009
cheers, just needed to be sure :p

This happens from time to time...i think it all through but something in my brain goes..."doesnt look right"....its the same part that convinces you a word isnt right even though you know you've spelt it correctly lol

Finished coding it and it all runs smoothly, and its saved me the hastle of trapping keypresses and validating input to esnure the user entered valid numeric value for output
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.

"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 331
Reputation: Ryshad has a spectacular aura about Ryshad has a spectacular aura about 
Solved Threads: 61
Ryshad's Avatar
Ryshad Ryshad is offline Offline
Posting Whiz

Re: is an Enum the right way to go?

 
0
  #5
Sep 29th, 2009
Originally Posted by ddanbe View Post
You can use enum but perhaps in this way:
  1. public enum Output
  2. {
  3. _0 = 0,
  4. _1 = 2,
  5. _2 = 4,
  6. _3 = 8,
  7. _4 = 16,
  8. _5 = 32, ....
  9.  
  10. }

This way you can set port 1 and 2 on like this ;
SetIt = _1 + _2; which is 0110 binary
Don't have to use Math.Pow that way.
I had spotted that :p Its on my to-do list...wanted to be sure the Enum was the right direction before i put too much time into it.
Thanks for this though, i was still trying to get it straight in my head that adding the two eresults worked the same as adding them before the math.pow. Now to be really lazy and write a couple of lines to print out the values...no way i wanna sit with a calculator and do them by hand haha
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.

"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
Reply With Quote Quick reply to this message  
Reply

Tags
enum, math, usb

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC