• No results found

The Switch Construction

In document C# Programming - Rob Miles (Page 69-72)

We now know nearly everything you need to know about constructing a program in the C# language. You may find it rather surprising, but there is really very little left to know about programming itself. Most of the rest of C is concerned with making the business of programming simpler. A good example of this is the switch construction.

3.5.1 Making Multiple Decisions

Suppose you are refining your double glazing program to allow your customer to select from a pre-defined range of windows. You ask something like

Enter the type of window:

1 = casement 2 = standard 3 = patio door

Your program can then calculate the cost of the appropriate window by selecting type and giving the size. Each method asks the relevant questions and works out the price of that kind of item.

When you come to write the program you will probably end up with something like:

Creating Programs The Switch Construction

static void handleCasement () {

Console.WriteLine("Handle Casement");

}

static void handleStandard () {

Console.WriteLine("Handle Standard");

}

static void handlePatio () {

Console.WriteLine("Handle patio");

}

These methods are the ones which will eventually deal with each type of window. At the moment they just print out that they have been called. Later you will go on and fill the code in (this is actually quite a good way to construct your programs). Once you have the methods in place, the next thing you need to do is write the code that will call the one that the user has selected.

We already have a method that we can use to get a number from the user (it is called readInt and is given a prompt string and high and low limits). Our program can use this method to get the selection value and then pick the method that needs to be used.

3.5.2 Selecting using the if construction

When you come to perform the actual selection you end up with code which looks a bit like this:

int selection ;

selection = readInt ( "Window Type : ", 1, 3 ) ;

if ( selection == 1 ) {

handleCasement();

} else {

if ( selection == 2 ) {

handleStandard();

} else {

if ( selection == 3 ) {

handlePatio() ; }

else {

Console.WriteLine ( "Invalid number" );

} } }

This would work OK, but is rather clumsy. You have to write a large number of if constructions to activate each option.

3.5.3 The switch construction

Because you have to do this a lot C# contains a special construction to allow you to select one option from a number of them based on a particular value. This is called the

Creating Programs The Switch Construction

switch (selection) {

case 1 :

handleCasement ();

break ; case 2 :

handleStandard () ; break ;

case 3 :

handlePatio () ; break ;

default :

Console.WriteLine ( "Invalid number" ) ; break ;

}

The switch construction takes a value which it uses to decide which option to perform. It executes the case which matches the value of the switch variable. Of course this means that the type of the cases that you use must match the switch

selection value although, in true C# tradition, the compiler will give you an error if you make a mistake. The break statement after the call of the relevant method is to stop the program running on and performing the code which follows. In the same way as you break out of a loop, when the break is reached the switch is finished and the program continues running at the statement after the switch.

Another other useful feature is the default option. This gives the switch somewhere to go if the switch value doesn't match any of the cases available; in our case (sorry!) we put out an appropriate message.

You can use the switch construction with types other than numbers if you wish:

switch (command) {

case "casement" : handleCasement ();

break ; case "standard" :

handleStandard () ; break ;

case "patio" : handlePatio () ; break ;

default :

Console.WriteLine ( "Invalid command" ) ; break ;

}

This switch uses a string to control the selection of the cases. However, your users would not thank you for doing this, since it means that they have to type in the complete name of the option, and of course if they type a character wrong the command is not recognised.

Multiple cases

You can use multiple case items so that your program can execute a particular switch element if the command matches one of several options:

Creating Programs Using Files

switch (command) {

case "casement" : case "c" :

handleCasement ();

break ; case "standard" : case "s" :

handleStandard () ; break ;

case "patio" : case "P" :

handlePatio () ; break ;

default :

Console.WriteLine ( "Invalid command" ) ; break ;

}

The above switch will select a particular option if the user types the full part of the name or just the initial letter. If you want to perform selection based on strings of text like this I’d advise you to take a look at the ToUpper and ToLower methods provided by the string type. These can be used to obtain a version of a string which is all in upper or lower case, which can make the testing of the commands much easier:

switch (command.ToUpper()) {

case "CASEMENT" : case "C" :

....

Programmer’s Point: switches are a good idea

Switches make a program easier to understand as well as quicker to write. It is also easier to add extra commands if you use a switch since it are just a matter of putting in another case. However, I'd advise against putting large amounts of program code into a switch case. Instead you should put a call to a method as I have above.

In document C# Programming - Rob Miles (Page 69-72)