Olukayode Oluwole
asked on
How can i make my switch statement more efficient
How can i make the switch statement below more efficient
What i want is LoginDetails.staticfiscaly ear = "xx" DO NOTHING
If LoginDetails.staticfiscaly ear = 01 or 02 ................ 12 Then runstatus = proccesor.PayrollYearStart Routine(); // Annual clear records
Currently I am having to repeat same line for 01 .....12 twelve time as below
[switch (LoginDetails.staticfiscal year)
{
case "xx":
break;
case "01":
runstatus = proccesor.PayrollYearStart Routine(); // Annual clear records
break;
case "02":
runstatus = proccesor.PayrollYearStart Routine(); // Annual clear records
break;
case "03":
runstatus = proccesor.PayrollYearStart Routine(); // Annual clear records
break;
case "04":
runstatus = proccesor.PayrollYearStart Routine(); // Annual clear records
break;
case "05":
runstatus = proccesor.PayrollYearStart Routine(); // Annual clear records
break;
case "06":
runstatus = proccesor.PayrollYearStart Routine(); // Annual clear records
break;
case "07":
runstatus = proccesor.PayrollYearStart Routine(); // Annual clear records
break;
case "08":
runstatus = proccesor.PayrollYearStart Routine(); // Annual clear records
break;
case "09":
runstatus = proccesor.PayrollYearStart Routine(); // Annual clear records
break;
case "10":
runstatus = proccesor.PayrollYearStart Routine(); // Annual clear records
break;
case "11":
runstatus = proccesor.PayrollYearStart Routine(); // Annual clear records
break;
case "12":
runstatus = proccesor.PayrollYearStart Routine(); // Annual clear records
break;
}[/code]
I want 2 switch line s
Regards
Olukay
What i want is LoginDetails.staticfiscaly
If LoginDetails.staticfiscaly
Currently I am having to repeat same line for 01 .....12 twelve time as below
[switch (LoginDetails.staticfiscal
{
case "xx":
break;
case "01":
runstatus = proccesor.PayrollYearStart
break;
case "02":
runstatus = proccesor.PayrollYearStart
break;
case "03":
runstatus = proccesor.PayrollYearStart
break;
case "04":
runstatus = proccesor.PayrollYearStart
break;
case "05":
runstatus = proccesor.PayrollYearStart
break;
case "06":
runstatus = proccesor.PayrollYearStart
break;
case "07":
runstatus = proccesor.PayrollYearStart
break;
case "08":
runstatus = proccesor.PayrollYearStart
break;
case "09":
runstatus = proccesor.PayrollYearStart
break;
case "10":
runstatus = proccesor.PayrollYearStart
break;
case "11":
runstatus = proccesor.PayrollYearStart
break;
case "12":
runstatus = proccesor.PayrollYearStart
break;
}[/code]
I want 2 switch line s
Regards
Olukay
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I agree with AndyAinscow. The if statement is enough. You don't need to use a switch. If you need to run different routines based on the months, then a switch statement would be fine to use. Seeing as you always want to run PayrollYearStartRoutine(), the if statement takes care of the condition where staticfiscalyear being equal to "xx".
switch (LoginDetails.staticfiscalyear)
{
case "xx": break;
case "01":
case "02":
case "03":
case "04":
case "05":
case "06":
case "07":
case "08":
case "09":
case "10":
case "11":
case "12":
runstatus = proccesor.PayrollYearStartRoutine(); // Annual clear records
break;
default: break;
}
note: fall thru is (only) allowed if no statement is in the case section. otherwise you need break or goto
switch (condition)
{
case "01":
goto case "02";
case "02":
do_something();
break;
Sara
ASKER
Thanks Much appreciated
switch (LoginDetails.staticfiscal
{
case "xx":
break;
default:
runstatus = proccesor.PayrollYearStart
}
or use an if statement if there are only two choices.