Click here to Skip to main content
15,900,108 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Hi

I need to get the values based on the combination of two values i do not want to use if else statment or switch case Please advice as i am very new to this.

The sample data format is mentioned below

Combinations                         result
   0  1                       -->      x
   0  2                       -->      y
   0  3                       -->      z
   1  0                       -->      a
   2  3                       -->      d


Thanks

What I have tried:

i have tried switch case and used else if and if condition these conditions are making code huge as i have a large number of combinations and code unreadble.
Posted
Updated 6-Sep-16 1:34am
v2
Comments
Richard MacCutchan 6-Sep-16 4:47am    
Your question is unclear; exactly how are you using these values and what decisions do you need to make?
njammy 6-Sep-16 4:55am    
Where are these values stored? text file? database? constants?

To map the two integers into a character, you might use a Dictionary having a pair (a bidimensional Tuple) of integers as key.
 
Share this answer
 
There are a couple of problems here: the major one being that we have no idea what the range of values in your "combinations" could be: so we have no idea what kind of solution to propose. In addition, we don't know exactly what the result is: are those variable names? Values? Strings? We have no idea.
But I suspect that you are trying to avoid nesting your switch statements as they make code difficult.
So one solution might be:
C#
int comb1 = 1;
int comb2 = 0;
long combination = ((long)comb1) << 32 | comb2;
switch (combination)
    {
    case 0L << 32 | 1: result = x; break;
    case 0L << 32 | 2: result = y; break;
    case 0L << 32 | 3: result = z; break;
    case 1L << 32 | 0: result = a; break;
    case 2L << 32 | 3: result = d; break;
    default:
        // ...
        break;
    }
Which removes the nesting and reduces the code to manageable proportions.
 
Share this answer
 
Comments
Member 11699415 6-Sep-16 5:21am    
yes these are integer values and possbile values are

example For 0 there are n combinations
0,1
0,2
0,3
..... 0,15

i have for 4 total combinations
0,0 to 0,15
1,0 to 1,15
2,0 to 2,15
3,0 to 3,15
4,0 to 4,15
OriginalGriff 6-Sep-16 5:23am    
And what are the results? Variables or fixed values?
Patrice T 6-Sep-16 7:19am    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Member 11699415 6-Sep-16 5:24am    
fixed value got from enums
OriginalGriff 6-Sep-16 5:40am    
In which case, I'd probably set up a two dimensional array of enum values: then all you have to do is fetch the value:

MyEnum result = mappingTable[comb1, comb2];

Setting up the mapping table is a pain, but you can either do it in code:

private static MyEnum[5, 16] = {{MyEnum.x, MyEnum.y, ...

Or load values from a table in an external file.
Quote:
i have for 4, total combinations
0,0 to 0,15
1,0 to 1,15
2,0 to 2,15
3,0 to 3,15
4,0 to 4,15
As OriginalGriff said the easiest is to make this a 2D array of constants.
After that, in code, it just a matter of reading the array.
Try something like:
C#
int Table[][]={{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15},
               {16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31},
               ...};

and to read
C#
MyVar= Table[0,15];
 
Share this answer
 
Comments
Philippe Mori 6-Sep-16 8:58am    
Sometime it might make sense to do that but in a case like this it is much simpler to write a function that multiply one of the number by a power of 2 such that bits don't overlap like: x' = 16 * x1 + x2 where x1 would be the number between 0 and 4 inclusively and x2 the number between 0 and 15 inclusively.
Patrice T 6-Sep-16 9:53am    
I do both, depending on the data meaning and usage.
But we don't have much information about it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900