Link to home
Start Free TrialLog in
Avatar of ukerandi
ukerandiFlag for United Kingdom of Great Britain and Northern Ireland

asked on

C# If condition

Hi , I have to write many  if condition for dropdown 2 boxes value selections. see blow my code
 
private void ExecutiveTab(string Drpvalue1, string Drpvalue2)
        {


            if (Drpvalue1 == "PriorYear" && Drpvalue1 == "FQ")
            { 
            
            
            }
            if (Drpvalue1 == "PriorYear" && Drpvalue1 == "Automated")
            {


            }

            if (Drpvalue1 == "PriorYear" && Drpvalue1 == "Manual")
            {


            }

            if (Drpvalue1 == "PriorYear" && Drpvalue1 == "Direct")
            {


            }

            if (Drpvalue1 == "PriorYear" && Drpvalue1 == "InDirect")
            {


            }

            if (Drpvalue1 == "LastQuarter" && Drpvalue1 == "FQ")
            {


            }
            if (Drpvalue1 == "LastQuarter" && Drpvalue1 == "Automated")
            {


            }

}

Open in new window


I also thought doing this way
if (Drpvalue1 == "PriorYear")
            {
                switch (Drpvalue1)
                {
                    case  "FQ":
                        break;
                }

            }

Open in new window

But not sure what is the best and more professional way

Just wondering any proper way.Thanks
Avatar of p_davis
p_davis

its not correct to check for (variable == value && variable == value)

it would never yield a true because your are asking if  numberThatCantChange ==1 && numberThatCantChange == 2
if you are going to use strings in a switch statement,  you might want to make them a constant variable.... as long as the dropdown values aren't dynamic.
I'm assuming that the first example was a copy and paste error but you can still you a switch statement... and i would recommend that if you have a lot of predefined values to check; instead of an 'if' statement
SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
do u think u can saved those conditions into a repository like a xml file, or even a table in database? so it would be easier for future extension?
Another point is that all of these if statements will be tested in code like the above.  A better way in general would be to use if / else if / else so the code only runs until it meets a true condition.  

ps. In general the logic of the one if and an enclosed switch/case is preferred but is not always sensible - it depends on just what is being checked and how many possible values it could have.
Avatar of ukerandi

ASKER

sorry this is supposed to be
If(Drpvalue1 == "PriorYear" && Drpvalue2 == "FQ")
its a printing mistake .sorry :(
How is this,its
private void ExecutiveTab(string Drpvalue1, string Drpvalue2)
        {
if (Drpvalue1 == "PriorYear")
            {
                switch (Drpvalue2)
                {
                    case  "FQ":
                        break;
                   
                    case "Automated":
                        break;

                    case "Manual":
                        break;

                    case "Direct":
                        break;

                }


            }

 if (Drpvalue1 == "LastQuarter")
            {
                switch (Drpvalue2)
                {
                    case "FQ":
                        break;

                    case "Automated":
                        break;

                    case "Manual":
                        break;

                    case "Direct":
                        break;

                }


            }

}
Ask your self is that much more readable and easier to understand the flow.  (Hint: yes)

ps.  You might want to have a 'default' case in the switch statement should the Drpvalue2 not match to any of the case values.

pps.  Playing devils advocate what do you do if instead of "FQ" the user enters "Fq".  In other words just how are these string values assigned their value?  Comparing strings is always a potential source of errors in the code.
no, this is not enter by user.
What I need to know this kind of situation what is best way to use.with if,select or any other properway. I knew I can use table.(database table). my aim is find best professional way to do this kind of incident.
if anyone know very simple function or any less code ,please let me know or guide me.

Many thanks
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Karrtik, Can you show me the some example.
Sure, can you give me couple of code sample of your if/else with the code that you want to execute when condition is met, so that I can share equivalent what I would do as mentioned in my previous comments?