Stephen,

You can't use a case statement that way.  The switch is evaluated once and
then each case can only have a constant to compare against, e.g.,

switch (lnPrice) {
  case 1.00:
     // handle one dollar
     break;
  case 1.01:
  case 1.02:
   // handle 1.01 or 1.02
   break;
  default:
    // handle all others
}

What you need is if/else:

if (lnPrice < low) {
  lnLocal = lnPrice * .0225;
else if (lnPrice <= mid) {
  lnLocal = ((lnPrice - 1600) * .0275) + (1600 * .0225);
else if (lnPrice >= mid) {
  lnLocal = (1600 * .0275) + (1600 * .0225);
}

As someone else pointed out, the 2nd two conditions overlap, so I would
either change the last one from >= to >, or else just make it an else
without another if.

In addition, although it may be optimized by the compiler in the release
version anyway, I would take the constants and fold them:

if (lnPrice < low) {
  lnLocal = lnPrice * .0225;
else if (lnPrice <= mid) {
  lnLocal = ((lnPrice - 1600) * .0275) + 36;
else if (lnPrice > mid) {
  lnLocal = 80;
}

--Bob

----- Original Message -----
From: "Stephen Russell" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Tuesday, December 28, 2004 11:15 AM
Subject: [C#.NET] Switch and can't convert


>
> This is my error message in the complie?
> " Cannot implicitly convert type 'bool' to 'int'"
>
> I have retarted Tax rules that are value based.  So I'm trying to do this:
>
> public double FigureLocal(double dPrice)
> {
> int  low = 1600, mid = 3200;
> int lnPrice = System.Convert.ToInt16(dPrice);
> switch (lnPrice)
> {
> case (lnPrice < low) :   <<<<<<<<<  errors here and all other case
> statement/lines
> {
> lnLocal = lnPrice * .0225;
> break;
> }
> case lnPrice <= mid:
> {
> lnLocal = ((lnPrice - 1600) * .0275) + (1600 * .0225);
> break;
> }
> case lnPrice >= mid:
> {
> lnLocal = (1600 * .0275) + (1600 * .0225);
> break;
> }
> }
>
> return lnLocal;
> }
>
> Stephen Russell
> S.R. & Associates
> Memphis, TN 38115
>
> 901.246-0159
>
>
>
>
>
> Yahoo! Groups Links
>
>
>
>
>
>
>


Yahoo! Groups Sponsor
ADVERTISEMENT
click here


Yahoo! Groups Links

Reply via email to