-1
    private const decimal piece_rate_199 = 0.5M, piece_rate_399 = 0.55M, 
    piece_rate_599 = 0.6M, 
    piece_rate_600= 0.65M;
    private decimal Payrate, TotalPay, AveragePay, WorkerCount, PayEarnedDecimal, PayDecimal;
    private int TotalPieces, QuantityInteger;
    private string MessageString; 
     
    public Form1()
    {
        
        InitializeComponent();
    }
    private decimal FindPayRate()
    {
        QuantityInteger = int.Parse(textNumberPiece.Text);
        switch (QuantityInteger)
        {
            case QuantityInteger >= 600:
                PayDecimal = piece_rate_600;
                break;

            case 400 >= 599:
                PayDecimal = piece_rate_599;
                break;

            case 200 >= 399:
                PayDecimal = piece_rate_399;
                break;

            case 1 >= 199:
                PayDecimal = piece_rate_199;
                break;

        }

plz solve this i am totally a beginner

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
  • 8
    Please don't just dump your entire code into the body, with the error message as the title and say "fix it" - give us some surrounding context, explain what you're trying to do.. this error will be occurring on one particular line and (in this case) we just need that line – Caius Jard Jul 04 '20 at 13:02
  • Take a look at https://stackoverflow.com/questions/20147879/switch-case-can-i-use-a-range-instead-of-a-one-number – Caius Jard Jul 04 '20 at 13:05
  • Does this answer your question? [Switch case: can I use a range instead of a one number](https://stackoverflow.com/questions/20147879/switch-case-can-i-use-a-range-instead-of-a-one-number) – thatguy Jul 04 '20 at 13:15

4 Answers4

1

A switch-case is used for discrete values, not for general-purpose conditions. Using an if-else if-else structure should work though:

QuantityInteger = int.Parse(textNumberPiece.Text);
if (QuantityInteger >= 600) 
{
    PayDecimal = piece_rate_600
}
else if (QuantityInteger >= 400 && QuantityInteger <= 599)
{
    PayDecimal = piece_rate_599;
}
else if (QuantityInteger >= 200 && QuantityInteger <= 399)
{
    PayDecimal = piece_rate_399;
}
else if (QuantityInteger <= 199 && QuantityInteger >= 1)
{
    PayDecimal = piece_rate_199;
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 2
    While this is true and the correct solution, I just wanted to add you could do this with a switch-when statement, like this: https://stackoverflow.com/questions/20147879/switch-case-can-i-use-a-range-instead-of-a-one-number?noredirect=1&lq=1, which is more readable in my opinion – Daantje Jul 04 '20 at 13:25
1

The solution from @Mureinik is correct, I just wanted to add a way to do what you want while still using a switch statement. You can use when within a switch to get the wanted result:

    switch (QuantityInteger)
    {
        case int n when (n >= 600):
            PayDecimal = piece_rate_600;
            break;

        case int n when (n >= 400 %% n <= 599:
            PayDecimal = piece_rate_599;
            break;

        case int n when (n >= 200 && n <= 399):
            PayDecimal = piece_rate_399;
            break;

        case int n when (n >= 1 && n <= 199:
            PayDecimal = piece_rate_199;
            break;
    }

In my opinion, this is more readable than using if-else statements

Daantje
  • 225
  • 2
  • 10
  • You could actually make this a switch expression like `PayDecimal = QuantityInteger switch { int n when n >= 600 => piece_rate_600, int n when n >= 400 && n <=599 => piece_rate_599, ...}` – juharr Jul 04 '20 at 14:52
0
QuantityInteger = int.Parse(textNumberPiece.Text);

if (QuantityInteger > 1)
{
  PayDecimal = QuantityInteger <= 199 ? piece_rate_199 : (QuantityInteger <= 399 ? 
  piece_rate_399 : ((QuantityInteger <= 599 ? piece_rate_599 : piece_rate_600)));
}

Alternate way:

 QuantityInteger = int.Parse(textNumberPiece.Text);

            if (QuantityInteger > 1)
            {
                if (QuantityInteger < 200)// Upto 199
                {
                    PayDecimal = piece_rate_199;
                }
                else if (QuantityInteger < 400)// Upto 399
                {
                    PayDecimal = piece_rate_399;
                }
                else if (QuantityInteger < 600)// Upto 599
                {
                    PayDecimal = piece_rate_599;
                }
                else// 600 Above
                {
                    PayDecimal = piece_rate_600;
                }
            }
Ashok K
  • 26
  • 4
0

The issue is that you're doing a switch on an integer, but the cases are all comparisons that result in a boolean value. So you cannot use a traditional switch to do range check. Typically this is just handled with if-else if statements as others have shown, but you can use a switch if you take advantage of pattern matching that allows you to do additional logic in a when clause, which others have also shown. But here are a couple of other options. Note you don't need the check on the upper bounds as they are evaluated in order.

switch expression

PayDecimal = QuantityInteger switch
{
    int n when n >= 600 => piece_rate_600,
    int n when n >= 400 => piece_rate_599,
    int n when n >= 200 => piece_rate_399,
    int n when n >= 1 => piece_rate_199,
    _ => throw Exception("Less than 1"), // or whatever you want for this case.
}

Using chained conditional operator

PayDecimal = QuantityInteger >= 600 ? piece_rate_600,
    : QuantityInteger >= 400 ? piece_rate_599,
    : QuantityInteger >= 200 ? piece_rate_399,
    : QuantityInteger >= 1 ? piece_rate_199,
    : throw Exception("Less than 1"), // or whatever you want for this case.
juharr
  • 31,741
  • 4
  • 58
  • 93