简体   繁体   中英

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

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;
    }
}

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;
    }
}
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?

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;
   }
}
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;

       }
}

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM