Tutorial :C#: How can I combine a switch with an if-statement?



Question:

I need to combine a switch with an if-statement.

How can I do that? I want to do something like this:

switch (periodtype)  {      if(starttime>endtime)      {          ;      }      else      {          case 0: nextRunTime = nextRunTime.AddHours(period); break;          case 1: nextRunTime = nextRunTime.AddMinutes(period); break;          case 2: nextRunTime = nextRunTime.AddSeconds(period); break;      }  }  


Solution:1

What's wrong with:

if(starttime<=endtime)  {      switch (periodtype)      {          case 0: nextRunTime = nextRunTime.AddHours(period); break;          case 1: nextRunTime = nextRunTime.AddMinutes(period); break;          case 2: nextRunTime = nextRunTime.AddSeconds(period); break;      }  }  


Solution:2

if(starttime>endtime)  {      // Stuff  }  else  {      switch (periodtype)      {          case 0: nextRunTime = nextRunTime.AddHours(period); break;          case 1: nextRunTime = nextRunTime.AddMinutes(period); break          case 2: nextRunTime = nextRunTime.AddSeconds(period); break;      }  }  

Like that?


Solution:3

if(starttime>endtime)  {      ;  }  else  {     switch (periodtype)     {        case 0: nextRunTime = nextRunTime.AddHours(period); break;        case 1: nextRunTime = nextRunTime.AddMinutes(period); break;        case 2: nextRunTime = nextRunTime.AddSeconds(period); break;     }  }  


Solution:4

if(starttime <= endtime)  {        switch (periodtype)        {                    case 0: nextRunTime = nextRunTime.AddHours(period); break;               case 1: nextRunTime = nextRunTime.AddMinutes(period); break;               case 2: nextRunTime = nextRunTime.AddSeconds(period); break;           }  }  


Solution:5

Hmm way too late with answer

isnt this easier:

if (starttime <= endtime)  {      switch (periodtype)      {             case 0: nextRunTime = nextRunTime.AddHours(period); break;          case 1: nextRunTime = nextRunTime.AddMinutes(period); break;          case 2: nextRunTime = nextRunTime.AddSeconds(period); break;      }  }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »