Evaluating an Expression for Multiple Values Using switchAt times, the if construct isn't capable of handling a decision situation without a lot of extra work. One such situation is when you need to perform different actions based on numerous possible values of an expression, not just true or false. For instance, suppose that you wanted to perform actions based on a user's profession. The following shows what you might create using if: if (strProfession =="programmer") ... else if (strProfession =="teacher") ... else if (strProfession =="accountant") ... else ... As you can see, this structure can be a bit hard to read. If the number of supported professions increases, this type of construction will get harder to read and debug. In addition, executing many if statements like this is rather inefficient from a processing standpoint. The important thing to realize here is that each else...if is really evaluating the same expression (strProfession) but considering different values for the expression. Visual C# includes a much better decision construct for evaluating a single expression for multiple possible values: switch. A basic switch construct looks like the following: switch (expression) { case value1: ... jump-statement default: ... jump-statement } By the Way default is used to define code that executes only when expression doesn't evaluate to any of the values in the case statements. Use of default is optional. Here's the Profession example shown previously, but this time switch is used: switch (strProfession) { case "teacher": MessageBox.Show("You educate our young"); break; case "programmer": MessageBox.Show("You are most likely a geek"); break; case "accountant": MessageBox.Show("You are a bean counter"); break; default: MessageBox.Show("Profession not found in switch statement"); break; } The flow of the switch statement is as follows: When the case expression is matched, the code statement or statements within the case are executed. This must be followed by a jump statement, such as break, to transfer control out of the case body. By the Way If you create a case construct but fail to put code statements or a jump-statement within the case, execution will fall through to the next case statement, even if the expression doesn't match. The switch makes decisions much easier to follow. Again, the key with switch is that it's used to evaluate a single expression for more than one possible value. Building a switch ExampleYou're now going to build a project that uses advanced expression evaluation in a switch structure. This simple application displays a list of animals to the user in a combo box. When the user clicks a button, the application displays the number of legs of the animal chosen in the list (if an animal is selected). Start by creating a new Windows Application named Switch Example and then follow these steps:
Your form should now look like the one in Figure 13.4. Click Save All on the toolbar to save your work before continuing. Figure 13.4. This example uses only a combo box and a Button control. All that's left to do is add the code. Double-click the Button control to access its Click event and then enter the following code: switch (cboAnimals.Text) { case "Bird": MessageBox.Show("The animal has 2 legs."); break; case "Dog": // Notice there is no code here to execute. case "Horse": // Notice there is no code here to execute. case "Cat": MessageBox.Show("The animal has 4 legs."); break; case "Snake": MessageBox.Show("The animal has no legs."); break; case "Centipede": MessageBox.Show("The animal has 100 legs."); break; default: MessageBox.Show("You did not select from the list!"); break; } Here's what's happening:
As you can see, adding a new animal to the list can be as simple as adding a case statement. Try it now by pressing F5 to run the project and then follow these steps:
|