When defining an expression whose result would lead to a specific program execution, the switch statement considers that result and executes a statement based on the possible outcome of that expression, this possible outcome is called a case. The different outcomes are listed in the body of the switch statement and each case has its own execution, if necessary. The body of a switch statement is delimited from an opening to a closing curly brackets: “{“ to “}”. The syntax of the switch statement is: switch(Expression) { case Choice1: Statement1; break; case Choice2: Statement2; break; case Choice-n: Statement-n; break; } The expression to examine in a case statement is an integer. Since a member of an enumeration (enum) and the character (char) data types are just other forms of integers, they can be used too. Here is an example of using the switch statement: <%@ Page Language="C#" %> <html> <head> <script runat="server"> private void HouseSelected(object sender, EventArgs e) { switch(TypesOfHouses.SelectedIndex) { case 0: lblDescription.Text = "We have many low-level and high rise " + "buildings featuring the best condos in the area."; break; case 1: lblDescription.Text = "Our townhouses are the widest " + "you can find with various levels and rooms."; break; case 2: lblDescription.Text = "We have many single family houses in " + "ontemporary, classical, victorian architecture."; break; } } </script> <title>Altair Real Estate</title> </head> <body> <h3>Altair Real Estate</h3> <form id="frmRealEstate" runat="server"> <table> <tr> <td>What type of property do you want to purchase?</td> <td> <asp:DropDownList id="TypesOfHouses" AutoPostBack="True" OnSelectedIndexChanged="HouseSelected" runat="server"> <asp:ListItem>Condominium</asp:ListItem> <asp:ListItem>Townhouse</asp:ListItem> <asp:ListItem>Single Family</asp:ListItem> <asp:ListItem>Unknown</asp:ListItem> </asp:DropDownList> </td> </tr> <tr> <td colspan="2"> <asp:Label id="lblDescription" runat="server"></asp:Label> </td> </tr> </form> </body> </html> When establishing the possible outcomes that the switch statement should consider, at times there will be possibilities other than those listed and you will be likely to consider them. This special case is handled by the default keyword. The default case would be considered if none of the listed cases matches the supplied answer. The syntax of the switch statement that considers the default case would be: switch(Expression) { case Choice1: Statement1; break; case Choice2: Statement2; break; case Choice-n: Statement-n; break; default: Other-Possibility; break; } Therefore another version of the program above would be <script runat="server"> private void HouseSelected(object sender, EventArgs e) { switch(TypesOfHouses.SelectedIndex) { case 0: lblDescription.Text = "We have many low-level and high rise " + "buildings featuring the best condos in the area."; break; case 1: lblDescription.Text = "Our townhouses are the widest " + "you can find with various levels and rooms."; break; case 2: lblDescription.Text = "We have many single family houses in " + "ontemporary, classical, victorian architecture."; break; default: lblDescription.Text = "We will try to assist you in making " + "a clear choice for your new house"; break; } } </script> Besides a value of an int type, you can also use another variant of integers on a switch statement. For example, you can use letters to validate the cases.
Each of the cases we have used so far examined only one possibility before executing the corresponding statement. You can combine cases to execute the same statement. To do this, type a case, its value, and the semi-colon. Type another case using the same formula. When the cases are ready, you can then execute the desired statement. Here is an example: <script runat="server"> private void HouseSelected(object sender, EventArgs e) { switch(TypesOfHouses.SelectedIndex) { case 0: lblDescription.Text = "We have many low-level and high rise " + "buildings featuring the best condos in the area."; break; case 1: case 2: lblDescription.Text = "Our homes are built by the most " + "honest builders in the area."; break; lblDescription.Text = "We have many single family houses in " + "ontemporary, classical, victorian architecture."; break; default: lblDescription.Text = "We will try to assist you in making " + "a clear choice for your new house"; break; } } </script>
One of the most fundamental uses of enumerations is to process them in a switch statement. To do this, you pass the value of an enumeration to a switch. The values of the enumerations are then processed in the case statements. |
|
|||||||||
|