tags:

views:

198

answers:

6

My situation is that I need to get a single value that defines the combination of multiple selection options. Here is a small example of what I'm looking to do.

A user is buying a shirt. There are many different options the user can select when buying the shirt. Only one option can (and must) be selected from each selection group. The selection groups could be represented as a Radio Button List or Dropdown List, anythign that only allows one selection for each group. For example:

Shirt Color: Red, Blue, Green, Black, White, or Yellow
Shirt Size: X-Small, Small, Medium, Large, X-Large, XX-Large
Shirt Design: Horizontal Striped, Vertical Stripes, Polka Dots, Diagonal Stripes, No Design
Shirt Type: Button Up Shirt, Polo Shirt, T-Shirt, Long Sleeve T-Shirt, Sweatshirt

So only one option can be selected from each group for the sake of this problem. I get a total of 900 different variations (6x6x5x5) using the possible selections. Given that the following options have the following values:

Red = 1, Blue = 2, Green = 3, Black = 4, White = 5, Yellow = 6
X-Small = 0, Small = 1, Medium = 2, Large = 3, X-Large = 4, XX-Large = 5
Horizontal Striped = 0, Vertical Stripes = 1, Polka Dots = 2, Diagonal Stripes = 3, No Design = 4
Button Up Shirt = 0, Polo Shirt = 1, T-Shirt = 2, Long Sleeve T-Shirt = 3, Sweatshirt = 4

and that a selection of {Red, X-Small, Horizontal Striped, Button Up Shirt} = 1 and {Blue, X-Small, Horizontal Striped, Button Up Shirt} = 2, etc,

What is the correct way to find the value based on the selections made? For example what should the selection value of {Black, Large, Horizontal Striped, T-Shirt} be and how do you come to the solution? I can't quite put my finger on it and everything I've tried seems to have been unsuccessful. It's probably a simple solution and I'm just brain dead right now.

A: 

Do you really need a single, numerical value for each combination of options? Or do you just need some way to group the user's selection into a known unit?

If it's the later, I'd recommend creating a Shirt class, with properties for each selection type (Color, Size, Design, and Type). You could even have these be enum values since there are a limited number of values for each property.

Once the user's selection has been fully made, you can check the values of each property of the instance of Shirt to know how to put the final shirt together.

Andy
Yes unfortunately I do need a single numerical value for each combination. I understand it doesn't make a lot of sense to do in the situation I described but its a very condensed example to illustrate what I'm looking for.
Since one number is required, I'd probably go with Matthew Jones's answer, then.
Andy
A: 

Does it absolutely have to be a single value? A collection of enums strikes me as the most logical choice:

ShirtColor Color;
ShirtSize Size;
ShirtPattern Pattern;
ShirtType Type;

enum ShirtColor {Red, Blue, Green, Black, White, Yellow};
...

If it's got to be a single value, you might pick an Int32, and use each flags in each byte of that int for each of the four pieces of information you want to store. Use a bitmask for each of the characteristics you're extracting. Something like:

enum ShirtColor : byte {Red=0, Blue=1};
enum ShirtSize : byte {Small=0, Large=1};

int shirtInfo = 0x0001;

int colorOffset = 0;
int sizeOffset = 8;

ShirtColor color = (ShirtColor)(shirtInfo << colorOffset);
ShirtSize size = (ShirtSize)(shirtInfo << sizeOffset);

Console.WriteLine(Enum.GetName(typeof(ShirtColor), color));
Console.WriteLine(Enum.GetName(typeof(ShirtSize), size));
Michael Petrotta
+5  A: 

You could concatenate the values of the options like they were strings (by converting each individual value to a string, concatenating them, then converting that to an integer). So, {Black, Large, Horizontal Striped, T-Shirt} would be 4302. This should guarantee that no two possible combinations have the same identifier (though I am no math whiz).

Matthew Jones
yes, that's the way to go for what OP wants, this is used frequently to encapsulate multiple binary options into one value :-)
tekBlues
I knew there was a simple answer. This should do nicely. Thanks!
Didn't think of that. In my situation I don't have any option that goes past 9 so I don't think it will be a problem. Still its good to know the limitations.
if there was a chance for more than 10 options for any of the fields you could just use two digit fields ( and remember to put a 0 in front of a 1 digit selection )
John Boker
Woops, accidentally deleted my comment that the last reply was addressing. I had stated that the solution works fine as long as none of the items can be represented with double digits. So if Charcoal = 10, you get 10302, and there's no delimiter to tell where the next category begins (such as comma separated values). That may cause an overlap and no longer yield unique numbers.
Ahmad Mageed
@John: yep, that would address it!
Ahmad Mageed
+1 for simple solution.
AllenG
+1  A: 

In my mind, your scenario maps to a Shirt class with 4 enums - Color, Size, Design and Type. The class has public properties which expose private members of the type of all 4 enums.

Therefore, One shirt instance can have all 4 properties set without having to worry about 900 possible combinations. Additionally, one single Shirt instance will only have one possible combination of each of the properties. You can query the types of values set in a intuitive manner without having to remember what integer value maps to what type of property. This is precisely what Enums are for! ;-)

Cerebrus
A: 

One way is to assign number ranges of different magnitudes to the different types of information:

Red = 1, Blue = 2, Green = 3, Black = 4, White = 5, Yellow = 6
X-Small = 100, Small = 200, Medium = 300, Large = 400, X-Large = 500, XX-Large = 600
Horizontal Striped = 1000, Vertical Stripes = 2000, Polka Dots = 3000, Diagonal Stripes = 4000, No Design = 5000
Button Up Shirt = 10000, Polo Shirt = 20000, T-Shirt = 30000, Long Sleeve T-Shirt = 40000, Sweatshirt = 50000

Which would mean that 42304 is a Black Long Sleeve T-Shirt with vertical stripes in size medium.

Personally I would probably store and treat the different properties and separate values unless there were specific requirements prohibiting that.

Fredrik Mörk
A: 

Shirt Color: Red, Blue, Green, Black, White, or Yellow Shirt Size: X-Small, Small, Medium, Large, X-Large, XX-Large Shirt Design: Horizontal Striped, Vertical Stripes, Polka Dots, Diagonal Stripes, No Design Shirt Type: Button Up Shirt, Polo Shirt, T-Shirt, Long Sleeve T-Shirt, Sweatshirt

Note here you have 4 categories, and each category have a handful values. You can use a 4-byte integer and each byte for a category.

Then {Black, Large, Horizontal Striped, T-Shirt} could be the value (4 + 4*2^8 + 1*2^16 + 3*2^24).

Just don't think the value is tightly packed, and you will solve it naturally.