tags:

views:

242

answers:

5

I am writing a piece of code that to work would require an extensive amount of if/then statements. In order to eliminate the need for writing line upon line of if/then statements can I use a dictionary or a list? If so can someone direct me to a good web resource or show an instance where they have done it before?

Edit

Clarification: I have six inputs, each are to be combo boxes with a group of selections. Below is a detail of the inputs and the selections.

(Amps) 1:1 - 1:12 (12 different selections)

(Cable Size) 2:1 - 2:13 (13 different selections) Certain items in this list will be excluded by the selection of the first input.

(Cable Type) 3:1 - 3:2 (2 different selections)

(Temp Rating) 4:1 - 4:3 (3 different selections)

(System Type) 5:1 - 5:2 (2 different selections)

(Conduit Type) 6:1 - 6:2 (2 different selections)

From the above input will come two outputs which will appear in two text boxes.

(Cable Qty) 7:1 - 7:16 (16 different outputs)

(Conduit Size) 8:1 - 8:8 (8 different outputs)

I hope this serves to help and not hinder.

+2  A: 

might want to give some idea of what you're doing with the if/the statements. If you're just obtaining a value from a key then, yes, a dictionary probably would work.

Dictionary<string,string> map = new Dictionary<string,string>();

... populate the map with keys...

Then use it...

string value = "default value";
if (map.ContainsKey(key))
{
   value = map[key];
}
tvanfosson
+1  A: 

If you are always just obtaining a simple key-value lookup, then yes a dictionary lookup can replace a long string of if-then statements. However, if sometimes your logic is more complex than a simple key-value lookup, you may have to create a hybrid of if-then statements plus dictionary lookups. Sometimes these will be combined to make one logical statement.

The only correct answer in your case is to follow exactly what your business domain dictates. If you can simplify to dictionary lookups most of the time, then use them. Don't be too rigid to choose one over the other, though. Usually business logic is too messy to fall neatly into place like that.

Chad Braun-Duin
+2  A: 

I actually suggest building a object model to store your settings. This will give you an opportunity to encapsulate your logic regarding what options are available at what times. Another benefit is that your Amp[1] control can bind to your SettingsContainer.Amp[1].Value, or however it ends up.

DavGarcia
I have started an excel sheet for this.
tejas_grande
What exactly are you doing in Excel? Are you documenting your object model in Excel? Or using Excel for persistence?
DavGarcia
I used the spread sheet for the object model.
tejas_grande
+1  A: 

A dictionary look up is possible but I don't believe it is feasible with the problem you have described.

David Thomas Garcia has given a good solution to your problem. I like that solution because it makes a nice encapsulation in a business object that you could possibly reuse and I would expect it to simplify maintenance/debugging for you as well.

Have the object model expose default lists for each list of choices, then as each choice is selected, have the lower levels of choices automatically filter.

Sam
+2  A: 

It looks like you're trying to map each combination of the 6 inputs (12 * 13 * 2 * 3 * 2 * 2 possibilities) to one of the (16 * 8) outputs. If that's the case, you'll still have a lot of typing to do - but moving to a collection will allow you to easily externalize the mapping. I would guess that this would probably be best suited for a database table:

Amps | CableSize | CableType | TempRating | SystemType | ConduitType | CableQty | ConduitSize

You'd put a primary key on the 6 input columns, and then just do a simple SELECT:

SELECT CableQty, ConduitSize 
FROM Table 
WHERE Amps = @amps AND CableSize = @cableSize...etc

To do this in quick and dirty code, arrays would work:

const int AMPS = 0; const int CABLE_SIZE = 1; const int TEMP_RATING = 2; // etc.
var mappings = new Dictionary<int[], int[]>(12 * 13 * 2 * 3 * 2 * 2);
mappings.Add(
   new int[] { 1, 1, 1, 1, 1, 1 }, // inputs
   new int[] { 1, 2 } //outputs
);
// repeat...a lot

var outputs = mappings.First(inputs => {
   inputs[AMPS] == myAmps
   && inputs[CABLE_SIZE] == myCableSize
   && inputs[TEMP_RATING] == myTempRating
   && // etc
});

It doesn't save you much typing - though you could use for loops and the like to populate the mappings if there's some sort of logic to it - but it's a hell of a lot more readable than 6 pages of if statements (I'd probably region off or partial class loading the mappings).

Mark Brackett
The database table; is that an external file? That's not something I would use a .mdb file would I?
tejas_grande
Yes, an MDB would work for that. That'd allow non programmers (presumably, the folks who come up with these mappings) to change the mappings without changing code.
Mark Brackett
Ok, I think I have a clear direction, and that's what I wanted. I welcome any other comments or suggestions. I will update this thread as I try to implement a solution. Thanks.
tejas_grande
Mark, the array you show, is that a multidimensional array? I have been doing some digging into this subject a little more over the past few days. I worked up an excel spread sheet that shows a complete map of the possibilites
tejas_grande