User987
User987

Reputation: 3823

C# switching between 3 different properites

I have 3 properties in my classes which look like following:

public class MyClass
{
  public string property1 {get;set;}
  public string property2 {get;set;}
  public string property3 {get;set;}
}

And I'm trying to switch between these 3 properties when I make an API call...

When I make an API call with any of these properties which is send as parameter towards my server, the server returns a flag "TotalResults". Now what I'd like to do here is to stop making API calls as soon as the server returns TotalResults > 0 (meaning there is at least 1 result returned by server).

So I was thinking I could try this with switch case statement. I'd lik to use the switch case statement like following:

The first case where server returns totalresults>0 i'd like to get out of the switch statement and proceed on with code...

Sample code:

  switch (myProperties)
    {
       case "":
          //Do Something
          break;
       case "":
          //Do Something
          break;
       case "":
          //Do Something
          break;
       default:
          //Do the Default
          break;
    }

So the first case which returns at least 1 result is the one that will be shown in view...

Can someone help me out ?

P.S. I understand I can switch 1 property with different values , but not 3 different properties with switch-case statement?

Upvotes: 0

Views: 224

Answers (2)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726809

You can construct a "bit mask" value based on the value of three properties, encoding the combination of values for which TotalReturn was above zero:

var combination =
    ((prop1TotalReturn > 0) ? 4 :0)
|   ((prop2TotalReturn > 0) ? 2 :0)
|   ((prop3TotalReturn > 0) ? 1 :0);

Note the choice of constants, 4, 2, and 1. These are powers of two, so the result will be in the range from 0 to 7, inclusive. Each bit set in combination corresponds to a property of MyClass.

Now you can use combination in a switch, to decide what to do based on which properties produced TotalReturn above zero, for example:

switch (combination) {
    case 7:
    case 6:
    case 5:
    case 4: // Use prop1
        break;
    case 3:
    case 2: // Use prop2
        break;
    case 1: // Use prop3
        break;
    case 0; // No property produced TotalReturn > 0
        break;
}

If you don't have all the data upfront, switch is not going to work. You can use deferred execution of LINQ to get the data "lazily":

var propNames = new[] {"prop1", "prop2", "prop3"};
var firstPositive = propNames
    .Select(name => new {
        Name = name
    ,   TotalReturn = MakeApiCall(name)
    }).FirstOrDefault(res => res.TotalReturn > 0);
if (firstPositive != null) {
    Console.WriteLine("TotalReturn for {0} was {1}", firstPositive.Name, firstPositive.TotalReturn);
} else {
    Console.WriteLine("No positive TotalReturn");
}

Upvotes: 0

Patrick Hofman
Patrick Hofman

Reputation: 157048

For multiple conditions on more than one variable we have a feature in C#: if statements. A regular switch is just to check one variable against a number of constants.

You could do something like this:

if (property1 == property2 == property3 == 0)
{
    // do your thing
}

// if not, you are done

Upvotes: 1

Related Questions