开发者

How to find which condition is true without using if statement

I have two conditions.

What I want to know is which condition is true, both are true/false, or only one is true.

The problem is if I use if statements, it increases the amount of code and it doesn't appear like a robust solution.

Are there any conditions or loops in C# which can check and tell in one go?

Can any one provide me with an alternative and clean solution?

Scenario:

string  garage = getGarageCost(id)
    string  helmet = getHelmetCost(id)

    if(garage == "")

    {

    string selected =  "dropdown is not selected"
    }

    else if(Convert.ToInt32(garage) >= 1)
    {
    string selected = "dropdown is selected"
    return selected value;
    }

    if(helmet == "")
    {
    string selected =  "dropdown is not selected"
    }
    else if(Convert.ToInt32(helmet) >= 1)
    {
    string selected = "dropdown is selected"
    return selected value;
    }

*Edit: *

I have 2 drop-down lists, which if selected passes the selected value to the page.

I have to check the dropdown list and then pass the values if nothing is selected then it should pass a string "nothing Selected".

If it is sele开发者_JAVA技巧cted then it should do some calculation.

The problem is I have to do 5 if statements because there are 5 possibilities

  1. one selected
  2. one not selected
  3. either way
  4. both selected
  5. both are not selected


What about a switch statement?

int  garage = getGarageCost(id)
int  helmet = getHelmetCost(id)

switch (garage) {

    case 0: 
        do some alternative work; 
        break;
    default: 
        everything else; 
        break;
}

switch (helmet) {

    case 0: 
        do some alternative work; 
        break;
    default: 
        everything else; 
        break;
}

pass data to view;


If both "dothis" are the same, then you can convert the int to int? and use this logic:

int value = getGarageCost(id) ?? getHelmetCost(id);
if (value != null)
{
    dothis()
}
else
{
    // nothing selected in both lists
}

.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜