Click here to Skip to main content
15,793,535 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi To All,
I have five fields like age,agetype,payment,country,working all fields contains only 0 or 1 or 2. above fields i need to check this three combination if i put if else i need to write 50 more if else condition it is not possible.any idea or any other statement

What I have tried:

I cant find any solution for this task. can anybody give some tips
Posted
Updated 13-Dec-16 13:12pm
Comments
Azziet 13-Dec-16 2:59am    
you can use the Switch Case,
please share the code what you have tried with if else code
JOTHI KUMAR Member 10918227 13-Dec-16 3:06am    
int age;
int agetype;
int pay;
int cou;
int work;

if getting above fields value in dataset, above value have only 0,1,2.
below condtion how can i write

if(age ==0 && agetype==0 && pay==0 && cou==0 && work==0)
{
}
else if(age ==0 && agetype==1 && pay==1 && cou==1 && work==1)
{
}
else if(age ==0 && agetype==2 && pay==2 && cou==2 && work==2)
{
}
else if(age ==0 && agetype==0 && pay==1 && cou==0 && work==2)
{
}

else if(age ==0 && agetype==1 && pay==0 && cou==2 && work==0)
{
}

how much i write to satisify above five condition
Patrice T 13-Dec-16 17:41pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Shambhoo kumar 13-Dec-16 3:35am    
Hi Jothi Kumar, You can try "C# Ternary(?)" or switch case.
JOTHI KUMAR Member 10918227 13-Dec-16 4:24am    
any sample for using ternary operator using above condition

In case you wonder why answers are so different and not necessary useful, it is because you didn't gave us the useful informations.
You forgot to tell us what you do when your test have identified the combination of values. And this matters.
This code:
C#
int age;
int agetype;
int pay;
int cou;
int work;

if getting above fields value in dataset, above value have only 0,1,2.
below condtion how can i write

if(age ==0 && agetype==0 && pay==0 && cou==0 && work==0)
{
}
else if(age ==0 && agetype==1 && pay==1 && cou==1 && work==1)
{
}
else if(age ==0 && agetype==2 && pay==2 && cou==2 && work==2)
{
}
else if(age ==0 && agetype==0 && pay==1 && cou==0 && work==2)
{
}

else if(age ==0 && agetype==1 && pay==0 && cou==2 && work==0)
{
}

can be simplified to:
C#
int age;
int agetype;
int pay;
int cou;
int work;

if getting above fields value in dataset, above value have only 0,1,2.
below condtion how can i write

because the if structure is doing nothing.
 
Share this answer
 
You might use a multidimensional array. Assuming (for sake of simplicity) your if statement blocks just return an integer:

C#
static int[, ,] selector;

static int select(int i, int j, int k)
{
  return selector[i, j, k];
}

public static void Main()
{

  selector = new int[3, 3, 3];

  selector[0, 0, 0] = 1; // this corresponds to if (i==0 && j==0 && k == 0)
  selector[0, 0, 1] = 1; //   "        "     "  if (i==0 && j==0 && k ==1)
  selector[0, 0, 2] = 5;
  selector[0, 1, 0] = 7;
  selector[0, 1, 1] = 7;
  selector[0, 1, 2] = 7;
  //...
  selector[2, 2, 2] = 10;
}


On the other hand, if your 'if execution blocks' are more comlpex, you might use a multidimensional array of delegates.
 
Share this answer
 
v2
Comments
JOTHI KUMAR Member 10918227 13-Dec-16 4:36am    
In selector[0,0,0] =1; what is this 1 means??
Philippe Mori 13-Dec-16 16:29pm    
If the conditions are mostly exclusive, that might make some senses but I would use Action<> or Func<> as the array type and put code there.
CPallini 14-Dec-16 2:21am    
Inside an if-else if chain conditions are mutually exclusive.
Yes I keept it simple. For anything but a bare return statement, you need to change the array type as you suggested.
If that is the requirement to check for all permutations of the 5 parameters, then there is no escape. However, you can make you coding cleaner, clearer, and more straight forward with a twist. You may consider representing each permutation of these 5 int's as a string pattern of 5 digits each of which may be 0, 1, or 2. For example:
C#
int age = 0;
int agetype = 2;
int pay = 0;
int cou = 1;
int work = 2;
// Convert int to string pattern
string str = age.ToString() + agetype.ToString() + pay.ToString() + cou.ToString() + work.ToString();
switch(str)
{
    case "00000": // first digit is age, second agetype, third pay, and so on...
        // do sth
        break;
    case "00001":
        // do sth
        break;
    case "00002":
        // do sth
        break;
    case "00010":
        // do sth
        break;
    // and so on...
}

To recover from pattern to the original int's,
C#
// Recovery
age = int.Parse(str.Substring(0, 1));
agetype = int.Parse(str.Substring(1, 1));
pay = int.Parse(str.Substring(2, 1));
cou = int.Parse(str.Substring(3, 1));
work = int.Parse(str.Substring(4, 1));
 
Share this answer
 
v7
Comments
Jochen Arndt 13-Dec-16 8:22am    
Basically a good attempt. But why use a string?

Just multiply each variable with a power of 10 so that integers are used (values are known to be 0 to 2):

switch (age + agetype * 10 + pay * 100 + cou * 1000 + work * 10000)
{
// ...
}
Peter Leow 13-Dec-16 8:27am    
Why not? This is another twist.
Jochen Arndt 13-Dec-16 8:33am    
Using integers is faster and generates less code (I'm a C/C++ guy: No switch with strings).

Both solutions would be probably realised by the compiler generating a lookup table which is pointing to Carlos solution.
Peter Leow 13-Dec-16 8:37am    
Thank you, I have learned something valuable.
Well, you have not provide enough code for use to have enough information to give the best solutions so any solution would be a guess... But in any case, I would make many changes to that design.

Names are not all properly selected but I don't have necessary information about what the application does

C#
enum Age { Low = 0, Medium = 1, High = 2}
enum AgeType { Low = 0, Medium = 1, High = 2}
enum Payment { Low = 0, Medium = 1, High = 2}
enum Country { US = 0, Canada = 1, Mexico = 2}
enum Working{ Low = 0, Medium = 1, High = 2}

class Data
{
    public Age { get; set; }    
    public AgeType { get; set; }
    public Payment { get; set; }
    public Country { get; set; }
    public Working { get; set; }
}

class Code
{
    public abstract bool Test(Data data);
    public abstract void Execute(Data data);
}

class Case1Code : Code 
{
    public virtual bool Test(Data data)
    {
        return data.Age == Age.Low && ... ;
    }

    public virtual void Execute(Data data)
    {
        // Do something specific here...
    }
}

class Factory
{
    static Factory()
    {
        allChoices = new List<Code>();

        allChoices.Add(new Case1Code());
        allChoices.Add ... ;
    }

    public void Process(Data data)
    {
        allChoices.FirstOrDefault(x => x.Test(data))?.Process(data);
    }

    private static List<Code> allChoices;
}


This would be particularly useful if there are many operations that could depend on a condition set. If some classes have similar code, then you can have some protected methods for code sharing purpose.

In practice, you might want to have class hierarchies for part of the data (say the country) and then forward to another class hierarchies (maybe for age groups). However, without knowing exactly the business processus, it is hard to find the best solution.

For example, the country class might define the age limits for a given group or the available working status. Many countries might share similar rules so they would probably use the same class.

Depending on the problem, you might prefer to use Action<> or Func<> instead particularly if you have only one simple operation or if each operation have its own conditions...
 
Share this answer
 

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