943,829 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 1406
  • C# RSS
Sep 29th, 2009
0

is an Enum the right way to go?

Expand Post »
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:
C# Syntax (Toggle Plain Text)
  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:

C# Syntax (Toggle Plain Text)
  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 : /
Similar Threads
Reputation Points: 512
Solved Threads: 246
Nearly a Posting Virtuoso
Ryshad is offline Offline
1,260 posts
since Aug 2009
Sep 29th, 2009
0

Re: is an Enum the right way to go?

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.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 29th, 2009
2

Re: is an Enum the right way to go?

You can use enum but perhaps in this way:
c# Syntax (Toggle Plain Text)
  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.
Reputation Points: 2035
Solved Threads: 644
Senior Poster
ddanbe is offline Offline
3,738 posts
since Oct 2008
Sep 29th, 2009
0

Re: is an Enum the right way to go?

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
Reputation Points: 512
Solved Threads: 246
Nearly a Posting Virtuoso
Ryshad is offline Offline
1,260 posts
since Aug 2009
Sep 29th, 2009
0

Re: is an Enum the right way to go?

Click to Expand / Collapse  Quote originally posted by ddanbe ...
You can use enum but perhaps in this way:
c# Syntax (Toggle Plain Text)
  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
Reputation Points: 512
Solved Threads: 246
Nearly a Posting Virtuoso
Ryshad is offline Offline
1,260 posts
since Aug 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: How do you focus on text box in tab control on form load?
Next Thread in C# Forum Timeline: dataGridView.ClearSelection Except current cell





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC