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
LVL 10
ukerandiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

p_davisCommented:
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
0
p_davisCommented:
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.
0
p_davisCommented:
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
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Jacques Bourgeois (James Burger)PresidentCommented:
None of them is professional, because both makes no sense (unless I had to much wine, which should not be the case).

Your code makes no sense, no matter if you use an if or a select case. Look at it and think a little bit about what it means.

     Drpvalue1 == "PriorYear" && Drpvalue1 == "FQ"

&& is an AND operation. This means that you are looking for a situation where the same variable has 2 different values. How can the same variable have 2 different values at the same time?????

Maybe what you want to do is an OR, which is || in C#

If this is the case, then a switch is a better choice, because once it gets a hit, it skips the remaining cases.

With a series of if, the comparisons goes on uselessly even after a match has been found (unnless you can put in a return in the case, which is not possible in most situations because you often have to do something else after the sequence of comparison)s.
0
Ryan ChongCommented:
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?
0
AndyAinscowFreelance programmer / ConsultantCommented:
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.
0
ukerandiAuthor Commented:
sorry this is supposed to be
If(Drpvalue1 == "PriorYear" && Drpvalue2 == "FQ")
its a printing mistake .sorry :(
0
ukerandiAuthor Commented:
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;

                }


            }

}
0
AndyAinscowFreelance programmer / ConsultantCommented:
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.
0
ukerandiAuthor Commented:
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
0
AndyAinscowFreelance programmer / ConsultantCommented:
If these strings just come from code then you would be better with enumerated values instead.  Comparing integer (enums) types is much faster than string comparisons and with enums they can be sensibly named:
eFQ=1, eAutomated=2, eManual=3....
so you endup with code still easy to read and understand:
                   case eFQ:
                        break;

                    case eAutomated:
                        break;

                    case eManual:
                        break;
0
Jacques Bourgeois (James Burger)PresidentCommented:
If you have many Drp1value values, then you should also consider to case them, so that once one is found, the process will not be repeated for all of them uselessly:

switch (Drpvalue1)
    {
        case "PriorYear":
            {
                switch (Drpvalue2)
                {
                    case  "FQ":
                        break;
                   
                    case "Automated":
                        break;

                    case "Manual":
                        break;

                    case "Direct":
                        break;

                }


            }
         break;
    {
        case "LastQuarter":
            {
                switch (Drpvalue2)
                {
                    case "FQ":
                        break;

                    case "Automated":
                        break;

                    case "Manual":
                        break;

                    case "Direct":
                        break;

                }


          }

}

This being said, if this is in a database application, I would consider a lookup table in the database. Just a few lines of code would do the trick and would make the code easier to follow.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Karrtik IyerSoftware ArchitectCommented:
If all this is in code, and not coming from DB,  and there are too many if conditions to handle, I would look at having a static map of the condition keys (string)  and function pointers (delegates), so that at runtime I can just invoke the right function based on condition by doing a find on the map. And this way all my if else conditions are gone. Hope this helps.
0
ukerandiAuthor Commented:
Karrtik, Can you show me the some example.
0
Karrtik IyerSoftware ArchitectCommented:
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?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.