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:

``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:

``````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 : /

## All 4 Replies

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:

``````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.

commented: 00100100100101000010 +17
commented: straight in there with exactly what i needed...cheers again :) +1

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 :D

You can use enum but perhaps in this way:

``````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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.