| | |
is an Enum the right way to go?
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
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:
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:
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 : /

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)
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)
public enum Output { _1 = 0, _2 = 1, _3 = 2, _4 = 3, _5 = 4, _6 = 5, _7 = 6, _8 = 8, _9 = 9, _10 = 10, _11 = 11, _12 = 12, _13 = 16, _14 = 17, _15 = 18, _16 = 19, _17 = 20, _18 = 21, _19 = 22, _20 = 24, _21 = 25, _22 = 26, _23 = 27, _24 = 28 }
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 : /
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.
You can use enum but perhaps in this way:
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.
c# Syntax (Toggle Plain Text)
public enum Output { _0 = 1, _1 = 2, _2 = 4, _3 = 8, _4 = 16, _5 = 32, .... }
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
Make love, no war. Cave ab homine unius libri.
Danny
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
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
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
•
•
•
•
You can use enum but perhaps in this way:
c# Syntax (Toggle Plain Text)
public enum Output { _0 = 0, _1 = 2, _2 = 4, _3 = 8, _4 = 16, _5 = 32, .... }
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.
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
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
![]() |
Similar Threads
- Enum issue (C#)
- C++ Help with enum and cin (C++)
- struct array and enum problems (C++)
- Enum, struct, and pointer? (C)
Other Threads in the C# Forum
- Previous Thread: How do you focus on text box in tab control on form load?
- Next Thread: dataGridView.ClearSelection Except current cell
| Thread Tools | Search this Thread |
algorithm angle apple array australia bitmap c# c++ cd conversion cuil dataloss datatheft decimal degrees development device drawing enum equation firewire form function gadget gdi+ google hack hacking hardware ieee insider intel javascript mac macbooks mandelbrot math mathematics memorykey messaging method news numbers operator pc phone picturebox plotting polynomial prime primenumbersinrange radians recursive removabledevices round search security sms statistics technology time usb vb vbnet voicerecorder web wireless wolframalpha







