Example of Switch Statements [closed]

2019-02-16 09:41发布

If the total of this textbox is: PostDiscountTextBox.Text = $500.00, how do we make Switch statements stating that if the Post-discount Cost amount is between 0.00 and 999.99, display a message box with the message of "This amount qualifies for 'A-100' frequent flier miles" and an "OK" button?

Will someone provide an example of a switch statement?

I only have this so far, and I don't think it follows anything at all. Will someone guide me through this? Thank you.

        switch (PostDiscountCostTextBox.Text)
        {
            case (0.00 < && PostDiscountCostTextBox.Text <= 999.00)

Thank everyone who helped, but I am trying to figure out how to use a switch statement that evaluates Post-discount Cost based on a range of numeric values (nothing about an if statement). Yes, many cases will be put, and these will be my first two cases. Will someone be kind enough to provide me an example so that I could fill the rest of my cases in? I have lots. Thank you.

If the Post-discount Cost amount is between 0.00 and 999.99, display a message box with the message of "This amount qualifies for 'A-100' frequent flier miles." and an "OK" button. No title bar text or icon should be used.

If the Post-discount Cost amount is between 1,000.00 and 1,499.99, display a message box with the message of "This amount qualifies for 'B-500' frequent flier miles." and an "OK" button. No title bar text or icon should be used.

3条回答
女痞
2楼-- · 2019-02-16 10:10

The straightforward way is to use if else statements as the ranges check are not allowed for the switch operation. The another tricky way is to use Dictionaries. The below code snippet is a demonstration of what you want using the second approach.

decimal myVal = decimal.Parse(PostDiscountCostTextBox.Text);

        var conditions = new Dictionary<Func<int, bool>, Action>
        { 
            { x => x > 0 && x <= 999 ,    () => Console.WriteLine("This amount qualifies for 'A-100' frequent flier miles.")   } ,
            { x => x > 999 ,   () => Console.WriteLine("Dummy!")  } ,
        };

        cases.First(kvp => kvp.Key(myNum)).Value();

Not so easy as the if else approach, but desreves a test.

查看更多
家丑人穷心不美
3楼-- · 2019-02-16 10:18

That kind of switch usage is not allowed in C#.

Here is an example of proper switch usage

switch(n)       
{         
   case 1:   
      cost += 25;
      break;                  
   case 2:            
      cost += 25;
      break;           
   case 3:            
      cost += 50;
      break;         
   default:            
      Console.WriteLine("Invalid selection. Please select 1, 2, or 3.");            
      break;      
 }

Your example should be transform into if-elseif-else statement:

if(first_case_predicate)
{

}
else if(another_predicate)
{

}
else
{
    // like 'default' of switch
}
查看更多
在下西门庆
4楼-- · 2019-02-16 10:26

You cannot check for ranges in switch, you should use a chain of else if. See Is using decimal ranges in a switch impossible in C#?

查看更多
登录 后发表回答