[Solved] Writing switch statements using integers [closed]


You cannot do what you want with a switch, but you can simplify it to:

if (intDaysOverdue <= 30)
    decInterestRate = 0m;
else if (intDaysOverdue <= 59)
    decInterestRate = .5m;
else if (intDaysOverdue <= 89)
    decInterestRate = .10m;
else 
    decInterestRate = .15m;

Your >= 30 and >= 60 conditions are not needed, as they are already true because of the prior if statements.

Switch/Case is more suited for specific values, not ranges. This is what the if statement is for.

If your interest rate increases .5 for every 30 days, similar to as @EZI suggested in the comments, you could further simplify the code to:

decInterestRate = ((int)Math.Min(intDaysOverdue, 90) /30) * .5;

3

solved Writing switch statements using integers [closed]