0

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?

User987
  • 3,663
  • 15
  • 54
  • 115
  • 1
    There's no `switch` for different properties, and if the case is only 3 properties, better code it manually, any other solution will be slower. – Gusman May 04 '17 at 12:36
  • 2
    You can only use a `switch` using constant values – Matthiee May 04 '17 at 12:37
  • @Matthiee Got it , thanks for that.. :) I was thinking there might be this option in newer versions of C# – User987 May 04 '17 at 12:37
  • @Gusman 3 if statements would do fine ? – User987 May 04 '17 at 12:38
  • 1
    Yes, that's the best you can opt to, 3 if's or a chained if/else if/ else if – Gusman May 04 '17 at 12:38
  • 2
    Also `if` are much faster than switches and you can optimize even more by putting them in order of most often true – Franck May 04 '17 at 12:40
  • @Franck that's false, switch usually builds a hash table to access the correct code so it tends to be faster unless there are a very small number of branches. – Gusman May 04 '17 at 12:56
  • @Gusman i based myself on real production code i have using stopwatches. We improved execution time by quite alot by changing a big switch with a long list if `if...else`. planning to change to `hashtable` in the future and it should be even faster. – Franck May 04 '17 at 16:28
  • @Franck then you have something wrong as Switch already uses a hashtable where it is possible to use... http://stackoverflow.com/questions/3366376/are-net-switch-statements-hashed-or-indexed – Gusman May 04 '17 at 16:30

2 Answers2

1

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
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
0

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");
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • dasblinkenknight this is really neat, but with this combination i would first have to send make the API call and only then I can get return value of each of these properties and only then I can decide which one to use, right ? – User987 May 04 '17 at 12:47
  • @User987 That's right, `switch` needs to know the combination upfront. If you'd like to make calls until a condition is satisfied, `switch` is not a good choice. I edited to show an alternative. – Sergey Kalinichenko May 04 '17 at 13:00