proseidon
proseidon

Reputation: 2305

Multiple conditional assignments in C#?

Okay, if I have a string that I want to be equal to something based on multiple conditions, what's the best way to implement that?

Psuedocode

int temp = (either 1, 2, or 3)
string test = (if temp = 1, then "yes") (if temp = 2, then "no") (if temp = 3, then "maybe")

Is there some concise way to do this? What would you do?

Upvotes: 1

Views: 847

Answers (9)

pero
pero

Reputation: 4259

You can also turn things around:

class Program
{
    enum MyEnum
    {
        Yes = 1, 
        No, 
        Maybe
    }

    static void Main(string[] args)
    {
        Console.WriteLine(MyEnum.Maybe.ToString());
        Console.ReadLine();
    }
}

This also is more in line that temp can be only 1, 2 or 3. If it's an int compiler won't warn you if temp gets value of 34.

You can also do this:

string GetText(int temp){ 
    return ((MyEnum)temp).ToString();
}

GetText(2) will return "No"

Upvotes: 0

Steve
Steve

Reputation: 216263

The most concise answer is the nested ternary operator

string test = (temp == 1 ? "yes" : (temp == 2 ? "no" : (temp == 3 ? "maybe" : "")));

if temp values are only 1,2,3 then

string test = (temp == 1 ? "yes" : (temp == 2 ? "no" : "maybe"));

of course this is the concise answer as asked, and this doesn't means that it is the best. If you could not exclude that, in future you will need more values to test, then it is better to use a dictionary approach as explained in the @zeebonk answer.

Upvotes: 1

cmasupra
cmasupra

Reputation: 340

This switch is closer to your pseudocode and is exact C# code:

int temp = /* 1, 2, or 3 */;
string test;
switch(temp)
{
    case 1:
        test = "yes";
        break;
    case 2:
        test = "no";
        break;
    case 3:
        test = "maybe";
        break;
    default:
        test = /* whatever you want as your default, if anything */;
        break;
}

Your pseudocode doesn't include a default case, but it's good practice to include one.

Upvotes: 1

zeebonk
zeebonk

Reputation: 5034

You can use a switch statement as mentioned in other answers but a dictionary can also be used:

var dictionary = new Dictionary<int, string>();
dictionary.Add(1, "yes");
dictionary.Add(2, "no");
dictionary.Add(3, "maybe");

var test = dictionairy[value];

This method is way more flexible than a switch statement and ever more readable than nested tenary operator statements.

Upvotes: 5

Chris Cudmore
Chris Cudmore

Reputation: 30151

The basic idea is:

String choices[] = {"yes","no","maybe"};
string test = choices[temp-1];

There are many different ways of actually implementing it. Depending on what your condition variable is, you might like to implement it as some sort of key-value list. SEE Zeebonk's answer for an example.

Upvotes: 2

RollRoll
RollRoll

Reputation: 8462

the obvious answer would be switch case

but just another flavor:

int temp = x; //either 1,2 or 3

string test = (temp == 1 ? "yes" : temp == 2 ? "no" : "maybe");

Upvotes: 1

JG in SD
JG in SD

Reputation: 5607

string test = GetValue(temp);

public string GetValue(int temp)
{
  switch(temp)
  {
    case 1:
      return "yes";

    case 2:
      return "no";

    case 3:
      return "maybe";

    default:
      throw new ArgumentException("An unrecognized temp value was encountered", "temp");
  }
}

Upvotes: 4

Erre Efe
Erre Efe

Reputation: 15557

Use a switch

switch(temp)
{
    case 1:
        return "yes";
    case 2:
        return "no";
    case default:
        return "maybe";
}

Upvotes: 11

Dave Zych
Dave Zych

Reputation: 21887

You can use a switch statement

string temp = string.Empty;
switch(value)
{
    case 1:
        temp = "yes";
        break;
    case 2:
        temp = "no";
        break;
    case 3:
        temp = "maybe";
        break;
}

Upvotes: 2

Related Questions