C# Programming - Rob Miles
C# Programming - Rob Miles
Rob Miles
Edition 2.0 July 2010
Contents
Introduction 5
Welcome ............................................................................................................................. 5 Reading the notes ................................................................................................................ 5 Getting a copy of the notes ................................................................................................. 5
Computers ............................................................................................................ 6 Programs and Programming ................................................................................. 8 Programming Languages .................................................................................... 13 C# ....................................................................................................................... 14
18
Creating Programs
3.1 3.2 3.3 3.4 3.5 3.6
52
Methods .............................................................................................................. 52 Variables and Scope ........................................................................................... 58 Arrays ................................................................................................................. 61 Exceptions and Errors......................................................................................... 65 The Switch Construction .................................................................................... 68 Using Files .......................................................................................................... 71
Creating Solutions
4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 4.10 4.11 4.12 4.13
76
Our Case Study: Friendly Bank .......................................................................... 76 Enumerated Types .............................................................................................. 76 Structures ............................................................................................................ 79 Objects, Structures and References .................................................................... 82 Designing With Objects ..................................................................................... 89 Static Items ......................................................................................................... 94 The Construction of Objects ............................................................................... 98 From Object to Component .............................................................................. 104 Inheritance ........................................................................................................ 109 Object Etiquette ................................................................................................ 119 The power of strings and chars ......................................................................... 124 Properties .......................................................................................................... 126 Building a Bank ................................................................................................ 129
Advanced Programming
5.1 5.2 5.3 5.4 5.5 5.6
134
Generics and Collections .................................................................................. 134 Storing Business Objects .................................................................................. 138 Business Objects and Editing ........................................................................... 148 A Graphical User Interface ............................................................................... 153 Using Delegates ................................................................................................ 162 Threads and Threading ..................................................................................... 163
Structured Error Handling ................................................................................ 171 Program Organisation ....................................................................................... 174 Debugging ........................................................................................................ 180 The End? .......................................................................................................... 183
Glossary of Terms
184
Abstract ........................................................................................................................... 184 Accessor.......................................................................................................................... 184 Base ................................................................................................................................ 184 Call ................................................................................................................................. 184 Class ............................................................................................................................... 184 Code Reuse ..................................................................................................................... 185 Cohesion ......................................................................................................................... 185 Collection........................................................................................................................ 185 Compiler ......................................................................................................................... 185 Component...................................................................................................................... 185 Constructor ..................................................................................................................... 185 Coupling ......................................................................................................................... 186 Creative Laziness ............................................................................................................ 186 Delegate .......................................................................................................................... 186 Dependency .................................................................................................................... 186 Event ............................................................................................................................... 186 Exception ........................................................................................................................ 186 Functional Design Specification ..................................................................................... 187 Globally Unique Identifier (GUID) ................................................................................ 187 Hierarchy ........................................................................................................................ 187 Immutable ....................................................................................................................... 187 Inheritance ...................................................................................................................... 188 Interface .......................................................................................................................... 188 Library ............................................................................................................................ 188 Machine code .................................................................................................................. 188 Member ........................................................................................................................... 188 Metadata ......................................................................................................................... 188 Method ............................................................................................................................ 188 Mutator ........................................................................................................................... 189 Namespace ...................................................................................................................... 189 Overload ......................................................................................................................... 189 Override .......................................................................................................................... 189 Portable ........................................................................................................................... 189 Private ............................................................................................................................. 189 Property .......................................................................................................................... 190 Protected ......................................................................................................................... 190 Public .............................................................................................................................. 190 Reference ........................................................................................................................ 190 Signature ......................................................................................................................... 190 Source file ....................................................................................................................... 190 Static ............................................................................................................................... 190 Stream ............................................................................................................................. 191 Structure.......................................................................................................................... 191 Subscript ......................................................................................................................... 191 Syntax Highlighting ........................................................................................................ 191 Test harness .................................................................................................................... 191 This ................................................................................................................................. 191 Typesafe.......................................................................................................................... 192 Unit test........................................................................................................................... 192 Value type ....................................................................................................................... 192 Rob Miles 2010 Department of Computer Science, The University of Hull.
ii
All rights reserved. No reproduction, copy or transmission of this publication may be made without written permission. The author can be contacted at: The Department of Computer Science, Robert Blackburn Building The University of Hull, Cottingham Road HULL HU6 7RX UK Department: www.dcs.hull.ac.uk Email: rob@robmiles.com Blog: www.robmiles.com If you find a mistake in the text please report the error to foundamistake@robmiles.com and I will take a look. Edition 2.0 Wednesday, 28 July 2010
iii
Introduction
Welcome
Introduction
Welcome
Welcome to the Wonderful World of Rob Miles. This is a world of bad jokes, puns, and programming. In this book I'm going to give you a smattering of the C# programming language. If you have programmed before I'd be grateful if you'd still read the text. It is worth it just for the jokes and you may actually learn something. If you have not programmed before, do not worry. Programming is not rocket science it is, well, programming. The bad news about learning to program is that you get hit with a lot of ideas and concepts at around the same time when you start, and this can be confusing. The keys to learning programming are: Practice do a lot of programming and force yourself to think about things from a problem solving point of view Study look at programs written by other people. You can learn a lot from studying code which other folk have created. Figuring out how somebody else did the job is a great starting point for your solution. And remember that in many cases there is no best solution, just ones which are better in a particular context, i.e. the fastest, the smallest, the easiest to use etc. Persistence writing programs is hard work. And you have to work hard at it. The principle reason why most folks don't make it as programmers is that they give up. Not because they are stupid. However, don't get too persistent. If you haven't solved a programming problem in 30 minutes you should call time out and seek help. Or at least walk away from the problem and come back to it. Staying up all night trying to sort out a problem is not a good plan. It just makes you all irritable in the morning. We will cover what to do when it all goes wrong later in section 5.9.
Font.
If you have any comments on how the notes can be made even better (although I of course consider this highly unlikely) then feel free to get in touch Above all, enjoy programming. Rob Miles rob@robmiles.com www.robmiles.com www.dcs.hull.ac.uk
Computers
1.1 Computers
Before we consider programming, we are going to consider computers. This is an important thing to do, because it sets the context in which all the issues of programming itself are placed.
Computers
do something useful. It is called software because it has no physical existence and it is comparatively easy to change. Software is the voice which says "Computer Running" in a Star Trek film. Windows 7 is an operating system. It gives computer programs a platform on which they can execute. All computers are sold with some software. Without it they would just be a novel and highly expensive heating system. The software which comes with a computer is often called its Operating System. The Operating System makes the machine usable. It looks after all the information held on the computer and provides lots of commands to allow you to manage things. It also lets you run programs, ones you have written and ones from other people. You will have to learn to talk to an operating system so that you can create your C# programs and get them to go.
You could regard this as meaning: "you are 256 pounds overdrawn at the bank" or "you are 256 feet below the surface of the ground" or "eight of the thirty two light switches are off" The transition from data to information is usually made when the human reads the output. So why am I being so pedantic? Because it is vital to remember that a computer does not "know" what the data it is processing actually means. As far as the computer is concerned data is just patterns of bits, it is the user who gives meaning to these patterns. Remember this when you get a bank statement which says that you have 8,388,608 in your account!
Data Processing
Computers are data processors. Information is fed into them; they do something with it, and then generate further information. A computer program tells the computer what to do with the information coming in. A computer works on data in the same way that a sausage machine works on meat, something is put in one end, some processing is performed, and something comes out of the other end:
Data
Computer
Data
This makes a computer a very good "mistake amplifier", as well as a useful thing to blame.....
A program is unaware of the data it is processing in the same way that a sausage machine is unaware of what meat is. Put a bicycle into a sausage machine and it will try to make sausages out of it. Put duff data into a computer and it will do equally useless things. It is only us people who actually give meaning to the data (see above). As far as the computer is concerned data is just stuff coming in which has to be manipulated in some way. A computer program is just a sequence of instructions which tell a computer what to do with the data coming in and what form the data sent out will have.
Note that the data processing side of computers, which you might think is entirely reading and writing numbers, is much more than that, examples of typical data processing applications are: Digital Watch: A micro-computer in your watch is taking pulses from a crystal and requests from buttons, processing this data and producing a display which tells you the time. Car: A micro-computer in the engine is taking information from sensors telling it the current engine speed, road speed, oxygen content of the air, setting of the accelerator etc and producing voltages out which control the setting of the carburettor, timing of the spark etc, to optimise the performance of the engine. CD Player: A computer is taking a signal from the disk and converting it into the sound that you want to hear. At the same time it is keeping the laser head precisely positioned and also monitoring all the buttons in case you want to select another part of the disk. Games Console: A computer is taking instructions from the controllers and using them to manage the artificial world that it is creating for the person playing the game. Note that some of these data processing applications are merely applying technology to existing devices to improve the way they work. However the CD player and games console could not be made to work without built-in data processing ability. Most reasonably complex devices contain data processing components to optimise their performance and some exist only because we can build in intelligence. It is into this world that we, as software writers are moving. It is important to think of business of data processing as much more than working out the company payroll, reading in numbers and printing out results. These are the traditional uses of computers. Note that this "raises the stakes" in that the consequences of software failing could be very damaging. As software engineers it is inevitable that a great deal of our time will be spent fitting data processing components into other devices to drive them. You will not press a switch to make something work, you will press a switch to tell a computer to make it work. These embedded systems will make computer users of everybody, and we will have to make sure that they are not even aware that there is a computer in there! You should also remember that seemingly innocuous programs can have life threatening possibilities. For example a doctor may use a spread sheet to calculate doses of drugs for patients. In this case a defect in the program could result in illness or even death (note that I don't think that doctors actually do this but you never know..)
Programming is defined by most people as earning huge sums of money doing something which nobody can understand. Programming is defined by me as deriving and expressing a solution to a given problem in a form which a computer system can understand and execute. One or two things fall out of this definition: You need to be able to solve the problem yourself before you can write a program to do it. The computer has to be made to understand what you are trying to tell it to do.
customer sign it, and the bottom line is that if you provide a system which behaves according to the design specification the customer must pay you. Once you have got your design specification, then you can think about ways of solving the problem. You might think that this is not necessary if you are writing a program for yourself; there is no customer to satisfy. This is not true. Writing some form of specification forces you to think about your problem at a very detailed level. It also forces you to think about what your system is not going to do and sets the expectations of the customer right at the start.
There are lots of ways of representing this information in the form of diagrams, for now we will stick with written text when specifying each of the stages:
Information going in
In the case of our immortal double glazing problem we can describe the information as: The width of a window. The height of the window.
10
You can see what we need if you take a look at the diagram below:
Height of Window
Width of Window
The area of the glass is the width multiplied by the height. To make the frame we will need two pieces of wood the width of the window, and two pieces of wood the height of the window.
Note that we have also added units to our description, this is very important - perhaps our customer buys wood from a supplier who sells by the foot, in which case our output description should read: Note that both you and the customer must understand the document! The area of glass required for the window, in square metres. Remember that we are selling double glazing, so two panes will be required. The length of wood required for the frame, given in feet using the conversion factor of 3.25 feet per metre.
Having written this all up in a form that both you and the customer can understand, we must then both sign the completed specification, and work can commence.
11
Proving it Works
In a real world you would now create a test which will allow you to prove that the program works, you could for example say: If I give the above program the inputs 2 metres high and 1 metre wide the program should tell me I need 4 square metres of glass and 19.5 feet of wood. The test procedure which is designed for a proper project should test out all possible states within the program, including the all-important error conditions. In a large system the person writing the program may have to create a test harness which is fitted around the program and will allow it to be tested. Both the customer and the supplier should agree on the number and type of the tests to be performed and then sign a document describing these. Testing is a very important part of program development. There is even one development technique where you write the tests before you write the actual program that does the job. This is actually a good idea, and one we will explore later. In terms of code production, you can expect to write as much code to test your solution as is in the solution itself. Remember this when you are working out how much work is involved in a particular job.
Getting Paid
Better yet, set up a phased payment system so that you get some money as the system is developed. At this point the supplier knows that if a system is created which will pass all the tests the customer will have no option but to pay for the work! Note also that because the design and test procedures have been frozen, there is no ambiguity which can lead to the customer requesting changes to the work although of course this can still happen! The good news for the developer is that if changes are requested these can be viewed in the context of additional work, for which they can be expect to be paid.
Customer Involvement
Note also in a "proper" system the customer will expect to be consulted as to how the program will interact with the user, sometimes even down to the colour of the letters on the display! Remember that one of the most dangerous things that a programmer can think is "This is what he wants"! The precise interaction with the user - what the program does when an error is encountered, how the information is presented etc., is something which the customer is guaranteed to have strong opinions about. Ideally all this information should be put into the specification, which should include layouts of the screens and details of which keys should be pressed at each stage. Quite often prototypes will be used to get an idea of how the program should look and feel. Fact: If you expect to derive the specification as the project goes on either you will fail to do the job, or you will end up performing five times the work! If this seems that you are getting the customer to help you write the program then you are exactly right! Your customer may have expected you to take the description of the problem and go into your back room - to emerge later with the perfect solution to the problem. This is not going to happen. What will happen is that you will come up with something which is about 60% right. The customer will tell you which bits look OK and which bits need to be changed. You then go back into your back room, muttering under your breath, and emerge with another system to be approved. Again, Rob's law says that 60% of the duff 40% will now be OK, so you accept changes for the last little bit and again retreat to your keyboard.... The customer thinks that this is great, reminiscent of a posh tailor who produces the perfect fit after numerous alterations. All the customer does is look at something, suggests changes and then wait for the next version to find something wrong with. They will get a bit upset when the delivery deadline goes by without a finished product appearing but they can always cheer themselves up again by suing you. Actually, we have come full circle here, because I did mention earlier that prototyping is a good way to build a system when you are not clear on the initial specificaiton. However, if you are going to use prototypes it is a good thing to plan for this from the
12
Programming Languages
start rather than ending up doing extra work because your initial understanding of the problem was wrong. Fact: More implementations fail because of inadequate specification than for any other reason! If your insistence on a cast iron specification forces the customer to think about exactly what the system is supposed to do and how it will work, all to the better. The customer may well say "But I am paying you to be the computer expert, I know nothing about these machines". This is no excuse. Explain the benefits of "Right First Time" technology and if that doesn't work produce a revolver and force the issue! Again, if I could underline in red I would: All the above apply if you are writing the program for yourself. You are your own worst customer! You may think that I am labouring a point here; the kind of simple systems we are going to create as we learn to program are going to be so trivial that the above techniques are far too long winded. You are wrong. One very good reason for doing this kind of thing is that it gets most of the program written for you - often with the help of the customer. When we start with our double glazing program we now know that we have to:
read in the width verify the value read in the height verify the value calculate width times height times 2 and print it calculate ( width + height ) * 2 * 3.25 and print it
The programming portion of the job is now simply converting the above description into a language which can be used in a computer.......
To take the first point. We cannot make very clever computers at the moment. Computers are made clever by putting software into them, and there are limits to the size of program that we can create and the speed at which it can talk to us. At the moment, by using the most advanced software and hardware, we can make computers which are about as clever as a tape worm. Tape worms do not speak very good English; therefore we cannot make a computer which can understand English. The best we can do is to get a computer to make sense of a very limited language which we use to tell it what to do. To take the second point. English as a language is packed full of ambiguities. It is very hard to express something in an unambiguous way using English. If you do not believe me, ask any lawyer!
13
C#
Programming languages get around both of these problems. They are simple enough to be made sense of by computer programs and they reduce ambiguity.
1.4 C#
There are literally hundreds of programming languages around; you will need to know at least 3! We are going to learn a language called C# (pronounced C sharp). If you ever make the mistake of calling the language C hash you will show your ignorance straight away! C# is a very flexible and powerful programming language with an interesting history. It was developed by Microsoft Corporation for a variety of reasons, some technical, some political and others marketing. C# bears a strong resemblance to the C++ and Java programming languages, having borrowed (or improved) features provided by these languages. The origins of both Java and C++ can be traced back to a language called C, which is a highly dangerous and entertaining language which was invented in the early 1970s. C is famous as the language the UNIX operating system was written in, and was specially designed for this.
1.4.1 Dangerous C
I referred to C as a dangerous language. So what do I mean by that? Consider the chain saw. If I, Rob Miles, want to use a chain saw I will hire one from a shop. As I am not an experienced chain saw user I would expect it to come with lots of built in safety features such as guards and automatic cut outs. These will make me much safer with the thing but will probably limit the usefulness of the tool, i.e. because of all the safety stuff I might not be able to cut down certain kinds of tree. If I was a real lumberjack I would go out and buy a professional chain saw which has no safety features whatsoever but can be used to cut down most anything. If I make a mistake with the professional tool I could quite easily lose my leg, something the amateur machine would not let happen. In programming terms what this means is that C lacks some safety features provided by other programming languages. This makes the language much more flexible. However, if I do something stupid C will not stop me, so I have a much greater chance of crashing the computer with a C program than I do with a safer language.
1.4.2 Safe C#
The C# language attempts to get the best of both worlds in this respect. A C# program can contain managed or unmanaged parts. The managed code is fussed over by the system which runs it. This makes sure that it is hard (but probably not impossible) to crash your computer running managed code. However, all this fussing comes at a price, causing your programs to run more slowly. To get the maximum possible performance, and enable direct access to parts of the underlying computer system, you can mark your programs as unmanaged. An unmanaged program goes faster, but if it crashes it is capable of taking the computer
14
C#
with it. Switching to unmanaged mode is analogous to removing the guard from your new chainsaw because it gets in the way. C# is a great language to start learning with as the managed parts will make it easier for you to understand what has happened when your programs go wrong.
15
C#
I'm not going to go into details of how to download and install the .NET framework; that is for other documents, I am going to assume that you are using a computer which has a text editor (usually Notepad) and the .NET framework installed.
16
C#
Objects
Some of the things in the programs that we write are objects that are part of the framework we are using. To continue our cooking analogy, these are things like mixing bowls and ovens, which are used during the cooking process. The names of objects will be given in a different shade of blue in some of the listings in this text.
17
A First C# Program
Code Sample 1 - GlazerCalc Program This is a valid program. If you gave it to a C# compiler it would compile, and you could run it. The actual work is done by the two lines that I have highlighted. Broadly speaking the stuff before these two lines is concerned with setting things up and getting the values in to be processed. The stuff after the two lines is concerned with displaying the answer to the user. We can now go through each line in turn and try to see how it fits into our program.
18
A First C# Program
using System;
A big part of learning to program is learning how to use all the additional features of the system which support your programs. This is an instruction to the C# compiler to tell it that we want to use things from the System namespace. A namespace is a place where particular names have meaning. We have namespaces in our conversations too, if I am using the "Football" namespace and I say That team is really on fire I'm saying something good. If I am using the "Firefighter" namespace I'm saying something less good. In the case of C# the System namespace is where lots of useful things are described. One of these useful things provided with C# is the Console object which will let me write things which will appear on the screen in front of the user. If I want to just refer to this as Console I have to tell the compiler I'm using the System namespace. This means that if I refer to something by a particular name the compiler will look in System to see if there is anything matching that name. We will use other namespaces later on.
class GlazerCalc
Classes are the basis of object oriented programming, as we shall see later. A C# program is made up of one or more classes. A class is a container which holds data and program code to do a particular job. In the case of our double glazing calculator the class just contains a single method which will work out our wood lengths and glass area, but a class can contain much more than that if it needs to. You need to invent an identifier for every class that you create. I've called ours GlazerCalc since this reflects what it does. For now, don't worry too much about classes; just make sure that you pick sensible names for the classes that you create. Oh, and one other thing. There is a convention that the name of the file which contains a particular class should match the class itself, in other words the program above should be held in a file called GlazerCalc.cs.
static
This keyword makes sure that the method which follows is always present, i.e. the word static in this context means "is part of the enclosing class and is always here". When we get to consider objects we will find that this little keyword has all kinds of interesting ramifications. But for now I'd be grateful if you'd just make sure that you put it here in order to make your programs work properly.
void
A void is nothing. In programming terms the void keyword means that the method we are about to describe does not return anything of interest to us. The method will just do a job and then finish. In some cases we write methods which return a result (in fact we will use such a method later in the program). However, in order to stop someone else accidentally making use of the value returned by our Main method, we are explicitly stating that it returns nothing. This makes our programs safer, in that the compiler now knows that if someone tries to use the value returned by this method, this must be a mistake.
Main
You choose the names of your methods to reflect what they are going to do for you. Except for Main. This method (and there must be one, and only one such method) is where your program starts running. When your program is loaded and run the first method given control is the one called Main. If you miss out the Main method the system quite literally does not know where to start.
19
A First C# Program
()
This is a pair of brackets enclosing nothing. This may sound stupid, but actually tells the compiler that the method Main has no parameters. A parameter to a method gives the method something to work on. When you define a method you can tell C# that it works on one or more things, for example sin(x) could work on a floating point value of angle x. We will cover methods in very great detail later in this document.
{
This is a brace. As the name implies, braces come in packs of two, i.e. for every open brace there must be a matching close. Braces allow programmers to lump pieces of program together. Such a lump of program is often called a block. A block can contain the declaration of variable used within it, followed by a sequence of program statements which are executed in order. In this case the braces enclose the working parts of the method Main. When the compiler sees the matching close brace at the end it knows that it has reached the end of the method and can look for another (if any). The effects of an un-paired brace are invariably fatal....
double
By now you probably feel that you need a drink. But that is not what double means in this context. What it means is "double precision floating point number". Our program needs to remember certain values as it runs. Notably it will read in values for the width and height of the windows and then calculate and print values for the glass area and wood length. C# calls the places where values are put variables. At the beginning of any block you can tell C# that you want to reserve some space to hold some data values. Each item you can hold a particular kind of value. Essentially, C# can handle three types of data, floating point numbers, integer numbers and text (i.e. letters, digits and punctuation). The process of creating a variable is called declaring the variable. A double variable can hold a very wide range of values to a very high precision. You declare some variables of a particular type by giving the type of the data, followed by a list of the names you want the variables to have. We are using the type double for now. Later we will use other types.
20
A First C# Program
;
The semicolon marks the end of the list of variable names, and also the end of that declaration statement. All statements in C# programs are separated by the ; character, this helps to keep the compiler on the right track. The ; character is actually very important. It tells the compiler where a given statement ends. If the compiler does not find one of these where it expects to see one it will produce an error. You can equate these characters with the sprocket holes in film, they keep everything synchronised.
widthString =
This is an assignment statement. In this C# statement we are going to change the value in a variable. Our program is going to read a line of text from the user and place the result in the variable we have called widthString. Remember that a variable is simply a named box of a particular size, which can hold a single data item (in this case a string of text). A good proportion of your programs will be instructions to assign new values to variables, as the various results are calculated. C# uses the = character to make assignments happen. The first part of this statement is the name of a previously defined variable. This is followed by the = character which I call the gozzinta. I call it that because the value on the right gozzinta (goes into) the variable on the left. Sorry.
Console.
On the right of the equals we have the thing which is going to be assigned to widthString. In this case the result is going to be the string returned by the method ReadLine. This method is part of an object called Console which looks after the user input and output. The full stop (.) separates the object identifier from the method identifier.
ReadLine
This indicates that the ReadLine method is to be invoked. This asks the running program to dash off, do whatever statements there are in this method, and then come back. Methods are a way in which you can break up your program into a number of chunks, each of which has a particular job. They also mean that you only have to write a particular piece of code once, put it in a method, and then call that method whenever you want that particular job done. The C# system contains a number of built in methods to do things for our programs. ReadLine is one of these. When this program runs the ReadLine method is invoked (or called). It will wait for the user to enter a line of text and press the Enter key. Whatever is typed in is then returned as a string by the ReadLine method. The result of the method call is placed in the widthString variable.
21
A First C# Program
()
A method call is followed by the parameters to the method. A parameter is something that is passed into a method for it to work on. Think of it as raw materials for a process of some kind. In the case of ReadLine it has no raw materials; it is going to fetch the information from the user who will type it in. However, we still have to provide the list of parameters even if it is empty.
;
We have seen the semi-colon before. It marks the end of a statement in our program.
width =
This is another assignment (Mr. Phelps/Hawke 1). The variable width is being given a value. Many statements in your programs will simply be moving data around and performing actions on it.
double.
But the rest of the statement looks a bit scary. In fact it is not too tricky. We are asking Mr. double (the thing responsible holding double precision floating point numbers) to do a little job for us. In this case the little job is "take the string held by widthString and convert it into a double precision floating point number. Mr. double provides this ability by exposing a method called Parse. Note that there is nothing wrong or naughty about something in C# exposing its methods. It is how we get things done for us. When you come to design larger programs you will find that the best way to do this is to create components which expose methods to get the job done. The whole of the C# library set is provided as a series of methods and one of the things that you will have to get to grips with is where the methods are and how to use them. As with many things in life, the trick is knowing who to ask
Parse
The Parse method has the job of converting the string it has been given into a double precision floating point number. To do this it must look along the string, pull out each digit in turn and then calculate the actual value, as in "12" means a ten and two units. This process of looking along things is often called parsing. Hence the name of the method we are using. The method is given the string that is to be parsed and returns the number that it has found. Note that this gives significant potential for evil, in that if the user doesnt type in a value or types something like "Twenty Five" the Parse method call will not be able to resolve a number and will fail as a result. How it fails, and how you can resolve this failure, will be left for a future section in order to add more excitement to this text.
(widthString);
We have seen that a call of a method must be followed by the raw materials (parameters) for that method. In the case of ReadLine there are no parameters, but we still need to supply an empty list to indicate this. In the case of Parse the method needs to be given the string that it is to work on. We do this by putting the name of the string variable containing the text (widthString) into the brackets as above. The value of the information in widthString (i.e. the text that the user has typed in) is passed into the Parse method for it to work on and extract the number from.
1
22
A First C# Program
Console.WriteLine
This is a call of a method, just like the ReadLine method, except that this one takes what it is given and then prints it out on the console.
(
This is the start of the parameters for this method call. We have seen these used in the calls of Parse and also ReadLine
+
Plus is an addition operator. We have seen it applied to add two integers together. However, the plus here means something completely different 2. In this case it means "add two strings together".
23
A First C# Program
You will have to get used to the idea of context in your programs. We have seen this with namespaces. Here it is with operators. The C# system uses the context of an operation to decide what to do. In the case of the previous +, between two double precision floating point numbers it means "do a sum". Here it has a string on the left hand side. This means that it is going to perform string concatenation rather than mathematical addition.
woodLength
This is another example of context at work. Previously we have used woodLength as a numeric representation of a value, in this program the length of the wood required. However, in the context it is being used at the moment (added to the end of a string) it cannot work like that. The C# compiler must therefore ask the woodLength data item to convert itself into a string so it can be used correctly in this position. Fortunately it can do this, and so the program works as you would expect. It is very important that you understand precisely what is going on here. Consider:
Console.WriteLine ( 2.0 + 3.0 );
This would perform a numeric calculation (2.0 + 3.0) and produce a double precision floating point value. This result value would then be asked to provide a string version of itself to be printed, giving the output:
5
Would regard the + as concatenating two strings. It would ask the value 3 to convert itself into a string (sounds strange but this is what happens. It would then produce the output:
2.03
The string "2.0" has the text of the value 3.0 added on the end. This difference in behaviour is all because of the context of the operation that is being performed. You can think of all of the variables in our program being tagged with metadata (there is that word again) which the compiler uses to decide what to do with them. The variable heightString is tagged with information that says "this is a string, use a plus with this and you concatenate". The variable woodLength is tagged with metadata which says "this is a double precision floating point value, use a plus with this and you perform arithmetic".
+ " feet"
Another concatenation here. We are adding the word feet on the end. Whenever I print a value out I always put the units on the end. This makes it much easier to ensure that the value makes sense.
)
The bracket marks the end of the parameter being constructed for the WriteLine method call. When the method is called the program first assembles a completed string out of all the components, adding (or concatenating) them to produce a single result. It then passes the resulting string value into the method which will print it out on the console.
24
A First C# Program
;
The semi-colon marks the end of this statement.
}
Now for some really important stuff. The program is essentially complete. We have added all the behaviours that we need. However, the compiler still needs to be told that we have reached the end of the program. This first close brace marks the end of the block of code which is the body of the Main method. A block of code starts with a { and ends with a }. When the compiler sees this it says to itself "that is the end of the Main method".
}
The second closing brace has an equally important job to the first. It marks the end of the class GlazerCalc. In C# everything exists inside a class. A class is a container for a whole bunch of things, including methods. If we want to (and we will do this later) we may put a number of methods into a class. For now however, we only want the one method in the class. And so we use the second closing brace to mark the end of the class itself.
Punctuation
That marks the end of our program. One of the things that you will have noticed is that there is an awful lot of punctuation in there. This is vital and must be supplied exactly as C# wants it, otherwise you will get what is called a compilation error. This simply indicates that the compiler is too stupid to make sense of what you have given it! You will quickly get used to hunting for and spotting compilation errors, one of the things you will find is that the compiler does not always detect the error where it takes place, consider the effect of missing out a "(" character. However, note that just because the compiler reckons your program is OK is no guarantee of it doing what you want! Another thing to remember is that the layout of the program does not bother the compiler, the following is just as valid: using System;class GlazerCalc{static void Main(){double width, height, woodLength, glassArea;string widthString, heightString;widthString = Console.ReadLine();width = double.Parse(widthString);heightString = Console.ReadLine();height = double.Parse(heightString);woodLength = 2 * ( width + height ) * 3.25 ;glassArea = 2 * ( width * height ) ;Console.WriteLine ( "The length of the wood is " + woodLength + " feet" ) ;Console.WriteLine( "The area of the glass is " + glassArea + " square metres" ) ;} } - although if anyone writes a program which is laid out this way they will get a smart rap on the knuckles from me!
25
Manipulating Data
In the first case we can hold the value exactly; you always have an exact number of these items, they are integral. When you are writing a specification you should worry about the precision to which values are to be held. Too much accuracy may slow the machine down - too little may result in the wrong values being used. In the second case we can never hold what we are looking at exactly. Even if you measure a piece of string to 100 decimal places it is still not going to give you its exact length - you could always get the value more accurately. These are real. A computer is digital, i.e. it operates entirely on patterns of bits which can be regarded as numbers. Because we know that it works in terms of ons and offs it has problems holding real values. To handle real values the computer actually stores them to a limited accuracy, which we hope is adequate (and usually is). This means that when we want to store something we have to tell the computer whether it is an integer or a real. We also need to consider the range of possible values that we need to hold so that we can choose the appropriate type to store the data. You tell C# about a variable you want to create by declaring it. The declaration also identifies the type of the thing we want to store. Think of this as C# creating a box of a particular size, specifically designed to hold items of the given type. The box is tagged with some metadata (there is that word again) so that the system knows what can be put into it and how that box can be used.
26
Manipulating Data
The standard integer type, int, can hold frighteningly large numbers in C#, in the range -2,147,483,648 to 2,147,483,647 If you want to hold even larger integers than this (although I've no idea why you'd want this) there is a long version. An example of an integer variable would be something which kept track of the number of sheep in a field:
int numberOfSheep;
This creates a variable with could keep track of over two thousand million sheep! It also lets a program manipulate "negative sheep" which is probably not meaningful (unless you run a sheep bank of course and let people borrow them). Remember that the language itself is unaware of any such considerations. If you want to make sure that we never have more than 1,000 sheep and the number of sheep never goes negative you must add this behaviour yourself. When you edit your program source using Visual Studio (or another code editor that supports syntax highlighting) you will find that the names of types that are built into the C# language (such as int and float) are displayed in blue, as shown above.
If I am using one of the "shorter" types the literal value is regarded by the compiler as being of that type:
sbyte tinyVal = 127;
In this statement the 127 is regarded as an sbyte literal, not an integer. This means that if I do something stupid:
sbyte tinyVal = 128;
27
Manipulating Data
(the maximum value that an sbyte can hold is 127) the compiler will detect that I have made a mistake and the program will not compile.
An example of a double variable could be something which held the width of the universe in inches:
double univWidthInInches;
Finally, if you want the ultimate in precision but require a slightly smaller range you can use the decimal type. This uses twice the storage space of a double and holds values to a precision of 28-29 digits. It is used in financial calculations where the numbers are not so large but they need to be held to very high accuracy.
decimal robsOverdraft;
You can also use exponents to express double and float values:
9.4605284E15
This is a double precision literal value which is actually the number of meters in a light year. If you put an f on the end it becomes a floating point literal value. Unlike the way that integers work, with real numbers the compiler is quite fussy about how they can and can't be combined. This is because when you move a value from a double precision variable into an ordinary floating point one some of the precision is lost. This means that you have to take special steps to make sure that you as programmer make clear that you want this to happen and that you can live with the consequences. This process is known as casting and we will consider it in detail a bit later.
28
Manipulating Data
char variables
A char is type of variable which can hold a single character. A character is what you get when you press a key on a keyboard or display a single character on the screen. C# uses a character set called UNICODE which can handle over 65,000 different character designs including a wide range of foreign characters. An example of a character variable could be something which held the command key that the user has just pressed:
char commandKey;
This means "the character A". It is what your program would get if you asked it to read a character off the keyboard and the user held down shift and pressed A. If you are editing your program using an editor that supports syntax highlighting a character literal is shown in red.
The effect of these escape sequences depends on the device you send them to. Some systems will make a beep when you send the Alert character to them. Some clear the screen when you send the Form feed character. You can use them as follows:
char beep = '\a' ;
29
Manipulating Data
Note that I have to put leading zeroes in front of the two hex digits.
string variables
A type of box which can hold a string of text. In C# a string can be very short, for example "Rob", or it can be very long, for example "War and Peace" (that is the book not the three words). A string variable can hold a line of text. However, because there are special characters which mean "take a new line" (see above) it is perfectly possible for a single string to hold a large number of lines of text. An example of a string variable could be something which holds the line that the user has just typed in:
string commandLine;
- and try to ring the bell. If I am just expressing text with no escape characters or anything strange I can tell the compiler that this is a verbatim string. I do this by putting an @ in front of the literal:
@"\x0041BCDE\a"
This can be useful when you are expressing things like file paths. The verbatim character has another trick, which is that you can use it to get string literals to extend over several lines:
@"The quick brown fox jumps over the lazy dog"
This expresses a string which extends over three lines. The line breaks in the string are preserved when it is stored.
30
Manipulating Data
2.2.5 Identifiers
In C# an identifier is a name that the programmer chooses for something in the program. The name of a variable is more properly called an identifier. We will see other places where we create identifiers. C# has some rules about what constitutes a valid identifier: All identifiers names must start with a letter. After the letter you can have either letters or numbers or the underscore "_" character.
Upper and lower case letters are different, i.e. Fred and fred are different identifiers. Here are a few example declarations, one of which are not valid (see if you can guess which one and why):
int fred ; float jim ; char 29yesitsme ;
One of the golden rules of programming, along with "always use the keyboard with the keys uppermost" is: Always give your variables meaningful names. According to the Mills and Boon romances that I have read, the best relationships are meaningful ones.
31
Manipulating Data
The convention in C# for the kind of variables that we are creating at the moment is to mix upper and lower case letters so that each word in the identifier starts with a capital:
float averageIceCreamPriceInPence;
This is sometimes called camel case, presumably because of the "humps" in the identifier which are caused by the capitals.
The first part of the program should be pretty familiar by now. Within the Main function we have declared three variables, first, second and third. These are each of integer type. The last three statements are the ones which actually do the work. These are assignment statements. An assignment gives a value to a specified variable, which must be of a sensible type (note that you must be sensible about this because the compiler, as we already know, does not know or care what you are doing). The value which is assigned is an expression. The equals in the middle is there mainly to confuse us, it does not mean equals in the numeric sense; I like to think of it as a gozzinta (see above). Gozzintas take the result on the right hand side of the assignment and drop it into the box on the left, which means that:
2 = second + 1;
- is a piece of programming naughtiness which would cause all manner of nasty errors to appear.
Expressions
An expression is something which can be evaluated to produce a result. We can then use the result as we like in our program. Expressions can be as simple as a single value and as complex as a large calculation. They are made up of two things, operators and operands.
32
Manipulating Data
Operands
Operands are things the operators work on. They are usually literal values or the identifiers of variables. In the program above first, second, third are identifiers and 2 is a literal value. A literal value is something which is literally there in the code. A literal value has a type associated with it by the compiler.
Operators
Operators are the things which do the work: They specify the operation to be performed on the operands. Most operators work on two operands, one each side. In the program above + is the only operator. Here are a few example expressions:
2 + 3 * 4 -1 + 3 (2 + 3) * 4
These expressions are worked out (evaluated) by C# moving from left to right, just as you would yourself. Again, just as in traditional maths all the multiplication and division is performed first in an expression, followed by the addition and subtraction. C# does this by giving each operator a priority. When C# works out an expression it looks along it for all the operators with the highest priority and does them first. It then looks for the next ones down and so on until the final result is obtained. Note that this means that the first expression above will therefore return 14 and not 20. If you want to force the order in which things are worked out you can put brackets around the things you want done first, as in the final example. You can put brackets inside brackets if you want, provided you make sure that you have as many open ones as close ones. Being a simple soul I tend to make things very clear by putting brackets around everything. It is probably not worth getting too worked up about this expression evaluation as posh people call it; generally speaking things tend to be worked out how you would expect them. For completeness here is a list of all operators, what they do and their precedence (priority). I am listing the operators with the highest priority first. * / + unary minus, the minus that C# finds in negative numbers, e.g. -1. Unary means applying to only one item. multiplication, note the use of the * rather than the more mathematically correct but confusing x. division, because of the difficulty of drawing one number above another on a screen we use this character instead Addition. subtraction. Note that we use exactly the same character as for unary minus.
This is not a complete list of all the operators available, but it will do for now. Because these operators work on numbers they are often called the numeric operators. But of course you should remember that some of them (for example +) can be used between other types of data as well. It is also possible to use operators in a way which causes values to be moved from one type to another. This can cause problems as we shall see now.
33
Manipulating Data
to perform will cause data to be lost from the program. It considers every operation in terms of "widening and narrowing" values.
This works fine because the floating point type can hold all the values supported by the integer type. However:
float x = 1; int i = x ;
- would cause the compiler to complain (even though at the moment the variable x only holds an integer value). The compiler is concerned that you may be discarding information by such an assignment and treats it as an error. Note that this applies within a floating point values as well, for example:
double d = 1.5; float f = d ;
- would cause an error as well, since the compiler knows that a double is wider than a float.
Casting
We can force C# to regard a value as being of a certain type by the use of casting. A cast takes the form of an additional instruction to the compiler to force it to regard a value in a particular way. You cast a value by putting the type you want to see there in brackets before it. For example:
double d = 1.5; float f = (float) d ;
In the above code the message to the compiler is "I don't care that this assignment could cause the loss of information. I, as the writer of the program, will take the responsibility of making sure that the program works correctly". You can regard casting as the compiler's way of washing its hands of the problem. If a program fails because data is lost it is not because the compiler did something silly. As we saw above, each type of variable has a particular range of possible values, and the range of floating point values is much greater than that for integers. This means that if you do things like this:
int i ; i = (int) 123456781234567890.999 ;
I do not think of this as a failing in C#. It gives you great flexibility, at the cost of assuming you know what you are doing....
- the cast is doomed to fail. The value which gets placed in i will be invalid. Nothing in C# checks for mistakes like this. It is up to you when you write your program to make sure that you never exceed the range of the data types you are using - the program will not notice but the user certainly will!
34
Manipulating Data
The code above takes 1.999 (which would be compiled as a value of type double) and casts it to int. This process discards the fractional part, which means that the variable I will end up with the value 1 in it, even though the original number was much closer to 2. You should remember that this truncation takes place whenever you cast from a value with a fractional part (float, double, decimal) into one without.
- will be treated with the contempt they richly deserve. The C# compiler knows that it is daft to divide the value 3.0 by the string "stupid". However, consider this:
float x ; x = 3.4 ;
This code looks perfectly legal. However, it is not. This is because the literal value 3.4 is a double precision value when expressed as a literal, and the variable x has been declared as a floating point. If I want to put a floating point literal value into a floating point variable I can use casting:
float x ; x = (float) 3.4 ;
This casts the double precision literal into a floating point value, so that the assignment works. To make life easier the creators of C# have added a different way we can express a floating point literal value in a program. If you put an f after the value this is regarded as a floating point value. This means that:
float x ; x = 3.4f ;
You might think that these would give the same result. Not so. The compiler thinks that the first expression, which involves only integers, should give an integer result. It therefore would calculate this to be the integer value 0 (the fractional part is always truncated). The second expression, because it involves a floating point value would however be evaluated to give a double precision floating point result, the more accurate answer of 0.5.
35
Manipulating Data
The way that an operator behaves depends on the context of its use. Later on we will see that the + operator, which normally performs a numeric calculation, can be used between strings to concatenate them together, i.e. "Ro" + "b" would give the result "Rob". If you want complete control over the particular kind of operator the compiler will generate for you the program must contain explicit casts to set the correct context for the operator.
using System; class CastDemo { static void Main () { int i = 3, j = 2 ; float fraction ; fraction = (float) i / (float) j ; Console.WriteLine ( "fraction : " + fraction ) ; } }
The (float) cast in the above tells the compiler to regard the values in the integer variables as floating point ones, so that we get 1.5 printed out rather than 1.
The interesting thing about this is that it is a pattern of behaviour which can be reused time and time again. As an example, consider another friend of yours, who runs a chemist shop. He wants a program that will work out the total cost of tablets that he buys, and the number of bottles that he needs. He enters the cost of the tablets and the number he wants. The tablets are always sold in bottles of 100. You can very easily modify your program to do this job, the only hard part is figuring out how many bottles that are needed for a particular number of tablets. If you just divide the number of tablets by 100 an integer division will give you the wrong answer (for any number of tablets less than 100 your program will tell you that 0 bottles are needed). One way to solve this is to add 99 to the number of tablets before you
36
Writing a Program
perform the division, forcing the number of bottles required to "round up" any number of tablets greater than 0. This makes the active part of the program as follows:
int bottleCount = ((tabletCount + 99) / 100) ; int salePrice = bottleCount * pricePerBottle ;
We can then put the rest of the code around this to make a finished solution: Remember that if you divide two integers the result is always rounded down and the fractional part discarded.
string pricePerBottleString = Console.ReadLine(); int pricePerBottle = int.Parse(pricePerBottleString); string tabletCountString = Console.ReadLine(); int tabletCount = int.Parse(tabletCountString); int bottleCount = ((tabletCount + 99) / 100) ; int salePrice = bottleCount * pricePerBottle ; Console.WriteLine ( "The number of bottles is " + bottleCount ) ; Console.WriteLine( "The total price is " + salePrice ) ;
The interesting thing here is that the program for the chemist is actually just a variation on the program for the double glazing salesman. Both conform to a pattern of behaviour (read data in, process it, print it out) which is common to many applications. Any time that you are asked to write a program that reads in some data, works out some answers and then prints out the result you can make use of this pattern. Part of the skill of a programmer is identifying the nature of a problem in terms of the best pattern to be used to solve it.
37
Writing a Program
It should be clear who wrote it, and when it was last changed. If you write something good you should put your name on it. If you change what you wrote you should add information about the changes that you made and why.
A big part of a well written program is the comments that the programmer puts there. A program without comments is a bit like an aeroplane which has an autopilot but no windows. There is a chance that it might take you to the right place, but it will be very hard to tell where it is going from the inside.
Block Comments
When the C# compiler sees the "/*" sequence which means the start of a comment it says to itself: Aha! Here is a piece of information for greater minds than mine to ponder. I will ignore everything following until I see a */ which closes the comment. As an example:
/* This program works out glass and wood required for a double glazing salesman. */
Be generous with your comments. They help to make your program much easier to understand. You will be very surprised to find that you quickly forget how you got your program to work. You can also use comments to keep people informed of the particular version of the program, when it was last modified and why, and the name of the programmer who wrote it even if it was you. If you are using an editor that supports syntax highlighting you will find that comments are usually displayed in green.
Line Comments
Another form of comment makes use of the // sequence. This marks the start of a comment which extends to the end of that particular line of code. It is useful for putting a quick note on the end of a statement:
position = position + 1 ; // move on to the next customer
I've annotated the statement to give the reader extra information about what it actually does.
38
Writing a Program
Every program ever written is composed of the three elements above, and very little else! You can use this to good effect when designing an overall view of how your program is going to work. At the moment we have only considered programs which run in a straight line and then stop. The path which a program follows is sometimes called its "thread of execution". When you call a function the thread of execution is transferred into the function until it is complete.
Conditional Execution - if
The program above to work out the wood and glass for our glazing man is nice; in fact our customer will probably be quite pleased with it. However, it is not perfect. The problem is not with the program, but with the user. If you give the program a window width of -1 it goes ahead and works out a stupid result. Our program does not have any checking for invalid widths and heights. The user might have grounds for complaint if the program fails to recognise that he has given a stupid value, in fact a number of cases are currently being fought in the United States courts where a program has failed to recognise invalid data, produced garbage and caused a lot of damage. What we want to do is notice the really stupid replies and tell the user that he has done something dubious. In our program specification, which we give the customer, we have said something like (this is our metadata): The program will reject window dimensions outside the following ranges:
width less than 0.5 metres width greater than 5.0 metres height less than 0.75 metres height greater than 3.0 metres
This means that we have done all we can; If the program gets 1 rather than 10 for the width then that is the users' problem, the important thing from our point of view is that the above specification stops us from being sued! In order to allow us to do this the program must notice naughty values and reject them. To do this we can use the construction:
if (condition) statement or block we do if the condition is true else statement or block we do if the condition is false
The condition determines what happens in the program. So what do we mean by a condition? C# has a way in which true and false can be explicitly stated in a program. We have already seen that the bool type is used to store this logical state. We can create conditions which return a logical result. These are called "logical conditions". Which is logical. The simplest condition is simply the value true or false, for example:
if (true) Console.WriteLine ( "hello mum" ) ;
This is valid, although rather useless as the condition is always true, so "hello mum" is always printed (note that we left the else portion off - this is OK because it is optional).
39
Writing a Program
==
equals. If the left hand side and the right hand side are equal the expression has the value true. If they are not equal the value is false.
4 == 5
- would evaluate to false. Note that it is not particularly meaningful to compare floating point variables to see if they hold exactly the same values. Because of the fact that they are held to limited precision you might find that conditions fail when they should not for example the following equation:
x = 3.0 * (1.0 / 3.0) ;
If you want to compare floating point values subtract them and see if the difference is very small.
- would be false - even though mathematically the test should evaluate to true.
!=
not equal. The reverse of equals. If the operands are not equal the expression has the value true, if they are equal it has the value false. Again, this test is not advisable for use with floating point numbers.
<
less than. If the operand on the left is less than the one on the right the value of the expression is true. If the left hand operand is larger than or equal to the right hand one the expression gives false. It is quite valid to compare floating point numbers in this way.
>
greater than. If the operand on the left is greater than the one on the right the result is true. If the operand on the left is less than or equal to the one on the right the result is false.
<=
less than or equal to. If the operand on the left is less than or equal to the one on the right you get true, otherwise you get false.
>=
greater than or equal to. If the operand on the left is greater than or equal to the one on the right you get true, otherwise it is false.
!
not. This can be used to invert a particular value or expression, for example you can say !true, which is false, or you could say: !(x==y) - which means the same as (x!=y). You use not when you want to invert the sense of an expression.
&&
and. If the operands each side of the && are true the result of the && is true. If one of them is false the result is false, for example
40
Writing a Program
- this would be true if the width was valid according to our above description. Ive put brackets around each of the conditions in this example, so that it is easier to see what is going on. However, the compiler is able to work out that the && needs to be applied between the result of the two logical expressions, so they are not actually required.
||
or. If either of the operands each side of the || are true the result of the expression is true. The expression is only false if both operands are false, for example:
width <= 0.5 || width >= 5.0
- this would be true if the width was invalid. We are saying that the width is invalid if it is less than the minimum or larger than the maximum. Note that to reverse the sense of the condition (i.e. true when the value is invalid) we not only have to change the > into <= in each expression, but also change the && into an ||. Using these operators in conjunction with the if construction we can make decisions and change what our program will do in response to the data we get.
The two statements are now a block, which is performed only if width is greater than 5.0. You can lump many hundreds of statements together in this way, the compiler does not mind. You can also put such blocks of code inside other blocks, this is called nesting. The number of { and } characters must agree in your program, otherwise you will get strange and incomprehensible errors when the compiler hits the end of the file in the middle of a block or reaches the end of your program half way down your file!
41
Writing a Program
values for heights and widths. These come from the metadata I was so careful to gather when I wrote the specification for the program. Now I could just use the values 0.5, 5.0, 0.75 and 3.0 - but these are not packed with meaning and make the program hard to change. If, for some reason, my maximum glass size becomes 4.5 metres I have to look all through the program and change only the appropriate values. I do not like the idea of "magic numbers" in programs, what I would like to do is replace each number with something a bit more meaningful. We can do this by making a variable which is constant, i.e. it can never be changed.
const double PI=3.141592654;
This both more meaningful (we are using PI not some anonymous value) and makes the program quicker to write. Anywhere you use a magic number you should use a constant of this form, for example:
const double MAX_WIDTH 5.0 ;
This makes your programs much easier to read, and also much easier to change. There is a convention that you always give constant variables names which are expressed in CAPITAL LETTERS. This is so that when you read your program you can tell which things have been defined. We can therefore modify our double glazing program as follows: using System; class GlazerCalc { static void Main() { double width, height, woodLength, glassArea; const const const const double double double double MAX_WIDTH = 5.0 ; MIN_WIDTH = 0.5 ; MAX_HEIGHT = 3.0 ; MIN_HEIGHT = 0.75 ;
string widthString, heightString; Console.Write ( "Give the width of the window : " ); widthString = Console.ReadLine(); width = double.Parse(widthString); if (width < MIN_WIDTH) { Console.WriteLine ( "Width is too small" ) ; Console.WriteLine ( "Using minimum" ) ; width = MIN_WIDTH ; } if (width > MAX_WIDTH) { Console.WriteLine ( "Width is too large.\n\n" ) ; Console.WriteLine ( "Using maximum" ) ; width = MAX_WIDTH ; } Console.Write ( "Give the height of the window : " ); heightString = Console.ReadLine(); height = double.Parse(heightString);
42
Writing a Program
if (height < MIN_HEIGHT) { Console.WriteLine( "Height Console.WriteLine ( "Using height = MIN_HEIGHT ; } if (height > MAX_HEIGHT) { Console.WriteLine( "Height Console.WriteLine ( "Using height = MAX_HEIGHT ; }
woodLength = 2 * ( width + height ) * 3.25 ; glassArea = 2 * ( width * height ) ; Console.WriteLine ( "The length of the wood is " + woodLength + " feet" ) ; Console.WriteLine( "The area of the glass is " + glassArea + " square metres" ) ; } } This program fulfils our requirements. It will not use values incompatible with our specification. However I would still not call it perfect. If our salesman gives a bad height the program simply limits the value and needs to be re-run, with the height having to be entered again. What we would really like is a way that we can repeatedly fetch values for the width and height until we get one which fits. C# allows us to do this by providing looping constructions.
2.3.3 Loops
Conditional statements allow you to do something if a given condition is true. However often you want to repeat something while a particular condition is true, or a given number of times. C# has three ways of doing this, depending on precisely what you are trying to do. Note that we get three methods not because we need three but because they make life easier when you write the program (a bit like an attachment to our chainsaw to allow it to perform a particular task more easily). Most of the skill of programming involves picking the right tool or attachment to do the job in hand. (the rest is finding out why the tool didn't do what you expected it to!). In the case of our program we want to repeatedly get numbers in until while we are getting duff ones, i.e. giving a proper number should cause our loop to stop. This means that if we get the number correctly first time the loop will execute just once. You might think that I have pulled a fast one here; all I have done is change: Get values until you see one which is OK into Get values while they are not OK Part of the art of programming is changing the way that you think about the problem to suit the way that the programming language can be told to solve it.
do -- while loop
In the case of our little C# program we use the do -- while construction which looks like this:
43
Writing a Program
This allows us to repeat a chunk of code until the condition at the end becomes false. Note that the test is performed after the statement or block, i.e. even if the test is bound to fail the statement is performed once. A condition in this context is exactly the same as the condition in an if construction, raising the intriguing possibility of programs like:
using System; class Forever { public static void Main () { do Console.WriteLine ( "Hello mum" ) ; while ( true ); } }
This is a perfectly legal C# program. How long it will run for is an interesting question, the answer contains elements of human psychology, energy futures and cosmology, i.e. it will run until: 1. 2. 3. You get bored with it. Your electricity runs out. The universe implodes.
This is a chainsaw situation, not a powerful chainsaw situation. Just as it is possible with any old chainsaw to cut off your leg if you try really hard so it is possible to use any programming language to write a program which will never stop. It reminds me of my favourite shampoo instructions: 1. 2. 3. 4. Wet Your Hair Add Shampoo and Rub vigorously. Rinse with warm water. Repeat.
I wonder how many people are still washing their hair at the moment?
while loop
Sometimes you want to decide whether or not to repeat the loop before you perform it. If you think about how the loop above works the test is done after the code to be repeated has been performed once. For our program this is exactly what we want, we need to ask for a value before we can decide whether or not it is valid. In order to be as flexible as possible C# gives us another form of the loop construction which allows us to do the test first:
while (condition) statement or block
Note that C makes an attempt to reduce the number of keys you need to press to run the program by leaving out the word do. (if you put the do in the compiler will take great delight in giving you an error message - but you had already guessed that of course!).
for loop
Often you will want to repeat something a given number of times. The loop constructions we have given can be used to do this quite easily:
44
Writing a Program
using System; class WhileLoopInsteadOfFor { public static void Main () { int i ; i = 1 ; while ( i < 11 ) { Console.WriteLine ( "Hello mum" ) ; i = i + 1 ; } } }
The variable which controls things is often called the control variable, and is usually given the name i.
This useless program prints out hello mum 10 times. It does this by using a variable to control the loop. The variable is given an initial value (1) and then tested each time we go around the loop. The control variable is then increased for each pass through the statements. Eventually it will reach 11, at which point the loop terminates and our program stops. C# provides a construction to allow you to set up a loop of this form all in one:
for ( setup ; finish test ; update ) { things we want to do a given number of times }
The setup puts a value into the control variable which it will start with. The test is a condition which must be true for the for -- loop to continue. The update is the statement which is performed to update the control variable at the end of each loop. Note that the three elements are separated by semicolons. The precise sequence of events is as follows: 1. 2. 3. 4. 5. Put the setup value into the control variable. Test to see if we have finished the loop yet and exit to the statement after the for loop if we have. Perform the statements to be repeated. Perform the update. Repeat from step 2.
Writing a loop in this way is quicker and simpler than using a form of while because it keeps all the elements of the loop in one place instead of leaving them spread about the program. This means that you are less likely forget to do something like give the control variable an initial value, or update it. If you are so stupid as to mess around with the value of the control variable in the loop you can expect your program to do stupid things, i.e. if you put i back to 0 within the loop it will run forever. And serve you right.
45
Writing a Program
Note that we are using two variables as switches, they do not hold values as such; they are actually used to represent states within the program as it runs. This is a standard programming trick that you will find very useful. You can break out of any of the three kinds of loop. In every case the program continues running at the statement after the last statement of the loop.
46
Writing a Program
Please do not go any further down this time round the loop. Go back to the top of the loop, do all the updating and stuff and go around if you are supposed to. In the following program the bool variable Done_All_We_Need_This_Time is set true when we have gone as far down the loop as we need to.
for ( item = 1 ; item < Total_Items ; item=item+1 ) { ..... item processing stuff .... if (Done_All_We_Need_This_Time) continue ; .... additional item processing stuff .... }
The continue causes the program to re-run the loop with the next value of item if it is OK to do so. You can regard it as a move to step 2 in the list above.
To perform this test we use one of the logical operators described above to write a condition which will be true if the width is invalid:
if ( width < MIN_WIDTH || width > MAX_WIDTH ) ..
47
Writing a Program
using System; class GlazerCalc { static void Main() { double width, height, woodLength, glassArea; const double MAX_WIDTH = 5.0 ; const double MIN_WIDTH = 0.5 ; const double MAX_HEIGHT = 3.0 ; const double MIN_HEIGHT = 0.75 ; string widthString, heightString; do { Console.Write ( "Give the width of the window between " + MIN_WIDTH + " and " + MAX_WIDTH + " :" ); widthString = Console.ReadLine(); width = double.Parse(widthString); } while ( width < MIN_WIDTH || width > MAX_WIDTH ) ; do { Console.Write ( "Give the height of the window between " + MIN_HEIGHT + " and " + MAX_HEIGHT + " :" ); heightString = Console.ReadLine(); height = double.Parse(heightString); } while ( height < MIN_HEIGHT || height > MAX_HEIGHT ); woodLength = 2 * ( width + height ) * 3.25 ; glassArea = 2 * ( width * height ) ; Console.WriteLine ( "The length of the wood is " + woodLength + " feet" ) ; Console.WriteLine( "The area of the glass is " + glassArea + " square metres" ) ; } }
In this case the operator is + and is operating on the variable window_count and the value 1. The purpose of the above statement is to add 1 to the variable window_count. However, it is a rather long winded way of expressing this, both in terms of what we have to type and what the computer will actually do when it runs the program. C# allows us to be terser if we wish, the line:
window_count++
- would do the same thing. We can express ourselves more succinctly and the compiler can generate more efficient code because it now knows that what we are doing is adding one to a particular variable. The ++ is called a unary operator, because it works on just one operand. It causes the value in that operand to be increased by one. There is a corresponding -- operator which can be used to decrease (decrement) variables. You can see examples of this construction in the for loop definition in the example above.
48
Writing a Program
The other shorthand which we use is when we add a particular value to a variable. We could put:
house_cost = house_cost + window_cost;
This is perfectly OK, but again is rather long winded. C# has some additional operators which allow us to shorten this to:
house_cost += window_cost;
The += operator combines addition and the assignment, so that the value in house_cost is increased by window_cost. Some other shorthand operators are: a += b a -= b a /= b a *= b the value in a is replaced by a + b the value in a is replaced by a b the value in a is replaced by a / b the value in a is replaced by a * b
There are other combination operators; I will leave you to find them!
This is perfectly legal (and perhaps even sensible) C#. It has the effect of setting both i and j to 0. An assignment statement always returns the value which is being assigned (i.e. the bit on the right of the gozzinta). This value can then be used as a value or operand. If you do this you are advised to put brackets around the statement which is being used as a value, this makes the whole thing much clearer for both you and the compiler! When you consider operators like ++ there is possible ambiguity, in that you do not know if you get the value before or after the increment. C# provides a way of getting either value, depending on which effect you want. You determine whether you want to see the value before or after the sum by the position of the ++ : i++ ++i Means Give me the value before the increment Means Give me the value after the increment
As an example:
int i = 2, j ; j = ++i ;
- would make j equal to 3. The other special operators, += etc. all return the value after the operator has been performed.
49
Writing a Program
The {n} part of the string says parameter number n, counting from 0. In the second write statement I have swapped the order of the numbers, but since I've swapped the order of the parameters too the output is the same. Of course if I do something mad, for example {99}, the WriteLine method will fail with an error.
The 0 characters stand for one or more digits. When placed after a decimal point they can be used to control the number of decimal places which are used to express a value. Note that doing this means that if the number is an integer it is printed out as 12.00.
Note that if I do this I get leading zeroes printed out, which is useful if you are printing things like cheques.
I have used the # character to get my thousands printed out with commas:
50
Writing a Program
i: 150 f: 1,234.57
Note that the formatter only uses the # characters and commas that it needs. The value 150 does not have a thousands digit so it and the comma are left out. Note also though that I have included a 0 as the smallest digit. This is so that when I print the value 0 I actually get a value printed, otherwise when I print zero I get nothing on the page.
Printing in columns
Finally I can add a width value to the print layout information. This is very useful if you want to print material in columns:
int i = 150 ; double f = 1234.56789 ; Console.WriteLine ( "i: {0,10:0} f: {1,15:0.00}", i, f ) ; Console.WriteLine ( "i: {0,10:0} f: {1,15:0.00}", 0, 0 ) ;
The integer value is printed in a column 10 characters wide, and the double is printed in a 15 character wide column. At the moment the output is right justified, if I want the numbers left justified I make the width negative:
int i = 150 ; double f = 1234.56789 ; Console.WriteLine ( "i: {0,-10:0} f: {1,-15:0.00}", i, f ) ; Console.WriteLine ( "i: {0,-10:0} f: {1,-15:0.00}", 0, 0 ) ;
Note that this justification would work even if you were printing a string rather than a number, so if you want to print columns of words you can use this technique to do it. You can specify the print width of any item, even a piece of text, which makes printing in columns very easy.
51
Creating Programs
Methods
3 Creating Programs
In this chapter we will build on our programming abilities to make programs that are broken down into manageable chunks and find out how a program can store and manipulate large amounts of data using arrays.
3.1 Methods
We have already come across the methods Main, WriteLine and ReadLine. Main is the method we write which is where our program starts. WriteLine and ReadLine were provided by the creators of C# to give us a way of displaying text and reading information from the user. This is what methods are all about. Your programs will contain methods that you create to solve parts of the problem and they will also use methods that have been provided by other people. In this section we are going to consider why methods are useful and how you can create your own.
We will need both of these when we start to write larger programs. Again, as with lots of features of the C# language, methods don't actually make things possible, but they do help with the organisation of our programs. Essentially you take a block of code and give it a name. Then you can refer to this block of code to do something for you. As a silly example:
52
Creating Programs
Methods
using System ; class MethodDemo { static void doit () { Console.WriteLine ("Hello"); } public static void Main () { doit(); doit(); } }
In the main method I make two calls of doit. Each time I call the method the code in the block which is the body of the method is executed. In this case it contains a single statement which prints "Hello" on the console. The result of running the above program would be:
Hello Hello
So, we can use methods to save us from writing the same code twice. We simply put the code inside a method body and then call it when we need it.
3.1.2 Parameters
At this point methods are useful because they let us use the same block of statements at many points in the program. However, they become more useful if we allow them to have parameters. A parameter is a means of passing a value into a method call. The method is given the data to work on. As an example, consider the code below:
using System ; class MethodDemo { static void silly ( int i) { Console.WriteLine ( "i is : " + i ) ; } public static void Main () { silly ( 101 ) ; silly ( 500 ) ; } }
The method silly has a single integer parameter. Within the block of code which is the body of this method we can use the parameter i as an integer variable. When the method starts the value supplied for the parameter is copied into it. This means that when the program runs we get output like this:
i is : 101 i is : 500
53
Creating Programs
Methods
using System ; class ReturnDemo { static int sillyReturnPlus ( int i) { i = i + 1; Console.WriteLine ( "i is : " + i ) ; return i; } public static void Main () { int res; res = sillyReturnPlus (5); Console.WriteLine ( "res is : " + res ) ; } }
The method sillyReturnPlus takes the value of the parameter and returns it plus one. The value that a method returns can be used anywhere in a program where a variable of that type could be used, in other words a call of sillyReturnPlus can take the place of an integer. See if you can work out what this code would display:
res = sillyReturnPlus (5) + sillyReturnPlus (7) + 1; Console.WriteLine ( "res is : " + res ) ;
It is actually OK to ignore the value returned by a method, but this is actually a rather pointless thing to do:
sillyReturnPlus (5); // will compile but do nothing useful
The readValue method is told the prompt to use and the lowest and the highest allowed values. It can then be used to read values and make sure that they are in range. We can use this method to read anything and make sure that value supplied is within a particular range.
double windowWidth = readValue ( "Enter width of window: ", MIN_WIDTH, MAX_WIDTH) ; double age = readValue ( "Enter your age: ", 0, 70) ;
The first call of readValue gets the width of a window. The second reads an age between 0 and 70.
54
Creating Programs
Methods
Moving code around and creating methods is called refactoring. This will be an important part of the Software Engineering we do later.
Method Limitations
A method is very good for getting work done, but it can be a bit limited because of the way that it works. For example, if I want to write a method which reads in the name and the age of a person I have a problem. From what we have seen of methods, they can only return one value. So I could write a method which returns the name of a person, or write one which returns an age. But I cant write a method that returns both values at the same time as a method can only return one value. As you will find out when you try, changing the value of a parameter does not change the value of the thing passed in. This is because, unless you specify otherwise, only the value of a parameter is passed into a call to a method.
The method addOneToParam adds one to the parameter; prints the result out and then returns:
int test = 20 ; addOneToParam(test); Console.WriteLine ( "test is : " + test ) ;
The piece of C# above calls the method with the variable test as the parameter. When it runs it prints out the following:
i is : 21 test is : 20
It is very important that you understand what is happening here. The value of test is being used in the call of addOneToParam. The program works out the result of the expression to be passed into the method call as a parameter. It then passes this value into the call. This means that you can write calls like:
test = 20 ; addOneToParam(test + 99);
Pass by value is very safe, because nothing the method does can affect variables in the code which calls it. However, it is a limitation when we want to create a method which returns more than one value.
55
Creating Programs
Methods
Note that the keyword ref has been added to the information about the parameter.
test = 20 ; addOneToRefParam(ref test); Console.WriteLine ( "test is : " + test ) ;
The code above makes a call to the new method, and also has the word ref in front of the parameter. In this case the output is as follows:
i is : 21 test is : 21
In this case the method call has made changes to the content of the variable. Note that C# is careful about when a parameter is a reference. You have to put the word ref in the method heading and also in the call of the method.
56
Creating Programs
Methods
static void readPerson ( out string name, out int age ) { name = readString ( "Enter your name : " ) ; age = readInt ( "Enter your age : ", 0, 100 ) ; }
The method readPerson reads the name and the age of a person. Note that it uses two more methods that I have created, readString and readInt. I can call readPerson as follows:
string name ; int age ; readPerson ( out name, out age ) ;
Note that I must use the out keyword in the call of the method as well. The readPerson method will read the person and deliver the information into the two variables.
string intString = readString (prompt) ; result = int.Parse(intString); } while ( ( result < low ) || ( result > high ) ); return result; }
The readString method will read text and make sure that the user does not enter empty text. The readInt method reads a number within a particular range. Note how I have rather cleverly used my readString method in my readInt one, so that the user can't enter an empty string when a number is required. We can use the methods as follows:
57
Creating Programs
string name; name = readString( "Enter your name : " ); int age; age = readInt ( "Enter your age : ", 0, 100);
I could add methods to read floating point values as well. In fact one thing I tend to do when working on a project is create little library of useful methods like these which I can use.
The variable j has the scope of the inner block. This means that only statements in the inner block can use this variable. In other words the code:
58
Creating Programs
{ int i ; { int j ; } j = 99 ; }
- would cause an error, as the variable j does not exist at this point in the program. In order to keep you from confusing yourself by creating two versions of a variable with the same name, C# has an additional rule about the variables in the inner blocks:
{ int i ; { int i ; } }
This is not a valid program because C# does not let a variable in an inner block have the same name as one in an outer block. This is because inside the inner block there is the possibility that you may use the "inner" version of i when you intend to use the outer one. In order to remove this possibility the compiler refuses to allow this. Note that this is in contrast to the situation in other languages, for example C++, where this behaviour is allowed. It is however perfectly acceptable to reuse a variable name in successive blocks because in this situation there is no way that one variable can be confused with another.
{ int i ; } { int i ; { int j ; } }
The first incarnation of i has been destroyed before the second, so this code is OK.
The variable i is declared and initialized at the start of the for loop and only exists for the duration of the block itself.
59
Creating Programs
class LocalExample { static void OtherMethod () { local = 99; // this will not compile } static void Main () { int local = 0; Console.WriteLine ("local is :" + local); } }
The variable local is declared and used within the Main method, and cant be used anywhere else. If a statement in OtherMethod tries to use local the program will fail to compile.
The variable member is now part of the class MemberExample, and so the Main method and OtherMethod can both use this variable. The program above would print out:
member is : 0 member is now : 99
This is because the call of OtherMethod would change the value of member to 99 when it runs. Class variables are very useful if you want to have a number of methods sharing a set of data. For example, if you were creating a program to play chess it would be sensible to make the variable that stores the board into a member of the class. Then the methods that read the player move, display the board, and calculate the computer move could all use the same board information.
60
Creating Programs
Arrays
One common programming mistake is to confuse static with const. Marking a variable as const means the value cannot be changed. Marking a variable with static means the variable is part of the class and is always present. If it helps you can think of static as something which is always with us, like the background static noise on your radio when you tune it off station. Alternatively you can think of it as stationary, and therefore not going anywhere.
3.3 Arrays
We now know how to create programs that can read values in, calculate results and print them. Our programs can also make decisions based on the values supplied by the user and also repeat actions a given number of times. It turns out that you now know about nearly all the language features that are required to implement every program that has ever been written. Only one thing is missing, and that is the ability to create programs which store large amounts of data. Arrays are one way to do this, and we are going to find out about them next.
Now you can start putting the data into each variable. You can use your new number reading methods for this:
score1 = readInt ( "Player 1 score : score2 = readInt ( "Player 2 score : score3 = readInt ( "Player 3 score : score4 = readInt ( "Player 4 score : score5 = readInt ( "Player 5 score : score6 = readInt ( "Player 6 score : score7 = readInt ( "Player 7 score : score8 = readInt ( "Player 8 score : score8 = readInt ( "Player 9 score : score10 = readInt ( "Player 10 score score11 = readInt ( "Player 11 score ", 0,1000); ", 0,1000); ", 0,1000); ", 0,1000); ", 0,1000); ", 0,1000); ", 0,1000); ", 0,1000); ", 0,1000); : ", 0,1000); : ", 0,1000);
61
Creating Programs
Arrays
All we have to do next is sort them..... Hmmmm..... This is awful! There seems to be no way of doing it. Just deciding whether score1 is the largest value would take an if construction with 10 comparisons! Clearly there has to be a better way of doing this, after all, we know that computers are very good at sorting this kind of thing. C# provides us with a thing called an array. An array allows us to declare a whole row of a particular kind of box. We can then use things called subscripts to indicate which box in the row that we want to use. Consider the following:
using System; class ArrayDemo { public static void Main () { int [] scores = new int [11] ; for ( int i=0; i<11; i=i+1) { scores [i] = readInt ( "Score : ", 0,1000); } } }
The int [] scores part tells the compiler that we want to create an array variable. You can think of this as a tag which can be made to refer to a given array. The bit which makes the array itself is the new int [11]. When C# sees this it says Aha! What we need here is an array. It then gets some pieces of wood and makes a long thin box with 11 compartments in it, each large enough to hold a single integer. It then paints the whole box red - because boxes which can hold integers are red. It then gets a piece of rope and ties the tag scores to this box. If you follow the rope from the scores tag you reach the array box. Actually, it probably doesn't use wood or rope, but you should get a picture of what is going on here.
- is quite OK. (as long as you don't fall off the end of the array). When an array is created all the elements in the array are set to 0.
62
Creating Programs
Arrays
The variable i now ranges from 0 to 999, vastly increasing the amount of data we are storing.
A constant variable is given a value when it is declared. This value can then only be read by the program, never updated. So, if I wanted to write a scores program that could be easily changed for any size of team I could write:
using System; class ArrayDemo { public static void Main () { const int SCORE_SIZE = 1000; int [] scores = new int [SCORE_SIZE] ; for ( int i=0; i < SCORE_SIZE; i=i+1) { scores [i] = readInt ( "Score : ", 0,1000); } } }
The variable SCORE_SIZE is an integer which has been marked with const. This means that it cannot be changed by statements within the program. It will never have a value other than 1000. There is a convention that constants of this kind are given in LARGE LETTERS with an underscore between words. Everywhere I previously used a fixed value to represent the size of the array I now use my constant instead. This means that if the size of the team changes I just have to change the value assigned when the constant is declared and then re-compile the program. The other benefit of this is that the for loop now looks a lot more meaningful. Since the value of i is now going from 0 to SCORE_SIZE it is more obvious to the reader that it is working through the score array.
63
Creating Programs
Arrays
of arrays" if you like (but only if this doesn't make your head hurt). For example, to hold the board for a game of noughts and crosses (tic tac toe) we could use:
int [,] board = new int [3,3]; board [1,1] = 1;
This looks very like our one dimensional array, but there are some important differences. The [,] now has a comma. The presence of a comma implies something each side of it. This means that the array now has two dimensions, rather than just one. So when we give the size of the board we must supply two dimensions rather than just one. Then, when we want to specify an element we have to give two subscript values. In the code above I've set the value in the middle square (the best one) to 1. You can think of a 2D array as a grid: 0 0 1 2 0 0 0 0 1 0 1 0 0 0 2
The first subscript will specify which column and the second which row. The diagram above shows the board with the best move already taken. In the example above the array is square (i.e. the same dimension across as up). We can change this if we like:
int [,] board = new int [3,10];
- but this would make it rather hard to play a sensible game on.
This code creates a three dimensional board and then gets the highly valuable location right in the middle of the game cube. You can go to more than three dimensions if you like, in that C# does not have a problem with this. However, you might have big problems because this is very hard to understand and visualise. Note that sometimes you might think that you need to add a second dimension, when what you really want to do is add another array. For example, to go back to our cricket scores program, if the team captain wants to store the names of the players you would not do this by adding another dimension to the scores array, instead you would declare a second array of strings to hold the name of each player:
int [] scores = new int [11] ; string [] names = new string [11] ;
Your program would have to make sure that the name and score information for each player always lined, in other words the element at subscript 0 in the names array would contain the name of the player scoring the value at element 0 in the scores array.
64
Creating Programs
The program is asking the user to enter their age. The user is entering their age, but they are not using numbers. This causes your program to get rather upset:
Unhandled Exception: System.FormatException: Input string was not in a correct format. at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) at RobsProgram.Main(String[] args)
This does seem very unfair. Someone daft enough to type their age as words is able to break the program. In this section we will find out how the C# language manages errors and how to make our programs much harder to break.
The above statement uses int.Parse to convert the string in ageString into an integer result which is then stored in the variable age. This works well, but it is not without its limitations. The problem with Parse is that if you give it a string that contains invalid text it doesnt know what to do with it. At work if I ever get a problem that I cant deal with I solve the problem by passing it onto my boss. After all, the boss earns the big bucks and is paid to sort things out. Parse solves its problems by throwing an exception for another part of the program to catch. This is the C# equivalent of a note to the boss that says I dont know what to do with this. Any ideas? If nothing catches the exception (in effect the boss is not around to deal with the problem) then the exception will finish the program. The C# runtime system will display the contents of the exception then stop, as it did above.
65
Creating Programs
int age; try { age = int.Parse(ageString); Console.WriteLine("Thank you"); } catch { Console.WriteLine("Invalid age value"); }
The code above uses Parse to decode the age string. However, the parse action takes place inside the try block. If the call of Parse throws an exception the code in the catch block runs and will display a message to the user. Note that once the exception has been thrown there is no return to the code in the try block, i.e. if the parse fails the program will not display the message "Thank you".
The catch now looks like a method call, with the Exception e being a parameter to the method. This is exactly how it works. Within the catch clause the value of e is set to the exception that was thrown by Parse. The Exception type has a property called Message which contains a string describing the error. If a user types in an invalid string the program above will write the text in the exception, which contains the message:
Input string was not in a correct format.
This message is obtained from the exception that was thrown. This is useful if the code inside the try block could throw several different kinds of exception, since it means that the message reflects what has actually happened. The Exception object also contains other properties that can be useful.
66
Creating Programs
try { // Exceptions at this level will be caught by the // "outer" catch clause try { // Exceptions at this level will be caught by the // "inner" catch clause } catch (Exception inner) { // This is the "inner" catch clause } // Exceptions at this level will be caught by the // "outer" catch clause } catch (Exception outer) { // This is the "outer" catch clause }
The code above shows how this works. If code in the innermost block throws an exception the run time system will find the inner catch clause. However, once execution leaves that inner block the outer catch is the one which will be run in the event of an exception being thrown.
67
Creating Programs
In the above code the statements in the finally part are guaranteed to run, either when the statements in the try part have finished, or just before execution leaves the catch part of your program.
The statement above makes a new exception and then throws it. When you make a new exception you can give it a string that contains the message the exception will deliver. In this case I have used the somewhat unhelpful message Boom. Throwing an exception might cause your program to end if the code does not run inside a try catch construction.
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:
68
Creating Programs
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.
This would work OK, but is rather clumsy. You have to write a large number of if constructions to activate each option.
69
Creating Programs
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:
70
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 Id 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" : ....
71
Creating Programs
Using Files
C# program stream
File in Windows
A C# program can contain an object representing a particular stream that a programmer has created and connected to a file. The program performs operations on the file by calling methods on the stream object to tell it what to do. C# has a range of different stream types which you use depending on what you want to do. All of the streams are used in exactly the same way. In fact you are already familiar with how streams are used, since the Console class, which connects a C# program to the user, is implemented as a stream. The ReadLine and WriteLine methods are commands you can give any stream that will ask it to read and write data. We are going to consider two stream types which let programs use files; these are the StreamWriter and StreamReader types.
The variable writer will be made to refer to the stream that you want to write into. When the new StreamWriter is created the program will open a file called test.txt for output and connect the stream to it. If this process fails for any reason, perhaps your operating system is not able/allowed to write to the file or the name is invalid, then the action will fail with an appropriate exception. Note however that this code does not have a problem if the file test.txt already exists. All that happens is that a brand new, empty, file is created in place of what was there. This is potentially dangerous. It means that you could use the two statements above to completely destroy the contents of an existing file, which would be bad. Most useful programs ask the user whether or not an existing file should be overwritten, you will find out later how to do this.
The above statement calls the WriteLine method on the stream to make it write the text hello world into the file test.txt. This is exactly the same technique that is used to write information to the console for the user to read. In fact you can use all the writing features including those we explored in the neater printing section to format your output. Each time you write a line to the file it is added onto the end of the lines that have already been written. If your program got stuck writing in an infinite loop it is possible that it might fill up the storage device. If this happens, and the write cannot be performed successfully, the call of WriteLine will throw an exception. A properly written program should probably make sure that any exceptions like this (they can also be thrown when you open a file) are caught and handled correctly.
72
Creating Programs
Using Files
When the Close method is called the stream will write out any text to the file that is waiting to be written and disconnect the program from the file. Any further attempts to write to the stream will fail with an exception. Once a file has been closed it can then be accessed by other programs on the computer, i.e. once the close has been performed you can use the Notepad program to open test.txt and take a look at what is inside it. Forgetting to close a file is bad for a number of reasons: It is possible that the program may finish without the file being properly closed. In this situation some of the data that you wrote into the file will not be there. If your program has a stream connected to a file other programs may not be able to use that file. It will also be impossible to move or rename the file. An open stream consumes a small, but significant, part of operating resource. If your program creates lots of streams but does not close them this might lead to problems opening other files later on.
The above uses the fully qualified name of the console resource and calls the method WriteLine provided by that resource. However, weve not had to use this form because at the start of our programs we have told the compiler to use the System namespace to find any names it hasnt seen before. The using keyword allows us to tell the compiler where to look for resources.
using System;
This statement tells the compiler to look in the System namespace for resources. Once we have specified a namespace in a program file we no longer need to use the fully qualified name for resources from that namespace. Whenever the compiler finds an
73
Creating Programs
Using Files
item it hasnt seen before it will automatically look in the namespaces it has been told to use. In other words, when the compiler sees the statement:
Console.WriteLine("Hello World");
- it knows to look in the System namespace for that object so that it can use the WriteLine method on it. If the programmer miss-types the class name:
Consle.WriteLine("Hello World");
- the compiler will look in the System namespace, fail to find an object called Console and generate a compilation error. This is the same error that you will get if you try to use the StreamWriter class without telling the compiler to look in the System.IO namespace. In other words, to use the file handing classes you will need to add the following statement at the very top of your program:
using System.IO;
It is possible to put one namespace inside another (just like a librarian would put a cabinet of Vases in the Roman room which he could refer to as Roman.Vases) and so the IO namespace is actually held within the System namespace. However, just because you use a namespace, this does not imply that you use all the namespaces defined within it, and so you must include the above line for file handling objects to be available. Namespaces are a great way to make sure that names of items that you create dont clash with those from other programmers. We will see how you can create your own namespaces later on.
The above program connects a stream to the file Test.txt, reads the first line from the file, display it on the screen and then close the stream. If the file cant be found then the attempt to open it will fail and the program will throw an exception.
The above program will open up the file test.txt and display every line in the file on the console. The while loop will stop the program when the end of the file is reached.
74
Creating Programs
Using Files
The above statements create a string variable which contains the path to a file called sales.txt. This file is held in the folder November, which is in turn held in the folder 2009, which is held in the folder data which is on drive C. The backslash (\) characters in the string serve to separate the folders along the path to the file. Note that I have specified a string literal that doesnt contain control characters (that is what the @ at the beginning of the literal means) as otherwise the \ characters in the string will get interpreted by C# as the start of a control sequence. If you have problems where your program is not finding files that you know are there, Id advise you to make sure that your path separators are not getting used as control characters.
75
Creating Solutions
4 Creating Solutions
4.1 Our Case Study: Friendly Bank
The bulk of this section is based on a case study which will allow you to see the features of C# in a strong context. You are taking the role of a programmer who will be using the language to create a solution for a customer. The program we are making is for a bank, the "United Friendly and Really Nice Bank of Lovely People ", otherwise known as the Friendly Bank. We will be creating the entire bank application using C# and will be exploring the features of C# that make this easy. It is unlikely that you will get to actually implement an entire banking system during your professional career as a programmer (although it might be quite fun and probably rather lucrative). However, from a programming point of view it is an interesting problem and as we approach it we will uncover lots of techniques which will be useful in other programs that we might write.
Bank Notes
At the end of some sections there will be a description of how this new piece of C# will affect how we create our bank system. These notes should put the feature into a useful context.
76
Creating Solutions
Enumerated Types
they know about programming they'll just say "Oh, you mean youve numbered some states" and not be that taken with it.
Sample states
Enumerated types are very useful when storing state information. States are not quite the same as other items such as the name of a customer or the balance of their account. For example, if I am writing a program to play the game Battleships (where squares of the "sea" hold different types of craft which can be attacked) I may decide that a given square of the sea can have the following thing in it: Empty sea Attacked Battleship Cruiser Submarine Rowing boat
If you think about it, I am sort of assembling more metadata here, in that I have decided that I need to keep track of the sea and then I have worked out exactly what I can put in it. I could do something with numbers if I like: Empty sea = 1 Attacked = 2 Battleship = 3 Cruiser = 4 Submarine = 5 Rowing boat = 6
However, this would mean that I have to keep track of the values myself and remember that if we get the value 7 in a sea location this is clearly wrong. C# has a way in which we can create a type which has just a particular set of possible values. These types are called "enumerated types":
enum SeaState { EmptySea, Attacked, Battleship, Cruiser, Submarine, RowingBoat } ;
I have created a type called SeaState which can be used to hold the state of a particular part of the sea. It can only have the given values above, and must be managed solely in terms of these named enumerations. For example I must write:
SeaState openSea ; openSea = SeaState.EmptySea;
My variable openSea is only able to hold values which represent the state of the sea contents. Of course C# itself will actually represent these states as particular numeric values, but how these are managed is not a problem for me.
77
Creating Solutions
Enumerated Types
Note that types I create (like SeaState) will be highlighted by the editor in a blue colour which is not quite the same as keywords. This shows that these items are extra types I have created which can be used to create variables, but they are not actually part of the C# language, like keywords are. It is important that you understand what is happening here. Previous we have used types that are part of C#, for example int and double. Now we have reached the point where we are actually creating our own data types which can be used to hold data values that are required by our application.
Every time that you have to hold something which can take a limited number of possible values, or states (for example OnSale, UnderOffer, Sold, OffTheMarket etc) then you should think in terms of using enumerated types to hold the values.
We now have a variable which can hold state information about an account in our bank. Every account will contain a variable of type AccountState which represents the state of that account.
78
Creating Solutions
Structures
4.3 Structures
Structures let us organise a set of individual values into a cohesive lump which we can map onto one of the items in the problem that we are working on. This is important in many applications.
A sample structure
From your specification you know that the program must hold the following: customer name - string customer address - string account number - integer value account balance - integer value overdraft limit - integer value
The Friendly Bank have told you that they will only be putting up to 50 people into your bank storage so, after a while you come up with the following:
const int MAX_CUST = 50; AccountState [] states = new AccountState [MAX_CUST] ; string [] names = new string [MAX_CUST] ; string [] addresses = new string [MAX_CUST] ; int [] accountNos = new int [MAX_CUST] ; int [] balances = new int [MAX_CUST] ; int [] overdraft = new int [MAX_CUST] ;
What we have is an array for each single piece of data we want to store about a particular customer. If we were talking about a database (which is actually what we are writing), the lump of data for each customer would be called a record and an individual part of that lump, for example the overdraft value, would be called a field. In our program we are working on the basis that balance[0] holds the balance of the first customer in our database, overdraft [0] holds the overdraft of the first customer, and so on. (Remember that array subscript values start at 0). This is all very well, and you could get a database system working with this data structure. However it would me much nicer to be able to lump your record together in a more definite way.
79
Creating Solutions
Structures
struct Account { public AccountState State; public string Name ; public string Address ; public int AccountNumber ; public int Balance ; public int Overdraft ; } ;
This defines a structure, called Account, which contains all the required customer information. Having done this we can now define some variables:
Account RobsAccount ; Account [] Bank = new Account [MAX_CUST];
The first declaration sets up a variable called RobsAccount, which can hold the information for a single customer. The second declaration sets up an entire array of customers, called Bank which can hold all the customers. We can assign one structure variable to another, just as we would any other variable type. When the assignment is performed all the values in the source structure are copied into the destination:
Bank[0] = RobsAccount;
This would copy the information from the RobsAccount structure into the element at the start of the Bank array. We refer to individual members of a structure by putting their name after the struct variable we are using with a . (full stop) separating them, for example:
RobsAccount.AccountNumber
- would refer to the integer field AccountNumber in the structured variable RobsAccount. (i.e. the AccountNumber value in RobsAccount) You can do this with elements of an array of structures too, so that:
Bank [25].Name
- would be the string containing the name of the customer in the element with subscript 25.
80
Creating Solutions
Structures
// structure struct Account { public AccountState State; public string Name ; public string Address ; public int AccountNumber ; public int Balance ; public int Overdraft ; } ; // program class BankProgram { public static void Main () { Account RobsAccount ; RobsAccount.State = AccountState.Active; RobsAccount.Name = "Rob Miles"; RobsAccount.Address = "His house"; RobsAccount.AccountNumber = 1234; RobsAccount.Balance = 0; RobsAccount.Overdraft = -1000; Console.WriteLine ( "Name is : " + RobsAccount.Name ) ; Console.WriteLine ( "Balance is : " + RobsAccount.Balance ) ; } } Note how the structure and enum are declared outside the BankProgram class. Once I have created my structure I can use it in the same way that I would use something like int or float. This program doesnt create an array of structures, but it does show you how to access the various fields in a single structure variable.
- would produce a compilation error. It is your job as programmer to make sure that you always put a value into a variable before you try to get something out of it.
This method provides a quick way of printing out the contents of an account variable:
PrintAccount (RobsAccount);
81
Creating Solutions
It is also possible to create methods that return results which are of a structure type, for example we could create a ReadAccount method that reads an account and returns it.
This would hold information about a particular window in a house, including the dimensions of the window, the state of the window order and a string describing the window itself. For a particular house, which contains a number of windows, we could create an array of Window structures.
82
Creating Solutions
We want to be able to break a large system down into distinct and separate components which can be developed independently and interchanged with others which do the same task, i.e. we want to get one team of programmers working on accounts, another on cheques, another on credit cards etc. We want to make sure that the effort involved with making new types of bank account is as small as possible, i.e. if the bank decides to introduce a new high interest deposit account we want to be able to make use of existing deposit account
To do all these things we are going to have to start to consider programs from the point of view of object based design. This section should come with some kind of a health warning along the lines of "some of these ideas might hurt your head a bit at the start". But the following points are also very important: objects dont add any new behaviours to our programs we know just about everything we need to know to write programs when we have learnt about statements, loops, conditions and arrays. objects are best regarded as a solution to the problem of design. They let us talk about systems in general terms. We can go back and refine how the objects actually do their tasks later.
You can write just about every program that has ever been written just by using the technologies that we have so far looked at. But objects allow us to work in a much nicer way. And so we are going to have to get the hang of them, like it or not
This implements a very simple bank account, where we are only holding the name of the account holder. The Main method creates a structure variable called RobsAccountStruct. RobsAccountStruct
Name: Rob
83
Creating Solutions
It then sets the name property of the variable to the string "Rob". If we run this program it does exactly what you would expect, in that it prints out the name "Rob". If the structure contained other items about the bank account these would be stored in the structure as well, and I could use them in just the same way.
The account information is now being held in a class, rather than a structure. The account class is called, quite simply, Account. The problem is that when we compile the program we get this:
ObjectDemo.cs(12,3): error CS0165: Use of unassigned local variable ' RobsAccount'
So, what is going on? To understand what is happening you need to know what is performed by the line:
Account RobsAccount;
This looks like a declaration of a variable called RobsAccount. But in the case of objects, this is not what it seems.
RobsAccount
What you actually get when the program obeys that line is the creation of a reference called RobsAccount. Such references are allowed to refer to instances of the Account. You can think of them as a bit like a luggage tag, in that they can be tied to something with a piece of rope. If you have the tag you can then follow the rope to the object it is tied to. But when we create a reference we don't actually get one of the things that it refers to. The compiler knows this, and so it gives me an error because the line:
RobsAccount.Name = "Rob";
- is an attempt to find the thing that is tied to this tag and set the name property to "Rob". Since the tag is presently not tied to anything our program would fail at this point. The compiler therefore says, in effect, "you are trying to follow a reference which does not refer to anything, therefore I am going to give you a 'variable undefined' error". We solve the problem by creating an instance of the class and then connecting our tag to it. This is achieved by adding a line to our program:
84
Creating Solutions
class Account { public string Name ; } ; class StructsAndObjectsDemo { public static void Main () { Account RobsAccount ; RobsAccount = new Account(); RobsAccount.Name = "Rob"; Console.WriteLine (RobsAccount.Name ); } }
The line I have added creates a new Account object and sets the reference RobsAccount to refer to it. Account RobsAccount Name: Rob
We have seen this keyword new before. We use it to create arrays. This is because an array is actually implemented as an object, and so we use new to create it. The thing that new creates is an object. An object is an instance of a class. I'll repeat that in a posh font:
4.4.2 References
We now have to get used to the idea that if we want to use objects, we have to use references. The two come hand in hand and are inseparable. Structures are kind of useful, but for real object oriented satisfaction you have to have an object, and that means that we must manage our access to a particular object by making use of references to it. Actually this is not that painful in reality, in that you can treat a reference as if it really was the object just about all of the time, but you must remember that when you hold a reference you do not hold an instance, you hold a tag which is tied onto an instance
85
Creating Solutions
Account RobsAccount ; RobsAccount = new Account(); RobsAccount.Name = "Rob"; Console.WriteLine (RobsAccount.Name ); Account Temp ; Temp = RobsAccount; Temp.Name = "Jim"; Console.WriteLine (RobsAccount.Name );
The question is; what would the second call of WriteLine print out? If we draw a diagram the answer becomes clearer: Account RobsAccount
Name: Jim
Temp
Both of the tags refer to the same instance of Account. This means that any changes which are made to the object that Temp refers to will also be reflected in the one that RobsAccount refers to, because they are the same object. This means that the program would print out Jim, since that is the name in the object that RobsAccount is referring to. This indicates a trickiness with objects and references. There is no limit to the number of references that can be attached to a single instance, so you need to remember that changing the object that a reference refers to may well change that instance from the point of view of other objects.
No References to an Instance
Just to complete the confusion we need to consider what happens if an object has no references to it:
Account RobsAccount ; RobsAccount = new Account(); RobsAccount.Name = "Rob"; Console.WriteLine (RobsAccount.Name ); RobsAccount = new Account(); RobsAccount.Name = "Jim"; Console.WriteLine (RobsAccount.Name );
This code makes an account instance, sets the name property of it to Rob and then makes another account instance. The reference RobsAccount is made to refer to the new item, which has the name set to Jim. The question is: What happens to the first instance? Again, this can be made clearer with a diagram:
86
Creating Solutions
Account Name: Jim The first instance is shown hanging in space, with nothing referring to it. As far as making use of data in the instance is concerned, it might as well not be there. Indeed the C# language implementation has a special process, called the Garbage Collector which is given the job of finding such useless items and disposing of them. Note that the compiler will not stop us from letting go of items like this. You should also remember that you can get a similar effect when a reference to an instance goes out of scope:
{ Account localVar ; localVar = new Account(); }
The variable localVar is local to the block. This means that when the program execution leaves the block the local variable is discarded. This means that the only reference to the account is also removed, meaning another job for the garbage collector.
87
Creating Solutions
Sorting by moving objects around If we held the accounts as an array of structure items we would have to do a lot of work just to keep the list in order. The bank may well want to order the information in more than one way too, for example they might want to order it on both customer surname and also on account number. Without references this would be impossible. With references we just need to keep a number of arrays of references, each of which is ordered in a particular way:
Sorting by using references If we just sort the references we dont have to move the large data items at all. New objects can be added without having to move any objects, instead the references can be moved around.
Sorting by use of a tree In the tree above each node has two references; one can refer to a node which is lighter, the other to a node which is darker. If I want a sorted list of the items I just have to go as far down the lighter side as I can and I will end up at the lightest. Then
88
Creating Solutions
I go up to the one above that (which must be the next lightest). Then I go down the dark side (Luke) and repeat the process. The neat thing about this approach is also that adding new items is very easy; I just find the place on the tree that they need to be hung on and attach the reference there. Searching is also very quick, in that I can look at each node and decide which way to look next until I either find what I am looking for or I find there is no reference in the required direction, in which case the item is not in the structure.
Reference Importance
The key to this way of working is that an object can contain references to other objects, as well as the data payload. We will consider this aspect of object use later; for now we just need to remember that the reference and the object are distinct and separate.
Rather than saying We need to do these operations on a bank account, object based design turns this on its head, a bit like President Kennedy did all those years ago:
89
Creating Solutions
And so, my fellow Americans: ask not what your country can do for youask what you can do for your country (huge cheers) We dont do things to the bank account. Instead we ask it to do these things for us. The design of our banking application can be thought of in terms of identifying the objects that we are going to use to represent the information and then specifying what things they should be able to do. The really clever bit is that once we have decided what the bank account should do, we then might be able to get somebody else to make it do these things. If our specification is correct and they implement it properly, we dont have to worry precisely how they made it work we just have to sit back and take the credit for a job well done. This brings us back to a couple of recurring themes in this document; metadata and testing. What a bank account object should be able to do is part of the metadata for this object. And once we have decided on the actions that the account must perform, the next thing we need to do is devise a way in which each of the actions can be tested. In this section we are going to implement an object which has some of the behaviours of a proper bank account.
The Account class above holds the member that we need to store about the balance of our bank accounts. Members of a class which hold a value which describes some data which the class is holding are often called properties. Ive used the decimal type for the account balance, since this is specially designed to hold financial values. We have seen that each of the data items in a class is a member of it and stored as part of it. Each time I create an instance of the class I get all the members as well. We have already seen that it is very easy to create an instance of a class and set the value of a member:
Account RobsAccount ; RobsAccount = new Account(); RobsAccount.Balance = 99;
The reason that this works is that the members of the object are all public and this means that anybody has direct access to them. This means that any programmer writing the application can do things like:
RobsAccount.Balance = 0;
90
Creating Solutions
- and take away all my money. If we are going to provide a way of stopping this from happening we need to protect the data inside our objects.
The property is no longer marked as public. Instead it is now private. This means that the outside world no longer has direct access to it. If I write the code:
RobsAccount.balance = 0;
The balance value is now held inside the object and is not visible to the outside world.
91
Creating Solutions
class Bank { public static void Main () { Account RobsAccount; RobsAccount = new Account(); if ( RobsAccount.WithdrawFunds (5) ) { Console.WriteLine ( "Cash Withdrawn" ) ; } else { Console.WriteLine ( "Insufficient Funds" ) ; } } }
This creates an account and then tries to draw five pounds out of it. This will of course fail, since the initial balance on my account is zero, but it shows how I go about providing access to members in an account. The method WithdrawFunds is a member of the Account class and can therefore access private members of the class.
public Methods
You may have noticed that I made the WithdrawFunds method public. This means that code running outside the class can make calls to that method. This has got to be the case, since we want people to interact with our objects by calling methods in them. In general the rules are: if it is a data member (i.e. it holds data) of the class, make it private if it is a method member (i.e. it does something) make it public
Of course, the rules can be broken on special occasions. If you dont care about possible corruption of the member and you want your program to run as quickly as possible you can make a data member public. If you want to write a method which is only used inside a class and performs some special, secret, task you can make it private.
Programmers Point: Use coding conventions to show which things are private
If you look closely at the code I write (and I would advise you to do this is good stuff) you will find that when I it write the name of a public item I use a capital letter to start the name (as in the case of WithdrawFunds to withdraw from our bank account). But I make the first letter of private members lower case (as in the case of the balance data member of our bank account). This makes it easy for someone reading my code, because they can see from the name of a class member whether or not it is public or private. The convention also extends to variables which are local to a block. These (for example the ubiquitous i) always start with a lower case letter. Some people go further and do things like put the characters m_ in front of variables which are members of a class. They would call their balance value m_balance so that class members are easy to spot. I dont usually go that far, because I reckon that the name of the member is usually enough, but opinions differ on this one. The most important thing in this situation is that the whole programming team adopts the same conventions on matters like these. In fact, most development companies have documents that set out the coding conventions they use, and expect developers to adhere to these.
92
Creating Solutions
At the end of this set of statements the test account should have 50 pounds in it. If it does not my program is faulty. The method GetBalance is called an accessor since it allows access to data in my business object. I could write a little bit of code to test these methods:
Account test = new Account(); test.PayInFunds(50); if ( test.GetBalance() != 50 ) { Console.WriteLine ( "Pay In test failed" ); }
My program now tests itself, in that it does something and then makes sure that the effect of that action is correct. Of course I must still read the output from all the tests, which is tedious. Later we will consider the use of unit tests which make this much easier.
93
Creating Solutions
Static Items
2.
3.
94
Creating Solutions
Static Items
class AccountTest { public static void Main () { Account test = new Account(); test.PayInFunds (50); Console.WriteLine ("Balance:" + test.GetBalance()); } }
The AccountTest class has a static member method called Main. We know that this is the method which is called to run the program. It is part of the class AccountTest. If I made fifty AccountTest instances, they would all share the same Main method. In terms of C# the keyword static flags a member as being part of the class, not part of an instance of the class. I will write that down again in a posh font, for it is important:
A static member is a member of the class, not a member of an instance of the class
I don't have to make an instance of the AccountTest class to be able to use the Main method. This is how my program actually gets to work, in that when it starts it has not made any instances of anything, and so this method must be there already, otherwise it cannot run. Static does not mean "cannot be changed". I think this is time for more posh font stuff:
Now I can create accounts and set balances and interest rates on them. (of course if I was doing this properly I'd make this stuff private and provide methods etc, but I'm keeping things simple just now).
Account RobsAccount = new Account(); RobsAccount.Balance = 100; RobsAccount.InterestRateChanged = 10;
The snag is; I've been told that the interest rate is held for all the accounts. If the interest rate changes it must change for all accounts. This means that to implement the change I'd have to go through all the accounts and update the rate. This would be tedious, and if I missed one account, possibly expensive. I solve the problem by making the interest rate member static:
95
Creating Solutions
Static Items
public class Account { public decimal balance ; public static decimal interestRateCharged ; }
The interest rate is now part of the class, not part of any instance. This means that I have to change the way that I get hold of it:
Account RobsAccount = new Account(); RobsAccount.Balance = 100; Account.InterestRateChanged = 10;
Since it is a member of the class I now have to use the class name to get hold of it instead of the name of the instance reference.
This checks the age and income; you must be over 17 and have at least 1000 pounds income to be allowed an account. The snag is that, at the moment, we can't call the method until we have an Account instance. We can solve this by making the method static:
public static bool AccountAllowed ( decimal income, int age ) { if ( ( income >= 10000 ) && ( age >= 18 ) ) { return true; } else { return false; } }
Now the method is part of the class, not an instance of the class. I can now call the method by using the class name:
96
Creating Solutions
Static Items
This is nice because I have not had to make an instance of the account to find out if one is allowed.
This is a better design, in that I now have members of the class which set out the upper limits of the age and income. However it is a bad program, since the class above will not compile:
AccountManagement.cs(19,21): error CS0120: An object reference is required for the nonstatic field, method, or property 'Account.minIncome' AccountManagement.cs(19,43): error CS0120: An object reference is required for the nonstatic field, method, or property 'Account.minAge'
As usual, the compiler is telling us exactly what is wrong; using language which makes our heads spin. What the compiler really means is that "a static method is using a member of the class which is not static". If that doesnt help, how about this: The members minIncome and minAge are held within instances of the Account class. However, a static method can run without an instance (since it is part of the class). The compiler is unhappy because in this situation the method would not have any members to play with. We can fix this (and get our program completely correct) by making the income and age limits static as well:
97
Creating Solutions
public class Account { private static decimal minIncome ; private static int minAge ; public static bool AccountAllowed(decimal income, int age) { if ( ( income > minIncome) && ( age > minAge) ) { return true; } else { return false; } } }
If you think about it, this makes perfect sense. The limit values should not be held as members of a class, since we want them to be the same for all instances of the class, therefore, making them static is what we should have done in the first place.
If you look closely at what is happening you might decide that what is happening looks quite a bit like a method call. This is actually exactly what is happening. When an instance of a class is created the C# system makes a call to a constructor method in that class. The constructor method is a member of the class and it is there to let the programmer get control and set up the contents of the shiny new object. One of the rules of the C# game is that every single class must have a constructor method to be called when a new instance is created. But wait a minute, you say, Weve been making objects for a while and Ive never had to provide a constructor method. This is because the C# compiler is, for a change, being friendly here. Rather than shout at you for not providing a constructor method, the compiler instead quietly creates a default one for you and uses that.
98
Creating Solutions
You might think this is strange, in that normally the compiler loses no time in telling you off when you dont do something, but in this case it is simply solving the problem without telling you. There are two ways to look at this: nice compiler: the compiler is trying to make life easier for you evil compiler: the compiler knows that if it does this automatically now you will suffer more later when you try to understand why you dont need to add one How you regard the action of the compiler is up to you.
This is what the default constructor looks like. It is public so that it can be accessed from external classes who might want to make instances of the class. It accepts no parameters. If I create my own constructor the compiler assumes that I know what Im doing and stops providing the default one. This can cause problems, which we will discuss later.
This constructor is not very constructive (ho ho) but it does let us know when it has been called. This means that when my program executes the line:
robsAccount = new Account();
Note that this is not very sensible, in that it will result in a lot of printing out which the user of the program might not appreciate, but it does show how the process works.
This could create a new account and set the name property to Rob Miles, the address to Hull and the initial balance to zero. It turns out that I can do this very easily, all I have to do is make the constructor method to accept these parameters and use them to set up the members of the class:
99
Creating Solutions
class Account { // private member data private string name; private string address; private decimal balance; // constructor public Account (string inName, string inAddress, decimal inBalance) { name = inName; address = inAddress; balance = inBalance; } }
The constructor takes the values supplied in the parameters and uses them to set up the members of the Account instance that is being created. In this respect it behaves exactly as any other method call. Note that adding a constructor like this has one very powerful ramification: You must use the new constructor to make an instance of a class, i.e. the only way I can now make an Account object is by supplying a name, address and starting balance. If I try to do this:
robsAccount = new Account();
- the compiler will stop being nice to me and produce the error:
AccountTest.cs(9,27): error CS1501: No overload for method 'Account' takes '0' arguments
What the compiler is telling me is that there is no constructor in the class which does not have any parameters. In other words, the compiler only provides a default constructor if the programmer doesn't provide a constructor. This can cause confusion if we have made use of the default constructor in our program and we then add one of our own. The default constructor is no longer supplied by the compiler and our program now fails to compile correctly. In that situation you have to either find all the calls to the default one and update them, or create a default constructor of your own for these calls to use. Of course you don't have to do this because your design of the program was so good that you never have this problem. Just like me, hem hem.
I've missed off the balance value, since I want to use the "default" one of zero. If your code does this the compiler simply looks for a constructor method which has two strings as parameters, and nothing else. Something a bit like this:
100
Creating Solutions
public Account (string inName, string inAddress) { name = inName; address = inAddress; balance = 0; }
A call of:
SetDate ( 23, 7, 2005 );
- would be matched up with the method which accepts three integer parameters and that code would be executed.
I've made three constructors for an Account instance. The first is supplied with all the information, the second is not given a balance and sets the value to 0. The third is not given the address either, and sets the address to "Not Supplied". To do this I have had to duplicate code. Good programmers hate duplicating code. It is regarded as "dangerous extra work". The scary thing is that it is quite easy to do, just use the block copy command in the text editor and you can take the same piece of program and use it all over the place. But you should not do this. Because it is bad. If you need to change this piece of code you have to find every copy of the code and change it. This happens more often than you'd think, even if you don't put a bug in your code, you still might find yourself having to change it because the specification changes. So, C# provides a way in which you can call one constructor from another. Consider:
101
Creating Solutions
public Account (string inName, string inAddress, decimal inBalance) { name = inName; address = inAddress; balance = inBalance; } public Account ( string inName, string inAddress ) : this (inName, inAddress, 0 ) { } public Account ( string inName ) : this (inName, "Not Supplied", 0 ) { }
The keyword this means "another constructor in this class". As you can see in the code sample above, the highlighted bits of the code are calls to the first constructor. They simply pass the parameters which are supplied, along with any default values that we have created, on to the "proper" constructor to deal with. This means that the actual transfer of the values from the constructor into the object itself only happens in one method, and the other constructor methods just make calls to it. The syntax of these calls is rather interesting, in that the call to the constructor takes place before the body of the constructor method. In fact it is outside the block completely. This is sensible, because it reflects exactly what is happening. The "this" constructor runs before the body of the other constructor is entered. In fact, in the code above, since the call of this does all the work, the body of the constructor can be empty.
There will be an upper limit to the amount of cash you can pay in at once, so the PayInFunds method will refuse to pay the money in. But what is to stop the following:
RobsAccount = new Account ("Rob", "Hull", 1234567890);
102
Creating Solutions
Like James Bond, constructors are not allowed to fail. Whatever happens during the constructor call, it will complete and a new instance will be created. This poses a problem. It looks as if we can veto stupid values at every point except the one which is most important, i.e. when the object is first created.
If we try to create an account with a bad name it will throw an exception, which is what we want. The only problem here is that if the address is wrong too, the user of the method will not know this until they have fixed the name and then called the constructor again. I hate it when I'm using a program and this happens. It is normally when I'm filling in a form on the web. I type in my name wrong and it complains about that. Then I put my name right, and it complains about my address. What I want is a way in which I can get a report of all the invalid parts of the item at once. This can be done, at the expense of a little bit of complication:
public Account (string inName, string inAddress) { string errorMessage = ""; if ( SetName ( inName ) == false ) { errorMessage = errorMessage + "Bad name " + inName; } if ( SetAddress ( inAddress) == false ) { errorMessage = errorMessage + " Bad addr " + inAddress; } if ( errorMessage != "" ) { throw new Exception ( "Bad account" + errorMessage) ; } }
This version of the constructor assembles an error message which describes everything which is wrong with the account. Each new thing which is wrong is added to the message and then the whole thing is put into an exception and thrown back to the caller.
103
Creating Solutions
Rather than spend a lot of time at the start of a project worrying just how we are going represent an account and precisely what it should do, we just say "We need an account here" and then move on to other things. Later we will come back and revisit the problem in a greater level of detail, and from the point of view of what the Account class needs to do. The next thing to do is consider how we take a further step back and consider expressing a solution using components and interfaces. In this section you will find out the difference between an object and a component, and how to design systems using them.
104
Creating Solutions
This says that the IAccount interface is comprised of three methods, one to pay money in; another to withdraw it and a third which returns the balance on the account. From the balance management point of view this is all we need. Note that at the interface level I am not saying how it should be done, I am instead just saying what should be done. An interface is placed in a source file just like a class, and compiled in the same way. It sets out a number of methods which relate to a particular task or role, in this case what a class must do to be considered a bank account.
105
Creating Solutions
The code above does not look that different from the previous account class. The only difference is the top line:
public class CustomerAccount : IAccount { ...
The highlighted part of the line above is where the programmer tells the compiler that this class implements the IAccount interface. This means that the class contains concrete versions of all the methods described in the interface. If the class does not contain a method that the interface needs you will get a compilation error:
error CS0535: 'AccountManagement.CustomerAccount' does not implement interface member 'AccountManagement.IAccount.PayInFunds(decimal)'
In this case I missed out the PayInFunds method and the compiler complained accordingly.
People do this all the time. You can think of me in a whole variety of ways, here are two:
106
Creating Solutions
Rob Miles the individual (because that is who I am) A university lecturer (because that is what I can do)
If you think of me as a lecturer you would be using the interface that contains methods like GiveLecture. And you can use the same methods with any other lecturer (i.e. person who implements that interface). From the point of view of the university, which has to manage a large number of interchangeable lecturers, it is much more useful for it to think of me as a lecturer, rather than Rob Miles the individual. So, with interfaces we are moving away from considering classes in terms of what they are, and starting to think about them in terms of what they can do. In the case of our bank, this means that we want to deal with objects in terms of IAccount,(the set of account abilities) rather than CustomerAccount (a particular account class). In C# terms this means that we need to be able to create reference variables which refer to objects in terms of interfaces they implement, rather than the particular type that they are. It turns out that this is quite easy:
IAccount account = new CustomerAccount(); account.PayInFunds(50);
The account variable is allowed to refer to objects which implement the IAccount interface. The compiler will check to make sure that CustomerAccount does this, and if it does, the compilation is successful. Note that there will never be an instance of IAccount interface. It is simply a way that we can refer to something which has that ability (i.e. contains the required methods). This is the same in real life. There is no such physical thing as a "lecturer", merely a large number of people who can be referred to as having that particular ability or role.
107
Creating Solutions
The nice thing about this is that as it is a component we dont have to change all the classes which use it. When we create the account objects we just have ask if a standard account or a baby account is required. The rest of the system can then pick up this object and use it without caring exactly what it is. We will of course have to create some tests especially for it, so that we can make sure that withdrawals of more than ten pounds do fail, but using the new kind of account in our existing system is very easy.
Now anything which implements the IPrintToPaper interface will contain the DoPrint method and can be thought of in terms of its ability to print. A class can implement as many interfaces as it needs. Each interface is a new way in which it can be referred to and accessed.
public class BabyAccount : IAccount, IPrintToPaper { ...
This means that a BabyAccount instance behaves like an account and it also contains a DoPrint method which can be used to make it print out.
You can/should do much of this on paper, before you write any code at all. There are also graphical tools that you can use to draw formal diagrams to represent this information. The field of Software Engineering is entirely based on this process. You should also have spotted that interfaces are good things to hang tests on. If you have a set of fundamental behaviours that all bank accounts must have (for example
108
Creating Solutions
Inheritance
paying in money must always make the account balance go up) then you can write tests that bind to the IAccount interface and can be used to test any of the bank components that are created.
This method returns the integer which is the account number for this instance. By placing it in the interface we can say that the account must deliver this value, but we have not actually described how this should be done. The design of the interfaces in a system is just this. They state that we have a need for behaviours, but they do not necessarily state how they are made to work. I have to add comments to give more detail about what the method does. The need for things like account numbers, which really need to be unique in the world, has resulted in the creation of a set of methods in the C# libraries to create things called Globally Unique Identifiers or GUIDs. These are data items which are created based on the date, time and certain information about the host computer. Each GUID is unique in the world. We could use these in our Account constructor to create a GUID which allows each account to have a unique number.
4.9 Inheritance
Inheritance is another way we can implement creative laziness. It is a way that we can pick up behaviours from classes and just modify the bits we need to make new ones. In this respect you can regard it as a mechanism for what is called code reuse. It can also be used at the design stage of a program if you have a set of related objects that you wish to create. Inheritance lets a class pick up behaviours from the class which is its parent. You can regard an interface as a statement by a class that it has a set of behaviours because it implements a given interface. If a class is descended from a particular parent class this
109
Creating Solutions
Inheritance
means that it has a set of behaviours because it has inherited them from its parent. In short: Interface: "I can do these things because I have told you I can" Inheritance: "I can do these things because my parent can"
The key thing here is the highlighted part after the class name. I have put the name of the class that BabyAccount is extending. This means that everything that CustomerAccount can do, BabyAccount can do. I can now write code like:
BabyAccount b = new BabyAccount(); b.PayInFunds(50);
This works because, although BabyAccount does not have a PayInFunds method, the parent class does. This means that the PayInFunds method from the CustomerAccount class is used at this point. So, instances of the BabyAccount class have abilities which they pick up from their parent class. In fact, at the moment, the BabyAccount class has no behaviours of its own; it gets everything from its parent.
110
Creating Solutions
Inheritance
The keyword override means "use this version of the method in preference to the one in the parent". This means that code like:
BabyAccount b = new BabyAccount(); b.PayInFunds(50); b.WithdrawFunds(5);
The call of PayInFunds will use the method in the parent (since that has not been overridden) but the call of WithdrawFunds will use the method in BabyAccount.
Virtual Methods
Actually, there is one other thing that we need to do in order for the overriding to work. The C# compiler needs to know if a method is going to be overridden. This is because it must call an overridden method in a slightly different way from a "normal" one. In other words, the above code won't compile properly because the compiler has not been told that WithDrawFunds might be overridden in classes which are children of the parent class. To make the overriding work correctly I have to change my declaration of the method in the CustomerAccount class.
public class CustomerAccount : IAccount { private decimal balance = 0; public virtual bool WithdrawFunds ( decimal amount ) { if ( balance < amount ) { return false ; } balance = balance - amount ; return true; } }
The keyword virtual means I might want to make another version of this method in a child class. You dont have to override the method, but if you dont have the word present, you definitely cant.
111
Creating Solutions
Inheritance
This makes override and virtual a kind of matched pair. You use virtual to mark a method as able to be overridden and override to actually provide a replacement for the method.
Im not terribly happy about doing this, the balance is very important to me and Id rather that nobody outside the CustomerAccount class could see it. However, for now making this change will make the program work. Later we will see better ways to manage this situation.
112
Creating Solutions
Inheritance
public class BabyAccount : CustomerAccount, IAccount { public override bool WithdrawFunds (decimal amount) { if (amount > 10) { return false ; } return base.WithdrawFunds(amount); } }
The very last line of the WithDrawFunds method makes a call to the original WithDrawFunds method in the parent class, i.e. the one that the method overrides. It is important that you understand what Im doing here, and why Im doing it: I dont want to have to write the same code twice I dont want to make the balance value visible outside the CustomerAccount class.
The use of the word base to call the overridden method solves both of these problems rather beautifully. Because the method call returns a bool result I can just send whatever it delivers. By making this change I can put the balance back to private in the CustomerAccount because it is not changed outside it. Note that there are other useful spin-offs here. If I need to fix a bug in the behaviour of the WithDrawFunds method I just fix it once, in the top level class, and then it is fixed for all the classes which call back to it.
The problem with this way of working is that you are unable to use base. This makes it more difficult to pick up behaviours from parent classes.
113
Creating Solutions
Inheritance
This is the banking equivalent of the bottle of beer that is never empty. No matter how much cash is drawn out, it always returns a balance value of a million pounds! A naughty programmer could insert this into a class and give himself a nice spending spree. What this means is that we need a way to mark some methods as not being able to be overridden. C# does this by giving us a sealed keyword which means You cant override this method any more. Unfortunately this is rather hard to use. The rules are that you can only seal an overriding method (which means that we cant seal the GetBalance virtual method in the CustomerAccount class) and you can still replace a sealed method. Another use for sealed, which has a bit more potential, is that you can mark a class as sealed. This means that the class cannot be extended, i.e. it cannot be used as the basis for another class.
public sealed class BabyAccount : CustomerAccount,IAccount { ..... }
The compiler will now stop the BabyAccount from being used as the basis of another account.
114
Creating Solutions
Inheritance
The code above will only work if the CustomerAccount class has a constructor which accepts two strings, the name and the address of the new customer. You might think that I could solve this by writing a constructor a bit like this:
public CustomerAccount (string inName, decimal inBalance) { name = inName; balance = inBalance; }
But this class is an extension of the Account class. In other words, to make a CustomerAccount I have to make an Account. And the account is the class which will have a constructor which sets the name and the initial balance. In this situation the constructor in the child class will have to call a particular constructor in the parent to set that up before it is created. The keyword base is used to make a call to the parent constructor. In other words, the proper version of the customer account constructor is as follows:
public CustomerAccount (string inName, decimal inBalance) : base ( inName, inBalance) { }
The base keyword is used in the same way as this is used to call another constructor in the same class. The constructor above assumes that the Account class which CustomerAccount is a child of has a constructor which accepts two parameters, the first a string and the second a decimal value.
Constructor Chaining
When considering constructors and class hierarchies you must therefore remember that to create an instance of a child class an instance of the parent must first be created. This means that a constructor in the parent must run before the constructor in the child. In other words, to create a CustomerAccount you must first create an Account. The result of this is that programmers must take care of the issue of constructor chaining. They must make sure that at each level in the creation process a constructor is called to set up the class at that level.
115
Creating Solutions
Inheritance
will have to be different for each type of account (we dont want to use the same language to a baby account holder as we do for an older one). This means that at the time we create the bank account system we know that we need this method, but we dont know precisely what it does in every situation. We could just provide a standard method in the CustomerAccount class and then rely on the programmers overriding this with a more specific message but we then have no way of making sure that they really do provide the method. C# provides a way of flagging a method as abstract. This means that the method body is not provided in this class, but will be provided in a child class:
public abstract class Account { public abstract string RudeLetterString(); }
The fact that my new Account class contains an abstract method means that the class itself is abstract (and must be marked as such). It is not possible to make an instance of an abstract class. If you think about it this is sensible. An instance of Account would not know what to do it the RudeLetterString method was ever called. An abstract class can be thought of as a kind of template. If you want to make an instance of a class based on an abstract parent you must provide implementations of all the abstract methods given in the parent.
The trick is to take all the methods in the first category and put them inside the parent class. The methods in the second category must be made abstract. This leads us to a class design a bit like this:
116
Creating Solutions
Inheritance
public interface IAccount { void PayInFunds ( decimal amount ); bool WithdrawFunds ( decimal amount ); decimal GetBalance (); string RudeLetterString(); } public abstract class Account : IAccount { private decimal balance = 0; public abstract string RudeLetterString(); public virtual bool WithdrawFunds ( decimal amount ) { if ( balance < amount ) { return false ; } balance = balance - amount ; return true; } public virtual decimal GetBalance () { return balance; } public void PayInFunds ( decimal amount ) { balance = balance + amount ; } } public class CustomerAccount : Account { public override string RudeLetterString() { return "You are overdrawn" ; } } public class BabyAccount : Account { public override bool WithdrawFunds ( decimal amount ) { if (amount > 10) { return false ; } return base.WithdrawFunds(amount); } public override string RudeLetterString() { return "Tell daddy you are overdrawn"; } }
117
Creating Solutions
Inheritance
This code repays careful study. Note how I have moved all the things that all accounts must do into the parent Account class. Then I have added customised methods into the child classes where appropriate. Note also though that I have left the interface in place. That is because; even though I now have this abstract structure I still want to think of the account objects in terms of their accountness rather than any particular specific type.
118
Creating Solutions
Object Etiquette
4.10.1
The integer somehow seems to know how to print itself out. Now it is time to find out how this is achieved, and also how we can give our own objects the same magical ability. It turns out that this is all provided by the "objectness" of things in C#. We know that an object can contain information and do things for us. (in fact we have seen that the whole basis of building programs is to decide what the objects should do and then make them do these things). We have also seen that you can extend a parent object to create a child which has all the abilities of the parent, plus the new ones that we add. Now we are going to see how these abilities of objects are used in making parts of the C# implementation itself work.
The object class is a part of C#, and everything is a child of the object class. This has a couple of important ramifications: Every object can do what an object can do. A reference to an object type can refer to any class.
119
Creating Solutions
Object Etiquette
For the purpose of this part of the notes, the first point is the one which has the most importance here. It means that all classes that are made have a number of behaviours that they inherit from their ultimate parent, the object. If you look inside the actual code that makes an object work you will find a method called ToString. The object implementation of ToString returns a string description of the type of that object.
- and the results would be exactly the same. The nice thing about ToString is that it has been declared virtual. This means that we can override it to make it behave how we would like:
class Account { private string name; private decimal balance; public override string ToString() { return "Name: " + name + " balance: " + balance; } public Account (string inName, decimal inBalance) { name = inName; balance = inBalance; } }
In the tiny Account class above I've overridden the ToString method so that it prints out the name and balance value. This means that the code:
Account a = new Account("Rob", 25); Console.WriteLine(a);
So, from the point of view of good etiquette, whenever you design a class you should provide a ToString method which provides a text version of the content of that class.
120
Creating Solutions
Object Etiquette
The method above is from a ChildAccount class. This class extends the CustomerAccont and holds the name of the child's parent. The code above uses the ToString method in the parent object and then tacks the name of the parent on the end before returning it. The nice thing about this is that if the behaviour of the parent class changes, i.e. new members are added or the format of the string changes, the ChildAccount class does not have to change, since it will just make use of the upgraded method.
4.10.2
This is my point class. I can now create instances of Point and use them to manage my game objects:
Point spaceshipPosition = new Point(); spaceshipPosition.x = 1; spaceshipPosition.y = 2; Point missilePosition = new Point(); missilePosition.x = 1; missilePosition.y = 2; if ( spaceshipPosition == missilePosition ) { Console.WriteLine("Bang"); }
Note that I've made the x and y members of the Point class public. This is because I'm not that concerned about protecting them, but I do want my program to run quickly. The problem is that the above program code does not work. Even though I have put the spaceship and the missile at the same place on the screen the word Bang is not printed. This is because although the two Point objects hold the same data, they are not located at the same address in memory, which means that the equals test will fail.
121
Creating Solutions
Object Etiquette
public override bool Equals(object obj) { Point p = (Point) obj; if ( ( p.x == x ) && ( p.y == y ) ) { return true; } else { return false; } }
The Equals method is given a reference to the thing to be compared. Note that this reference is supplied as a reference to an object. The first thing we need to do is create a reference to a Point. We need to do this because we want to get hold of the x and y values from a Point (we can't get them from an object). The object is cast into a Point and then the x and y values are compared. If they are both the same the method returns true to the caller. This means that I can write code like:
if ( missilePosition.Equals(spaceshipPosition) ) { Console.WriteLine("Bang"); }
This test will work, because the Equals method actually compares the content of the two points rather than just their references. Note that I didn't actually need to override the Equals method; I could have written one called TheSame which did the job. However, the Equals method is sometimes used by C# library methods to see if two objects contain the same data and so overriding Equals makes my class behave correctly as far as they are concerned.
122
Creating Solutions
Object Etiquette
4.10.3
The class above has a single data member and a single method member. The data is a counter. Each time the Count method is called the counter is made one larger. I can use the class as follows:
Counter c = new Counter(); c.Count(); Console.WriteLine("Count : " + c.Data);
This calls the method and then prints out the data. We know that in this context the . (dot) means "follow the reference to the object and then use this member of the class".
We can add a this. if we like to make it explicit that we are using a member of a class rather than a local variable.
When the Store method is called it is given a reference to the currently executing account instance. The Store method accepts this and then stores this somehow. Of course, in this situation we are not passing an account into the bank, instead we are passing a reference to the account. If this hurts your head, then don't worry about it for now.
123
Creating Solutions
this.account.SetName("Rob");
This means "I have a member variable in this class called account. Call the SetName method on that member to set the name to 'Rob'".
4.11.1
String Manipulation
Strings are rather special. I regard them as a bit like bats. A bat can be regarded as either an animal or a bird in many respects. A string can be regarded as either an object (referred to by means of a reference) or a value (referred to as a value). This hybrid behaviour is provided because it makes things easier for us programmers. However it does mean that we have to regard strings as a bit special when you are learning to program, because they seem to break some of the rules that we have learnt. It is very important that you understand what happens when you transform a string. This is because, although strings are objects, they don't actually behave like objects all the time.
string s1="Rob"; string s2=s1; s2 = "different"; Console.WriteLine(s1 + " " + s2);
If you think you know about objects and references, you should be expecting s1 to change when s2 is changed. The second statement made the reference s2 refer to the same object as s1, so changing s2 should change the object that s1 refers to as well. To find out more about this, take a look at section 4.4.1. This does not happen though, because C# regards an instance of a string type in a special way. It calls it immutable. This is because programmers want strings to behave a bit like values in this respect.
4.11.2
Immutable strings
The idea is that if you try to change a string, the C# system instead creates a new string and makes the reference you are "changing" refer to the changed one. In other words, when the system sees the line:
s2 = "different";
- it makes a new string which contains the text "different" and makes s2 refer to that. The thing that s1 is referring to is unchanged. So, after the assignment s1 and s2 no longer refer to the same object. This behaviour, never allowing a thing to be changed by making a new one each time it is required, is how the system implements immutable. You might ask the question: "Why go to all the trouble?" It might seem all this hassle could be saved by just making strings into value types. Well, no. Consider the situation where you are storing a large document in the memory of the computer. By using references we only actually need one string instance with the word the in it. All the occurrences in the text can just refer to that one instance. This saves memory and it also makes searching for words much faster.
124
Creating Solutions
4.11.3
String Comparison
The special nature of strings also means that you can compare strings using equals and get the behaviour you want:
if ( s1 == s2 ) { Console.WriteLine("The same"); }
If s1 and s2 were proper reference types this comparison would only work if they referred to the same object. But in C# the comparison works if they contain the same text. You can use an equals method if you prefer:
if ( s1.Equals(s2) ) { Console.WriteLine("Still the same"); }
4.11.4
String Editing
You can read individual characters from a string by indexing them as you would an array:
char firstCh = name[0];
This would set the character variable firstCh to the first character in the string. However, you can't change the characters:
name[0] = 'R';
- would cause a compilation error because strings are immutable. You can pull a sequence of characters out of a string using the SubString method:
string s1="Rob"; s1=s1.Substring(1,2);
The first parameter is the starting position and the second is the number of characters to be copied. This would leave the string "ob" in s1.(remember that strings are indexed starting at location 0). You can leave out the second parameter if you like, in which case all the characters up to the end of the string are copied:
string s1="Miles"; s1=s1.Substring(2);
4.11.5
String Length
All of the above operations will fail if you try to do something which takes you beyond the length of the string. In this respect strings are just like arrays. It is also possible to use the same methods on them to find out how long they are:
Console.WriteLine ( "Length: " + s1.Length);
4.11.6
Character case
Objects of type string can be asked to create new, modified, versions of themselves in slightly different forms. You do this by calling methods on the reference to get the result that you want:
s1=s1.ToUpper();
125
Creating Solutions
Properties
The ToUpper method returns a version of the string with all the letters converted to UPPER CASE. No other characters in the string are changed. There is a corresponding ToLower method as well.
4.11.7
This is useful if your users might have typed " Rob " rather than "Rob". If you don't trim the text you will find equals tests for the name will fail. There are TrimStart and TrimEnd methods to just take off leading or trailing spaces if that is what you want. If you trim a string which contains only spaces you will end up with a string which contains no characters (i.e. its length is zero).
4.11.8
Character Commands
The char class also exposes some very useful methods which can be used to check the values of individual characters. These are static methods which are called on the character class and can be used to test characters in a variety of ways: char.IsDigit(ch) char.IsLetter(ch) char.IsLetterOrDigit(ch) char.IsLower(ch) char.IsUpper(ch) char.IsPunctuation(ch) char.IsWhiteSpace(ch) returns true if the character is a digit (0 to 9) returns true if the character is a letter (a to z or A to Z) returns true if the character is a letter or a digit returns true if the character is a lower case letter returns true if the character is an upper case letter returns true if the character is a punctuation character returns true if the character is a space, tab or newline
You can use these when you are looking through a string for a particular character.
4.11.9
4.12 Properties
Properties are useful. They are a bit like the switch keyword, which lets us select things easily. We don't need properties, but C# provides them because they make programs slightly easier to write and simpler to read.
126
Creating Solutions
Properties
4.12.1
The class contains a public member. I can get hold of this member in the usual way:
StaffMember s = new StaffMember(); s.Age = 21;
I can access a public member of a class directly; just by giving the name of the member. The problem is that we have already decided that this is a bad way to manage our objects. There is nothing to stop things like:
s.Age = -100210232;
This is very naughty, but because the Age member is public we cannot stop it.
4.12.2
We now have complete control over our property, but we have had to write lots of extra code. Programmers who want to work with the age value now have to call methods:
StaffMember s = new StaffMember(); s.SetAge(21); Console.WriteLine ( "Age is : " + s.GetAge() );
4.12.3
Using Properties
Properties are a way of making the management of data like this slightly easier. An age property for the StaffMember class would be created as follows:
127
Creating Solutions
Properties
public class StaffMember { private int ageValue; public int Age { set { if ( (value > 0) && (value < 120) ) { this.ageValue = value; } } get { return this.ageValue; } } }
The age value has now been created as a property. Note how there are get and set parts to the property. These equate directly to the bodies of the get and set methods that I wrote earlier. The really nice thing about properties is that they are used just as the class member was:
StaffMember s = new StaffMember(); s.Age = 21; Console.WriteLine ( "Age is : " + s.Age );
When the Age property is given a value the set code is run. The keyword value means the thing that is being assigned. When the Age property is being read the get code is run. This gives us all the advantages of the methods, but they are much easier to use and create. I can do other clever things too:
public int AgeInMonths { get { return this.ageValue*12; } }
This is a new property, called AgeInMonths. It can only be read, since it does not provide a set behaviour. However, it returns the age in months, based on the same source value as was used by the other property. This means that you can provide several different ways of recovering the same value. You can also provide read-only properties by leaving out the set behaviour. Write only properties are also possible if you leave out the get.
4.12.4
128
Creating Solutions
Building a Bank
This is an interface that specifies that classes which implement it must contain an Age property with both get and set behaviours.
4.12.5
Property problems
Properties are really neat, and they are used throughout the classes in the .NET libraries. But there are a few things that you need to be aware of when deciding whether or not to use them:
- would fail. However, the person performing the assignment would not be aware of this, since there is no way that the property can return a value which indicates success or failure. The only way that a property set method could do this would be to throw and exception, which is not a good way to go on. A SetAge method on the other hand could return a value which indicates whether or not it worked:
public bool SetAge( int inAge ) { if ( (inAge > 0) && (inAge < 120) ) { this.age = inAge; return true; } return false; }
A programmer setting the age value can now find out if the set worked or failed. If there is a situation where a property assignment can fail I never expose that as a property. I suppose that it would be possible to combine a set method and a get property, but I reckon that would be madness.
This looks very innocent, but could result in a thousand lines of code running inside the set property. It is possible to use this to advantage, in that an object can react to and track property changes. But it can also be made very confusing.
129
Creating Solutions
Building a Bank
A class which implements these methods can be used for the storage of accounts. I can put an account into it and then get it back again:
IBank friendlyBank = new ArrayBank (50); IAccount account = new CustomerAccount("Rob", 0); if (friendlyBank.StoreAccount(account)) { Console.WriteLine ( "Account stored OK" ); }
The code above creates a bank and then puts an account into it, printing out a message if the storage worked.
4.13.1
Note that it is very important that you understand what has happened here. We have not created any accounts at all. What we have created is an array of references. Each reference in the array can refer to an object which implements the IAccount interface. But at the moment none of the references refer anywhere, they are all set to null. When we add an account to the bank we simply make one of the references refer to that account instance. We never place an account "in" the array; instead we put a reference to that account in the array. accounts
Name : "Rob" Balance : 0 The diagram shows the state of the accounts array after we have stored our first account. The light coloured elements are set to null. The element at the start of the array contains a reference to the account instance. The method to add an account to our bank has to find the first empty location in the array and set this to refer to the account that has been added:
130
Creating Solutions
Building a Bank
public bool StoreAccount (IAccount account) { int position = 0; for (position = 0; position<accounts.Length; position++) { if (accounts[position] == null) { accounts[position] = account; return true; } } return false; }
This method works through the array looking for an element containing null. If it finds one it sets this to refer to the account it has been asked to store and then returns true. If it does not find a null element before it reaches the end of the array it returns false to indicate that the store has failed. When we want to work on an account we must first find it. The bank interface provides a method called FindAccount which will find the account which matches a particular customer name:
IAccount fetchedAccount = arrayBank.FindAccount("Rob");
This will either return the account with the required name, or null if the account cannot be found. In the array based implementation of the bank this is achieved by means of a simple search:
public IAccount FindAccount ( string name ) { int position=0 ; for (position=0 ; position<accounts.Length ; position++) { if ( accounts[position] == null ) { continue; } if ( accounts[position].GetName() == name ) { return accounts[position]; } } return null; }
This code works its way through the accounts array looking for an entry with a name which matches the one being looked for. If it finds an element which contains a null reference it skips past that onto the next one. If it reaches the end of the array without finding a match it returns null, otherwise it returns a reference to the account that it found.
4.13.2
4.13.3
131
Creating Solutions
Building a Bank
The idea is that we do something mathematical (called "hashing) to the information in the search property to generate a number which can then be used to identify the location where the data is stored. For example, we could take all the letters in the account name string, look up the ASCII code for each letter and then add these values up. The name "Rob" could be converted to 82+111+98 = 291. We could look in location 291 for our account. Of course this hash code is not foolproof, the name "Rpa" would give the same total (82+112+97) and refer to the same location. This is poetically referred to as a "hash clash". We can do clever things with the way we combine the values to reduce the chances of this happening, but we can't completely avoid clashes. Clashes can be resolved by adding a test when we are storing an account. If we find the location we would like to use is not null we simply work our way down looking for the first free location on from that point. When we want to find a given item we use the hash to take us to the starting position for the search and then look for the one with the matching name. In short, the hash function gives us a starting point for our search, rather than always looking from the beginning of the array in our simple array based code above. This greatly speeds up access to the data.
4.13.4
The Hashtable class can be found in the System.Collections namespace. It provides a method called Add which is given the value of the key and a reference to the item to be stored on that hash code. It also allows you to use a reference to the key (in this case the name) to locate an item:
return bankHashtable[name] as IAccount;
We have to use the "as" part of this coded as the collection will return a reference to an object. We want to return a reference to an instance which implements the IAccount interface. The as operator is a form of casting (where we force the compiler to regard an item as being of a particular type). It is used in preference to the code:
return (IAccount) bankHashtable[name];
The as operator has the advantage that if bankHashtable[name] does not return an account, or returns something of the wrong type, the as operator will generate a null reference. Since this is just what the caller of our FindAccount method is expecting this can be returned back directly. If a cast fails (i.e. at run time the bank hash table returns the wrong thing) our program will fail with an exception.
132
Creating Solutions
Building a Bank
It is interesting to note that because we have implemented our bank behaviour using an interface, we can very easily change the way that the account storage part of the program works without changing anything else.
133
Advanced Programming
5 Advanced Programming
5.1 Generics and Collections
Generics are very useful. This statement probably doesn't tell you much about them or what they do, but it does indicate that they are very useful. They sound a bit scary; telling people you learned about "generics" probably conjures up images of people with white coats and test tubes, at least amongst those who can't spell very well. But I digress. I think the root of Generics is probably "general", in that the idea of them is that you specify a general purpose operation and then apply it in different contexts in a way appropriate to each of them. If this sounds a bit like abstraction and inheritance you are sort of on the right track. If it doesn't, then it might be worth re-reading those bits of the book until they make sense. Perhaps the best way to talk about generics is to see how they can help us solve a problem for the bank. We have just seen that we can store Account references in an Array. But we know that when an array is created the programmer must specify exactly how many elements it contains. This leads to a "straw that breaks the camel's back" problem, where adding the 10,001st customer to the bank is not actually a cause for celebration, because the array size was set at 10,000 and our program crashes. One way to solve this problem is to use a really big array, but we aren't that keen on that because it means that for smaller banks the program might be wasting a lot of memory. Fortunately the C# libraries provide a number of solutions, starting with the simple ArrayList.
Creating an ArrayList
It is very easy to create an ArrayList:
ArrayList store = new ArrayList();
Note that you don't have to set the size of the ArrayList, although there are overloaded constructors that let you give this information and help the library code along a bit:
ArrayList storeFifty = new ArrayList(50);
The ArrayList called storeFifty is initially able to store 50 references, but it can hold more or less as required.
It is important to remember what is going on here. We are not putting an Account into the arraylist; we are instead making one element of the arraylist refer to that account. In this respect the word Add can be a bit misleading, in that what it actually does is add a reference, not the thing itself.
134
Advanced Programming
Remember that it would be perfectly possible to have robsAccount in multiple arraylists, just like your name can appear on multiple lists in the real world. It might be that the bank has many lists of customers. It will have a list of all the customers, along with a list of "special" customers and perhaps another list of those who owe it the most money. So, an item "in" an arraylist is never actually in it, the list actually contains a reference to that item. When an item is removed from an arraylist, it is not necessarily destroyed; it is just no longer on that list.
Unfortunately this won't work. If you write this code you will get a compilation error. The reason for this is that an arraylist holds a list of object references. If you think about it, this is the only thing that it could hold. The designers of the ArrayList class had no idea precisely what type of object a programmer will want to use it with and so they had to use the object reference. You will no doubt remember from the discussion of object hierarchies that an object reference can refer to an instance of any class (since they are all derived from object) and so that is the only kind of reference that the arraylist works with. This is not actually a huge problem, because a program can use a cast to change the type of the item the arraylist returns:
Account a = (Account) store[0]; a.PayInFunds(50);
This would get the element at the start of the arraylist, cast it into an Account class and then pay fifty pounds into it. A slightly larger problem is that an arraylist is not typesafe. This means that I can't be sure that an arraylist you give me has nothing other than accounts in it:
KitchenSink k = new KitchenSink(); store.Add(k);
This puts a reference to a KitchenSink instance into our bank storage. This will cause problems (and an exception) if we ever try to use it as an Account. To get a properly typesafe Account storage we have to take a look at generics in detail a bit later.
This removes the first occurrence of the reference robsAccount from the store arraylist. If the store contained more than one reference to robsAccount then each of them could be removed individually. Note that if the reference given is not actually in the arraylist (i.e. if I ran the code above when store did not contain robsAccount) this does not cause an error. If you remove an item the size of the arraylist is decreased.
135
Advanced Programming
The Contains method is given a reference to an object and returns true if the arraylist contains that reference. You could of course write this behaviour yourself, but having it built into the class makes it much easier.
136
Advanced Programming
This sounds a bit confusing, how about an example. If you wanted to share out marbles amongst you and your friends you could come up with way of doing that. Something like: "Divide the number of marbles by the number of friends and then distribute the remainder randomly". You now have a system that lets you share marbles. However, you could also use it to share out sweets, cakes or even cars. What the system works on is not particularly important. You could take your universal sharing algorithm and use it to share most anything. The sharing algorithm could have been invented without worrying about the type of the thing being shared; it can be a general sharing behaviour that is then applied to the things we want to share. In this respect, generics can be regarded as another form of abstraction.
The above statement creates a list called acountList which can hold references to Accounts. The type between the < and > characters is how we tell the list the kind of things it can store. We could create a List that can hold integers in a similar way:
List<int> scores = new List<int>();
Because we have told the compiler the type of things the list can hold it can perform type validation and make sure that nothing bad happens when the list is used:
KitchenSink k = new KitchenSink(); accountList.Add(k);
The above statements would cause a compilation error, since the acountList variable is declared as holding a list of Account references, and will not accept the kitchen sink. Since the compiler knows that AccountList holds Account references, this means that you can write code like this:
accountList[0].PayInFunds(50);
There is no need to cast the item in the list as the type has already been established. You can do everything with a List (Add, Count, Remove) that you can with an ArrayList, which makes them the perfect way to store a large number of items of a particular type.
This looks just like the way the HashTable was used (and it is). However, using a Dictionary has the advantage that we can only add Account values which are located by means of a string. In other words statements like this would be rejected.
137
Advanced Programming
This would find the element with the hash key "Rob" and then pay fifty pounds into it. The only problem with this use of a dictionary is that if there is no element with the key "Rob" the attempt to find one will result in a KeyNotFoundException being thrown. You can get around this by asking the dictionary if it contains a particular key:
if (d.ContainsKey("Rob")) { Console.WriteLine("Rob is in the bank"); }
The Dictionary class is simply wonderful for storing keys and values in a typesafe manner, and I recommend it strongly to you.
All the accounts in the bank are going to be managed in terms of objects which implement this interface to manage the balance and read the name of the account owner. We can create a class called CustomerAccount which implements the interface and contains the required methods. It also has a constructor which allows the initial name and balance values to be set when the account is created.
138
Advanced Programming
public class CustomerAccount : IAccount { public CustomerAccount( string newName, decimal initialBalance) { name = newName; balance = initialBalance; } private decimal balance = 0; private string name; public virtual bool WithdrawFunds ( decimal amount ) { if ( balance < amount ) { return false; } balance = balance - amount ; return true; } public void PayInFunds ( decimal amount ) { balance = balance + amount ; } public decimal GetBalance () { return balance; } public string GetName() { return name; } }
Note that this version of the class does not perform any error checking of the input values, so it is not what I would call "production" code, but it is just here to illustrate how the data in the class can be saved and restored.
139
Advanced Programming
public bool Save ( string filename ) { try { System.IO.TextWriter textOut = new System.IO.StreamWriter(filename); textOut.WriteLine(name); textOut.WriteLine(balance); textOut.Close(); } catch { return false; } return true; }
This method is given the name of the file that the account is to be stored int. It writes out the name of the customer and the balance of the account. So I could do things like:
if (Rob.Save ("outputFile.txt")) { Console.WriteLine ("Saved OK"); }
This would ask the account referred to by Rob to save itself in a file called "outputFile.txt". Note that I have written the code so that if the file output fails the method will return false, to indicate that the save was not successful.
This method opens a file, fetches the balance value and then creates a new CustomerAccount with the balance value and name. The Load method above takes great pains to ensure that if anything bad happens it does a number of things: It does not throw any exceptions which the caller must catch.
140
Advanced Programming
It returns null to indicate that the load failed if anything bad happens. It makes sure that the file it opens is always closed.
This is what I would call a "professional" level of quality, and is something you should aim for when writing code you are going to sell. We can use this as follows:
test = CustomerAccount.Load( "test.txt" );
This kind of method is sometimes called a factory method, in that it creates an instance of a class for us. If the factory fails (because the file cannot be found or does not contain valid content) it will return a null result, which we can test for:
if (test == null) { Console.WriteLine( "Load failed" ); }
Using streams
A better solution is to give the file save method a stream to save itself, rather than a filename. A stream is the thing that the C# library creates when we open a connection to a file:
System.IO.TextWriter textOut = new System.IO.StreamWriter( "Test.txt" );
The reference textOut refers to a stream which is connected to the file Test.txt. We can create a save method which accepts the stream reference as a parameter instead of a filename:
public void Save(System.IO.TextWriter textOut) { textOut.WriteLine(name); textOut.WriteLine(balance); }
This save method can be called from our original file save method:
141
Advanced Programming
public bool Save ( string filename ) { System.IO.TextWriter textOut = null; try { textOut = new System.IO.StreamWriter(filename); Save(textOut); } catch { return false; } finally { if (textOut != null) { textOut.Close(); } } return true; }
This method creates a stream and then passes it to the save method to save the item. Note that this is an example of overloading in that we have two methods which share the same name. The load method for our bank account can be replaced by one which works in a similar way.
public static CustomerAccount Load( System.IO.TextReader textIn) { CustomerAccount result = null; try { string name = textIn.ReadLine(); string balanceText = textIn.ReadLine(); decimal balance = decimal.Parse(balanceText); result = new CustomerAccount(name, balance); } catch { return null; } return result; }
This method is supplied with a text stream. It reads the name and the balance from this stream and creates a new CustomerAccount based on this data.
142
Advanced Programming
public void Save(System.IO.TextWriter textOut) { textOut.WriteLine(bankHashtable.Count); foreach (CustomerAccount account in bankHashtable.Values) { account.Save(textOut); } }
This is the Save method which would be added to our Hashtable based bank. It gets each account out of the hash table and saves it in the given stream. Note that we are using a new C# loop construction here, foreach. This is very useful when dealing with collections of data. It works its way through a collection, in this case the Values property of the bankHashtable, and supplies each item in turn. Note that before it writes anything it writes out the number of customers in the bank. This can be obtained via the Count property of the Hashtable. We do this is so that when the bank is read back in the load method knows how many accounts are required. We could just write out the data and then let the load method stop when it reaches the end of the file, but this would not allow us to detect if the file had been shortened. The Load method for the entire bank is as follows:
public static HashBank Load(System.IO.TextReader textIn) { HashBank result = new HashBank(); string countString = textIn.ReadLine(); int count = int.Parse(countString); for (int i = 0; i < count; i++) { CustomerAccount account = CustomerAccount.Load(textIn); result.bankHashtable.Add(account.GetName(), account); } return result; }
This reads the size of the bank and then reads each of the accounts in turn and adds it to the hash table. Note that this load method does not handle errors. A production version of the program would check that each account was loaded correctly before adding it to the hash table.
Health Warning
This is complicated stuff. I don't expect you to understand this at first reading. The reason that it is here is for completeness. Having told you all about class hierarchies and how useful it is to be able to base a new class on an existing one (as we did with BabyAccount) it would be most unfair of me to leave you guessing as to how these things can be stored and retrieved. This material is provided to give you an idea of
143
Advanced Programming
how you really could make a working bank, but it assumes a very good understanding of class hierarchies, method overriding/overloading and constructor chaining. The good news is that when you do understand this stuff you really can call yourself a fully-fledged C# programmer.
This is a complete BabyAccount implementation. It contains an additional property, parentName, and overrides the WithdrawFunds method to provide the new behaviour. Note that I have created a constructor which is supplied with the name of the account holder, the starting balance and the name of the parent. This makes use of the constructor in the parent to set the balance and name, and then sets the parent name. I can create a BabyAccount as follows:
BabyAccount babyJane = new BabyAccount ("Jane", 20, "John");
This would create a new BabyAccount instance and set the reference babyJane to refer to it.
144
Advanced Programming
This method overrides the Save method in the parent CustomerAccount. However, it first calls the overridden method in the parent to save the CustomerAccount data. Then it performs the save behaviour required by the BabyAccount class. This is very good design, as it means that if the data content and save behaviour of the parent class changes we don't need to change the behaviour of the child.
However, I'm not particularly keen on this approach. This method breaks one of the rules of good design, in that we now have a dependency between the two classes which means that when I change the CustomerAccount class (perhaps to add a new field called customerPIN) I also have to update the Load method in the BabyAccount class to make sure that this extra data is loaded. If I forget to do this the program will compile, but when it runs it will not work correctly. What we really want to do is make the CustomerAccount responsible for loading its data and the BabyAccount just look after its content; in a similar way to the way the save method uses the base keyword to save the parent object. The way to do this is to go back to the construction process. We know that a constructor is a method which gets control when an instance of a class is being created. A constructor can be used to set up the data in the instance, and it can be passed information to allow it to do this. In the code above there is a constructor for the BabyAccount class which accepts the three data items that the BabyAccount holds and then sets the instance up with these values. What we do is create constructors for the CustomerAccount and BabyAccount classes which read their information from a stream that is supplied to them:
public CustomerAccount(System.IO.TextReader textIn) { name = textIn.ReadLine(); string balanceText = textIn.ReadLine(); balance = decimal.Parse(balanceText); }
This constructor sets up the new CustomerAccount instance by reading the values from the stream that is supplied with. I can therefore write code like this:
145
Advanced Programming
This creates a new CustomerAccount from the stream. Now I can create a constructor for the BabyAccount which uses the constructor of the parent class:
public BabyAccount(System.IO.TextReader textIn) : base (textIn) { parentName = textIn.ReadLine(); }
This removes the dependency relationship completely. If the behaviour of the CustomerAccount constructor changes we do not have to change the BabyAccount at all. Note that these constructors do not do any error checking, they just throw exceptions if something goes wrong. Bearing in mind that the only way that a constructor can fail is to throw an exception anyway, this is reasonable behaviour.
We can now ask any item which implements the IAccount interface to save itself to either a file or a stream. This is very useful when we store our collection of accounts, since the account container will not have to behave differently depending on what kind of account it is saving, it just has to call the save method for the particular instance. This is a good idea.
146
Advanced Programming
This looks very like our original method, except that it has an additional line, which has been highlighted. This writes out the name of the class. It makes use of the method GetType(), which can be called on an instance of a class to get the type of that class. Having got the type we can then get the Name property of this type and print that. In other words, if the account is of type CustomerAccount the program will output:
CustomerAccount Rob 100
The output now contains the name of the type of each class that has been written. This means that when the stream is read back in this information can be used to cause the correct type of class to be created. The neatest way to do this is to create a factory which will produce instances of the required class:
class AccountFactory { public static IAccount MakeAccount( string name, System.IO.TextReader textIn) { switch (name) { case "CustomerAccount": return new CustomerAccount(textIn); case "BabyAccount": return new BabyAccount(textIn); default: return null; } } }
This class only contains a single method, which is static. The method is given two parameters, the name of the class to be created and a stream to read from. It uses the name to decide which item to make, creates one, and then returns that to the caller. If the name is not recognized it returns null. The bank load method can use this factory to create instances of accounts as they are loaded:
public static HashBank Load(System.IO.TextReader textIn) { HashBank result = new HashBank(); string countString = textIn.ReadLine(); int count = int.Parse(countString); for (int i = 0; i < count; i++) { string className = textIn.ReadLine(); IAccount account = AccountFactory.MakeAccount(className, textIn); result.bankHashtable.Add(account.GetName(), account); } return result; }
Again, this looks very like our original load method, except that it uses the factory to make account instances once it has read the name of the class from the stream.
Factory Dependencies
Note that we now have a genuine dependency between our system and the factory class. If we ever add a new type of account we need to update the behaviour of the factory so that it contains a case to deal with the new account type. There is in fact a way of removing the need to do this. It involves writing code which will search for C# classes which implement particular interfaces and creating them automatically. However this kind of stuff is beyond the scope of this text.
147
Advanced Programming
148
Advanced Programming
private string name; public string GetName() { return this.name; } public static string ValidateName ( string name ) { if ( name == null ) { return "Name parameter null"; } string trimmedName = name.Trim(); if ( trimmedName.Length == 0 ) { return "No text in the name"; } return ""; } public bool SetName ( string inName ) { string reply ; reply = ValidateName(inName); if ( reply.Length > 0 ) { return false; } this.name = inName.Trim(); return true; }
The name is stored as a private member of the account class. The programmer has provided three methods which let the users of my Account class deal with names. There is a method to get the name, another to set it and another to validate it. The validate method is called by the set method so that my business object makes sure that an account never has an invalid name. The validate method will reject a name string which is empty, or just contains spaces. It trims off all the spaces before and after the name text and then checks to see if the length of the resulting string is empty. If it is, the string is rejected. I provide the validate method so that users of my class can check their names to make sure that they are valid. This does not involve me in much extra work, since I also use the method myself when I validate the name. The validate method returns a string which gives an error message if the string is rejected. There may be several reasons why a name is not valid, at the moment I've just given two. The reasons why a name is invalid are of course part of the metadata for the project. Ive also made this method static so that names can be validated without needing to have an actual instance of the account.
149
Advanced Programming
int errorCount=0; string reply; reply = CustomerAccount.ValidateName(null); if (reply != "Name parameter null") { Console.WriteLine("Null name test failed"); errorCount++; } reply = CustomerAccount.ValidateName(""); if (reply != "No text in the name") { Console.WriteLine("Empty name test failed"); errorCount++; } reply = CustomerAccount.ValidateName(" "); if (reply != "No text in the name") { Console.WriteLine("Blank string name test failed"); errorCount++; } CustomerAccount a = new CustomerAccount("Rob", 50); if (!a.SetName("Jim")) { Console.WriteLine("Jim SetName failed"); errorCount++; } if ( a.GetName() != "Jim" ) { Console.WriteLine("Jim GetName failed"); errorCount++; } if (!a.SetName(" Pete ")) { Console.WriteLine("Pete trim SetName failed"); errorCount++; } if ( a.GetName() != "Pete" ) { Console.WriteLine("Pete GetName failed"); errorCount++; } if (errorCount > 0 ) { SoundSiren(); }
These are all the tests I could think of. First I test ValidateName to make sure that it rejects both kinds of empty string. Then I make sure that I can set the name on an account. Finally I check that the space trimming for the names works correctly.
150
Advanced Programming
while (true) { Console.Write ( "Enter new name : " ) ; newName = Console.ReadLine(); string reply; reply = account.ValidateName(newName); if ( reply.Length == 0 ) { break; } Console.WriteLine( "Invalid name : " + reply ); } account.SetName(newName);
This code will perform the name editing for an account instance referred to by account. It will read a new name in. If the name is valid the loop is broken and the name is set. If the name is not valid the message is printed and the loop repeats. This is not the most elegant solution, but it does keep the user informed of what it is doing and it does work. Now that we have our edit code we need to put it somewhere.
151
Advanced Programming
This is my account editor class. At the moment it can only edit the name, but I will add other edit methods later. The class keeps track of the account it is editing, I pass it a reference to this account when I construct it. I would use the name editor as follows:
CustomerAccount a = new CustomerAccount("Rob", 50); AccountEditTextUI edit = new AccountEditTextUI(a); edit.EditName();
This code creates an instance of a customer account. It then creates an editor object and asks it to edit the name of that account. Note that the editor class is passed a reference to the IAccount interface, not a references to the account class. This means that anything which behaves like an account can be edited using this class. Note also that the editor remembers the account class that is being edited so that when I call EditName it can work on that reference.
The user types in the name of the command that is required and then the program selects that function. My edit class contains a method which does this: This is my edit method which repeatedly reads commands and dispatches them to the appropriate method. I've extended the account class to manage the account balance and added edit methods which pay in funds and withdraw them. Note that I trim the command string and convert it to lower case before using it to drive the switch construction which selects the command. The edit method is passed a reference to the account which is being edited. It then passes that reference to the service methods which will do the actual work. public void DoEdit (CustomerAccount account) { string command; do { Console.WriteLine ( "Editing account for {0}", account.GetName() ); Console.WriteLine ( " Enter name to edit name" ); Console.WriteLine ( " Enter pay to pay in funds" ); Console.WriteLine ( " Enter draw to draw out funds" ); Console.WriteLine ( " Enter exit to exit program" ); Console.Write ("Enter command : "); command = Console.ReadLine(); command = command.Trim(); command = command.ToLower(); switch ( command ) {
152
Advanced Programming
case "name" : EditName(account); break; case "pay" : PayInFunds(account); break; case "draw" : WithDrawFunds(account); break; } } while ( command != "exit" ); }
153
Advanced Programming
using System.Windows.Forms; class FormsDemo { public static void Main () { Form f = new Form(); f.ShowDialog(); } }
The Form class provides a method called ShowDialog. This asks the form to show itself and pause the program until the form is closed. If I run the program above the following appears on the screen:
When the window is closed with the red button, it disappears and the program ends.
This creates a label, sets the text property of the label to Hello and then adds it to the controls which are contained by the frame. The result of this code is a form which looks like this:
I can fiddle with the properties of a component to move it around on the screen and do lots of interesting things:
154
Advanced Programming
using System.Windows.Forms; using System.Drawing; class FormsDemo { public static void Main () { Form f = new Form(); Label title = new Label (); title.Text="Hello"; title.Top=50; title.Left=50; title.ForeColor = Color.Red; title.BackColor = Color.Yellow; f.Controls.Add(title); f.ShowDialog(); } }
The position on the screen is given in pixels (a pixel being an individual dot on the screen). The origin (i.e. the pixel at 0,0) is in the top left hand corner of the screen. The Color class is found in the System.Drawing namespace. It provides a whole set of static colour values and also lets you create your own colours by setting the intensity of the red, green and blue components. The program above would display a form as below:
By adding a number of labels on the form and positioning them appropriately we can start to build up a user interface.
155
Advanced Programming
using System.Windows.Forms; using System.Drawing; class FormsDemo { public static void Main() { Form f = new Form(); Label nameLabel = new Label(); nameLabel.Text="Name"; nameLabel.Top=50; nameLabel.Left=0; f.Controls.Add(nameLabel); TextBox nameTextBox = new TextBox(); nameTextBox.Top=50; nameTextBox.Left=100; nameTextBox.Text = "Rob"; f.Controls.Add(nameTextBox); f.ShowDialog(); } }
I've fiddled slightly with the position of the label and given it a more sensible name. I've also set the name text we are editing to be Rob, just to show how the components work. If you run this program you get a form as follows:
This is not particularly flashy, but it is not bad for 25 or so lines of code. It also lets you change the text in the name for whatever you want. But at the moment we have no way of signalling when the edit has been finished. For that we need a button which we can press to cause an event.
156
Advanced Programming
using System.Windows.Forms; using System.Drawing; class FormsDemo { public static void Main() { Form f = new Form(); Label nameLabel = new Label(); nameLabel.Text="Name"; nameLabel.Top=50; nameLabel.Left=0; f.Controls.Add(nameLabel); TextBox nameTextBox = new TextBox(); nameTextBox.Top=50; nameTextBox.Left=100; nameTextBox.Text = "Rob"; f.Controls.Add(nameTextBox); Button finishButton = new Button(); finishButton.Top=80; finishButton.Left=100; finishButton.Text="Finished"; f.Controls.Add(finishButton); f.ShowDialog(); } }
However at the moment nothing happens when the button goes down. To get this to work we have to bind a method in our program to the event which is generated when the button is pressed. The windows forms system uses delegates to manage this.
157
Advanced Programming
Button Events
To get back to the problem we are solving; when the user clicks on the button on our form this is registered by the Windows operating system and then passed into the form as an event. The form decides which of the components on the form should be given the event and calls a method in that component to tell it "you've been pressed". The button then does its animation so that it moves in and out. Then it looks for people to tell about the event. The button keeps a list of people to tell about events. The list is actually a list of delegates. So, to get a button to run a method of ours we need to create a delegate object and then pass that to the button so that it knows to use it when the event occurs. A delegate is an object which can be used to represent a particular method in an instance of a class. In the case of our button, the delegate is going to represent the method that we want to have called when the button is pressed. We pass the button a reference to this delegate and it can then call the method it represents when the button is pressed. The good news is that creating a delegate instance for a windows component is such a common task that the forms library has provided a delegate class which does this for us. We just need to create an instance of this class to get a delegate to give to the button. The signature of the method that is called when the event occurs is the same for every kind of event, from mouse movement, to button press, to window closing, so we can use this class every time we need to create a delegate. The actual code to create a delegate and assign it to a method is as follows:
using System.Windows.Forms; using System.Drawing; using System; class FormsDemo { public static void Main() { Form f = new Form(); Label nameLabel = new Label(); nameLabel.Text="Name"; nameLabel.Top=50; nameLabel.Left=0; f.Controls.Add(nameLabel); TextBox nameTextBox = new TextBox(); nameTextBox.Top=50; nameTextBox.Left=100; nameTextBox.Text = "Rob"; f.Controls.Add(nameTextBox); Button finishButton = new Button(); finishButton.Top=80; finishButton.Left=100; finishButton.Text="Finished"; finishButton.Click += new EventHandler(finishButton_Click); f.Controls.Add(finishButton); f.ShowDialog();
158
Advanced Programming
} private static void finishButton_Click( object sender, EventArgs e) { Console.WriteLine ("Finish Pressed"); } }
The highlighted code is the bit that ties a delegate to the click event. The EventHandler delegate class is in the System namespace and can refer to methods which are void and accept two parameters, a reference to the object which caused the event and a reference to an EventArgs instance which contains details about the event which occurred. In the code above I have an event handler method called finishButton_Click which accepts these parameters (but doesn't do anything with them). Instead it prints out a message each time the button is pressed. If you compile the above program and run it you will find that each time you press the Finish button the program prints out a message. In the completed editor this method will finish the edit off for us. Note that we dont actually have to use the parameters, often our methods will ignore the values supplied. However, sometimes, for example when we are capturing mouse movement events, we might want to use them to provide information such as the mouse coordinates.
159
Advanced Programming
using System; using System.Windows.Forms; public class AccountEditForm : System.Windows.Forms.Form { private Label nameLabel; private TextBox nameTextBox; private Button finishButton; IAccount account; public AccountEditForm (IAccount inAccount) { this.account = inAccount; this.nameLabel = new Label(); this.nameLabel.Text="Name"; this.nameLabel.Top=50; this.nameLabel.Left=0; this.Controls.Add(this.nameLabel); this.nameTextBox = new TextBox(); this.nameTextBox.Top=50; this.nameTextBox.Left=100; this.nameTextBox.Text = this.account.GetName(); this.Controls.Add(this.nameTextBox); this.finishButton = new Button(); this.finishButton.Top=80; this.finishButton.Left=100; this.finishButton.Text="Finished"; this.finishButton.Click += new System.EventHandler(finishButton_Click); this.Controls.Add(this.finishButton); } private void finishButton_Click(object sender, System.EventArgs e) { string reply = account.ValidateName(nameTextBox.Text); if ( reply.Length > 0 ) { System.Windows.Forms.MessageBox.Show(reply) ; return; } this.account.SetName( nameTextBox.Text) ; this.Dispose(); } }
The form will be passed a reference to the account instance which is being edited. It then gets the properties out of the form and displays them for editing. When the user presses the finish button the form must make sure that the name is valid. If it is the name of the account class should be set and the form must then dispose of itself. This code is the windows equivalent of the AccountEditTextUI method that we created previously. All the form components are created in the constructor for the form. The finishButton_Click method gets the name out of the text box and validates it. If the name is invalid it uses a static method in the windows forms system to pop up a message box and report an error:
160
Advanced Programming
This is how we tell the user that the name is not correct. There are a number of different forms of this Show method which you can use to get different forms of message box and even add extra buttons to it.
Disposing of forms
The Dispose method is used when we have finished with this form and we want it to get rid of it. All the components on the form are destroyed and the form is removed from the screen. Once we have disposed of a form we can never use it again. We will need to create a new form instead. The Dispose method is important because we must explicitly say when we have finished with the instance. If we just removed a reference to the form object it will still remain in memory until the garbage collector gets around to noticing it.
This piece of test code creates an account and sets the name of the account holder to Rob. It then creates an edit form and uses that to edit the name value in the account.
Modal Editing
The call of ShowDialog in the edit form is modal. This means that while the form is on the screen the rest of the system is paused. In the code above the WriteLine is not performed until after the edit form has been closed.
161
Advanced Programming
Using Delegates
Using a Delegate
As an example, consider the calculation of fees in our bank. The bank will have a number of different methods which do this, depending on the type of customer and the status of that customer. It might want to have a way in which a program can choose which fee calculation method to use as it runs. We have seen that things like overriding let us create methods which are specific to a particular type of object, for example we provided a custom WithdrawFunds for the BabyAccount class which only lets us draw out a limited amount of cash. These techniques are very useful, but they are hard wired into the code that I write. Once I've compiled the class the methods in it cannot change. A delegate is a "stand in" for a method. If you call the delegate it calls the method it presently refers to. This means I can use it as a kind of method selector. A delegate type is created like this:
public delegate decimal CalculateFee (decimal balance);
Note that I've not created any delegates yet, I've just told the compiler what the delegate type CalculateFee looks like. This delegate can stand in for a method which accepts a single decimal parameter (the balance on the account) and returns a decimal value (the amount we are going to charge the customer). An example of a method we might want to use with this delegate you could consider this one:
162
Advanced Programming
public decimal RipoffFee (decimal balance) { if ( balance < 0 ) { return 100; } else { return 1; } }
This is a rather evil fee calculator. If you are overdrawn the fee is 100 pounds. If you are in credit the fee is 1 pound. If I want to use this in my program I can make an instance of CalculateFee which refers to it:
CalculateFee calc = new CalculateFee (RipoffFee);
Now I can "call" the delegate and it will actually run the ripoff calculator method:
fees = calc(100);
The calc delegate presently refers to a delegate instance which will use the RipoffFee method. I can change that by making another delegate:
calc = new CalculateFee (FriendlyFee);
Now when I "call" calc it will run the FriendlyFee method. This of course only works if FriendlyFee is a method which returns a value of type decimal and accepts a single decimal value as a parameter. An instance of a delegate is an object like any other. This means that it can be managed in terms of references, so I can build structures which contain delegates and I can also pass delegates in and out of methods. You can regard a list of delegates as a list of methods that I can call. This gives us another layer of abstraction and means that we can now design programs which can have behaviours which change as the program runs. For now however, it is best just to consider them in terms of how we use delegates to manage the response of a program to events.
163
Advanced Programming
single track, a computer can run more than one thread in a single block of program code. A computer can support multiple threads in two ways. The first is by rapidly switching between active threads, giving each thread the chance to run for a limited time before moving on to run another. When a thread is not running it is held in a frozen state, waiting for its turn to run. As far as the thread is concerned it is running continuously, but in reality a thread may only be active for a small fraction of a second every now and then. The second way to support multiple threads is to actually have more than one processor. The first computers had only one processor and so were forced to use time slicing to support multiple threads. Newer machines now have dual core or even quad core processors which can actually run multiple threads simultaneously.
164
Advanced Programming
This method does nothing, but it does do it many millions of times. If I run a program that calls this method the first thing that happens is that the program seems to stop. Then the fan in my laptop comes on as the processor starts to heat up as it is now working hard. If I start up Windows Task Manager I see the picture below:
My laptop has four processor cores, which means that it can run four threads at a time. You can see little graphs under the CPU (Central Processor Unit) Usage History part of the display showing how busy each processor is. From the look of the above graph, the leftmost processor and the rightmost processor are quite busy but the middle two processors are not doing anything. If I stop my program running I get a display like the one below.
Now all the processors are just ticking over. When we start using more than one thread we should see more of the graphs climbing up, as my programs run on multiple processors.
The Thread class provides a link between your program and the operating system. One job of the operating system is to manage threads on your computer and decide
165
Advanced Programming
exactly when each should get to run. By calling methods provided by the Thread class you can start and stop them.
Creating a Thread
Once you have your start point defined you can now create a Thread value:
Thread t1 = new Thread(busyLoopMethod);
The variable t1 now refers to a thread instance. Note that at the moment the thread is not running, it is waiting to be started. When we start running t1 it will follow the delegate busyLoopMethod and make a call to the busyloop method.
Starting a Thread
The Thread class provides a number of methods that your program can use to control what it does. To start the thread running you can use the Start method:
t1.Start();
The above code creates a thread called t1 and starts it running the busyLoop method. It also calls busyLoop directly from the Main method. This means that there are two processes active. This changes the Task Manager display:
166
Advanced Programming
Now more of the usage graphs are showing activity and the CPU usage is up from 25% to 50% as our program is now using more processors.
This loop will create 100 threads, all running the busyLoop method. Now our computer is really busy:
All the processors are now maxed out, and the CPU usage is up to 100%. If you run this code you might actually find that the machine becomes slightly less responsive, and that all the cooling fans come on full blast.
167
Advanced Programming
The count variable is declared within the body of the method. This means that each method has its own copy of this local variable. If you are still thinking about threads as trains, it is as if the data for each thread is held in trucks that are pulled along behind the train. We could make a tiny change to this code and make the count variable a member of the class:
static long count; static private void busyLoop() { for (count = 0; count < 1000000000000L; count = count + 1) { } }
The method looks very similar, but now every thread running busyLoop is now sharing the same count variable. This is potentially disastrous. It is now very hard to predict how long this multi-threaded program will take to finish. Consider the following sequence of actions: 1. 2. 3. 4. Thread 1 fetches the value of the count variable so that it can add 1 to it. Before the addition can be performed, Thread 1 is stopped and Thread 20 is allowed to run. Thread 20 fetches the value of count, adds one to it and stores it back in memory. Sometime later Thread 1 gets control again, adds 1 to the value it fetched before it was stopped and stores the result back in memory.
Because of the way that Thread 1 was interrupted during its calculation, it has overwritten the changes that Thread 20 made. As the threads run they are all loading, incrementing and storing the value of count and overwriting changes that they have made. What we need is a way of stopping the threads from interrupting each other. The statement count = count + 1; must be allowed to complete without being interrupted. You can do this by using a feature called mutual exclusion or mutex. We can return to our train analogy here.
168
Advanced Programming
This object doesnt actually hold any data. It is just used as the token which is held by the active process.
Monitor.Enter(sync); count = count + 1; Monitor.Exit(sync);
The code between the Monitor.Enter and Monitor.Exit calls can only be performed by one thread at a time. In other words, there is no chance of Windows interrupting the increment statement and switching to another process. All the increment operations will complete in their entirety. Once execution leaves the statements between the Monitor calls the thread can be suspended as usual. Threads that arent able to run are parked in a queue of waiting threads. This queue is held in order, so the first thread to get to the entry point will be the first to get the token, with the other threads lining up for their turn. The Monitor class looks after all this for us; we dont need to know how it works.
Pausing Threads
As you can see above, one way to pause a thread is to create a loop with an enormous limit value. This will certainly pause your program, but at the expense of any other threads that want to run. If you just want your program to pause, perhaps to allow the user to read the output or to wait a little while for something to happen you should use the Sleep method which is provided by the Thread class:
Thread.Sleep(500);
This method is given the number of milliseconds (thousandths of a second) that the execution is to pause. The above call would pause a program for half a second.
Joining Threads
You might want to make one thread wait for another to finish working. A thread finishes when it exits the method that was called when it started. In our examples above, each thread will end when the call of busyloop finishes. A thread instance provides a Join method that can be called to wait for another thread to complete operation.
t1.Join();
This would cause the executing thread to wait until the thread t1 finishes.
Thread Control
The Thread class provides a set of methods that you can use to control the execution of a thread. You can abort a thread by calling its Abort method:
t1.Abort();
This asks the operating system to destroy that thread and remove it from memory.
169
Advanced Programming
If you dont want to destroy a thread, but simply make it pause for a while, you can call the Suspend method on the thread:
t1.Suspend();
The thread is put to sleep until you call the Resume method on that thread.
t1.Resume();
the thread is running the thread has finished executing the thread method the thread has been suspended the thread is executing a Sleep, waiting to join with another thread or waiting for a Monitor
170
Advanced Programming
(and producers never wait for consumers) then you can be sure that you never get the situation where one thread waits for a thread that is waiting for it. Finally you should design in the logging and debugging behaviour (you can always use conditional compilation to turn the logging off later) so that if you do have a problem it is easy to get some diagnostic information which will tell you what your program was doing at the time it went wrong.
To start a program on your system you can use the Start method on the Process class:
Process.Start("Notepad.exe");
The Start method is supplied with the name of the program that you want to start. The above line of C# would start the Notepad program. You can also create Process instances that you can control from within your program in a similar manner to threads.
171
Advanced Programming
This works because the System.Exception class has a default constructor which takes no parameters. The default constructor in BankException (which is added automatically by the compiler), can just use this to make an Exception instance. However, there is a version of the Exception constructor which lets us add a message to the exception. This can be picked up and used to discover what has gone wrong. To create a standard exception with a message I pass the constructor a string. If I want to use this with my bank exception I have to sort out the constructor chaining for my exception class and write code as follows:
public class BankException : System.Exception { public BankException (string message) : base (message) { } }
This makes use of the base keyword to call the constructor of the parent class and pass it the string message.
The throw keyword is followed by a reference to the exception to be thrown. In the code above I make a new instance of the bank exception and then throw that. At this point the execution would transfer to the nearest catch construction. This might be a catch of mine, or it might be one supplied by the system. If the exception is caught by the system it means that my program will be terminated. However, I can catch the exception myself as follows:
172
Advanced Programming
Account a; try { a = new Account(newName, newAddress); } catch (BankException exception) { Console.WriteLine( "Error : " + exception.Message); }
The code tries to create a new account. If doing this causes an exception to be thrown, the code in the exception handler is obeyed. The reference exception is set to refer to the exception which has been thrown. The Message member of an exception is the text which was given. This means that if I try to create an account with an empty name the exception will be thrown, the catch invoked, and the message printed.
Now I can use different catches, depending on how the code fails:
173
Advanced Programming
Program Organisation
Account a; try { a = new Account("Rob", ""); } catch (BankExceptionBadName nameException) { Console.WriteLine("Invalid name : " + nameException.Message); } catch (BankExceptionBadAddress addrException) { Console.WriteLine("Invalid address : " + addrException.Message); } catch (System.Exception exception ) { Console.WriteLine("System exception : " + exception.Message); }
Each of the catches matches a different type of exception. At the very end of the list of handlers I have one which catches the system exception. This will be called if the exception is not a name or an address one.
The two problems are distinct and separate and C# provides mechanisms for both. In this section we are going to see how a large program can be broken down into a number of different chunks.
174
Advanced Programming
Program Organisation
In our bank management program we have identified a need for a class to keep track of a particular account. The class will be called Account. Consider a really simple Account class:
public class Account { private decimal balance = 0; public void PayInFunds ( decimal amount ) { balance = balance + amount; } public decimal GetBalance () { return balance; } public bool WithDrawFunds ( decimal amount ) { if ( amount < 0 ) { return false ; } if ( balance >= amount ) { balance = balance - amount ; return true; } else { return false ; } } }
This is a fairly well behaved class in that it won't let us withdraw more money than we have in the account. We could put it into a file called "Account.cs" if we wanted to. However, we might want to create lots of other classes which will deal with bank accounts. So instead I have decided to put the class into a file called "AccountManagement.cs".As I add more account management The problem is that the compiler now gets upset when I try to compile the file:
error CS5001: AccountManagement.exe' does not have an entrypoint defined
The compiler is expecting to produce an executable program. These have an entry point in the form of the Main method. Our bank account class does not have a main method because the program will never start by actually running an account. Instead other programs will run and create accounts when they need them. So, the compiler cannot make an executable file, since it does not know where the program should start. Note that I have made the Account class public. This is so that classes in other files can make use of it. You can apply the protection levels to classes in the same way that you can protect class members. Generally speaking your classes will be public if you want them to be used in libraries.
Creating a Library
I solve this problem by asking the compiler to produce a library instead. I use the target option to the compile command. Options are a way of modifying what a command does. The compiler can tell that it is being given an option because options always start with a slash character:
csc /target:library AccountManagement.cs
The compiler will not now look for a Main method, because it has been told to produce a library rather than a program. If I look at what has been created I find that the compiler has not made me an executable, instead it has made me a library file:
175
Advanced Programming
Program Organisation
AccountManagement.dll
The language extension dll stands for dynamic link library. This means that the content of this file will be loaded dynamically as the program runs.
Using a Library
Now I have a library I next have to work out how to use it. Firstly I'm going to create another source file called AccountTest.cs. This contains a Main method which will use the Account class:
using System; class AccountTest { public static void Main () { Account test = new Account(); test.PayInFunds (50); Console.WriteLine ("Balance:" + test.GetBalance()); } }
This makes a new account, puts 50 pounds in it and then prints out the balance. If I try to compile it I get a whole bunch of errors:
AccountTest.cs(5,3): error CS0246: The type or namespace name 'Account' could not be found (are you missing a using directive or an assembly reference?) AccountTest.cs(6,3): error CS0246: The type or namespace name 'test' could not be found (are you missing a using directive or an assembly reference?) AccountTest.cs(7,37): error CS0246: The type or namespace name 'test' could not be found (are you missing a using directive or an assembly reference?)
The problem is that the compiler does not know to go and look in the file AccountManagement.cs to find the Account class. This means that it fails to create test, which causes further errors. To solve the problem I need to tell the compiler to refer to AccountManagement to find the Account class:
csc /reference:AccountManagement.dll AccountTest.cs
The reference option is followed by a list of library files which are to be used. In this case there is just the one file to look at, which is the library that contains the required class. The compiler now knows where to find all the parts of the application, and so it can build the executable program.
Both these files need to be present for the program to work correctly. This because of the "dynamic" in dynamic link library. It means that library is only loaded when the program runs, not when it is built.
176
Advanced Programming
Program Organisation
Unhandled Exception: System.IO.FileNotFoundException: File or assembly name AccountManagement, or one of its dependencies, was not found. File name: "AccountManagement" at AccountTest.Main() lots of other stuff
This means that we need to be careful when we send out a program and make sure that all the components files are present when the program runs.
5.8.2 Namespaces
We can use library files to break up our solution into a number of files. This makes management of the solution slightly easier. But we also have another problem. We don't just want to break things into physical chunks. We also want to use logical ones as well. If you are not sure what I mean by this, consider the situation in our bank. We have decided that Account is a sensible name for the class which holds all the details of a customer account. But if you consider the whole of the bank operations you find that the word "account" crops up all over the place. The bank will buy things like paper clips, rubber stamps, little pens on chains (pre-supplied with no ink in of course) and the like from suppliers. You could say that the bank has an account with such a supplier. It may well wish to keep track of these accounts using a computer system. Perhaps the programmers might decide that a sensible name for such a thing would be Account. Arrgh! We are now heading for real problems. If the two systems ever meet up we can expect a digital fight to the death about what "Account" really means. Which would be bad. We could solve the problem by renaming our account class CustomerBankAccount. But this would be messy, and mean that at design time we have to make sure that we name all our classes in a way which will always be unique. A far better way would be to say that we have a CustomerBanking namespace in which the word Account has a particular meaning. We can also have an
177
Advanced Programming
Program Organisation
EquipmentSupplier namespace as well. This prevents the two names from clashing, in that they are not defined in the same namespace.
I have used the namespace keyword to identify the namespace. This is followed by a block of classes. Every class declared in this block is regarded as part of the given namespace, in this case CustomerBanking. A given source file can contain as many namespace definitions and each can contain as many classes as you like.
This creates a variable which can refer to instances of the Account class. The Account class we use is the one in the CustomerBanking namespace. A name like this, with the namespace in front, is known as a fully qualified name. If we want to create an instance of the class we use the fully qualified name again:
test = new CustomerBanking.Account();
If I want to use the Account class from the CustomerBanking namespace I have to modify my test class accordingly.
178
Advanced Programming
Program Organisation
Using a namespace
If you are using a lot of things from a particular namespace C# provides a way in which you can tell the compiler to look in that namespace whenever it has to resolve the name of a particular item. We do this with the using keyword:
using CustomerBanking ;
- it automatically looks in the CustomerBanking namespace to see if there is a class called Account. If there is it uses that. We have already used this technique a lot. The System namespace is where a lot of the library components are located. We can use these by means of fully qualified names:
System.Console.WriteLine ( "Hello World" );
- at the very top. This means that the programmer can just write:
Console.WriteLine ( "Hello World" );
The compiler goes "ah, I'll go and have a look for a thing called Console in all the namespaces that I've been told to use". If it finds one, and only one, Console then all is well and it uses that. If it finds two Console items (if I was an idiot I could put a Console class in my CustomerBanking namespace I suppose) it will complain that it doesn't know which to use.
Nesting Namespaces
You can put one namespace inside another. This allows you to break things down into smaller chunks. The System namespace is like this, there is a namespace which is part of the System namespace which is specifically concerned with Input/Output. You get to use the items in the System.IO namespace with an appropriate include:
using System.IO ;
Of course the namespaces that you use should always be carefully designed. But then again, you'd probably figured that one out already.
179
Advanced Programming
Debugging
5.9 Debugging
Some people are born to debug. Not everyone is good at debugging, some will always be better than others. Having said that there are techniques which you can use to ease the process, and we are going to explore some of these here. The good news is that if you use a test driven approach to your development the number of faults that are found by the users should be as small as possible, but there will still be some things that need to be fixed. Incidentally, the reason why problems with programs are called bugs is that the original one was caused by an actual insect which got stuck in some contacts in the computer, causing it to fail. This is not however how must bugs are caused. It is very rarely that you will see your program fail because the hardware is faulty, I've been programming for many years and only seen this in a handful of occasions. The sad thing is that most of the bugs in programs have been put there by programmers. In other words, debugging is working very hard to find out how stupid you were in the first place.
180
Advanced Programming
Debugging
Faults which sometimes happen are a pain. What this means is that you do not have a definite sequence of events which causes the system to fail. This does not mean that there is no sequence (unless you are suffering from a hardware induced transient of some kind which is rather rare) but that you have not found the sequence yet. An example would be a print function which sometimes crashes and works at other times. You might track this down to the number of pages printed, or the amount of text on the page, or the size of the document which is being edited when the print is requested. The manifestation of a fault may be after the error itself has actually occurred, for example a program may fail when an item is removed from a database, but the error may be in the routine which stored the item, or in code which overwrote the memory where the database lives. A fault may change or disappear when the program itself is changed; errors which overwrite memory will corrupt different parts of the program if the layout of the code changes. This can lead to the most annoying kind of fault, where you put in extra print statements to find out more about the problem, and the problem promptly disappears! If you suspect such an error, your first test is to change the code and data layout in some way and then re-run the program. If the fault changes in nature this indicates problems with program or data being corrupted.
5.9.2 Bugswatting
You can split faults into other categories, where the program crashes (i.e. stops completely) or when it does the wrong thing. Surprisingly, crashes are often easier to fix. They usually point to: a state which is not catered for (make sure that all selection statements have a default handler which does something sensible and that you use defensive programming techniques when values are passed between modules) programs that get stuck into loops, do while constructions which never fail, for loops which contain code which changes the control variable, methods that call themselves by mistake and recurse their way into stack overflow an exception that is not caught properly
If your program does the wrong thing, this can be harder to find. Look for: use of un-initialised members of classes typographical errors (look for incorrectly spelt variable names, incorrect comparison operators, improperly terminated comments) logical errors (look for faults in the sequence of instructions, invalid loop termination's, wrongly constructed logical conditions)
As I said above, some folks are just plain good at finding bugs. Here are some tips: Don't make any assumptions. If you assume that "the only way it could get here is through this sequence" or "there is no way that this piece of code could be obeyed" you will often be wrong. Rather than make assumptions, add extra code to prove that what you think is happening is really happening. Explain the problem to someone else - even if it is just the cat! The act of explaining the problem can often lead you to deduce the answer. It is best if the person you are talking to is highly sceptical of your statements. Look at all possible (if seemingly unrelated) factors in the manifestation of the bug. This is particularly important if the bug is intermittent. If the bug appears on Friday afternoons on your UNIX system, find out if another department uses the machine to do a payroll run at that time and fills up all the scratch space, or the loading department turn on the big hoist and inject loads of noise into the mains.
181
Advanced Programming
Debugging
Leave the problem alone for a while. Go off and do something different and you may find that the answer will just appear. Alternatively you may find the answer as soon as you come back to the problem. Remember that although the bug is of course impossible, it is happening. This means that either the impossible is happening, or one of your assumptions that it is impossible is wrong! Can you get back to a state where the bug was not present, and then look at the changes made since? If the system is failing as a result of a change or, heaven forbid, a bug fix, try to move back to a point where the bug is not there, and then introduce the changes until the bug appears. Alternatively, look carefully at how the introduction of the feature affects other modules in the system. A good Source Code Control System is very valuable here, in that it can tell you exactly what changes have been made from one version to the next. One thing that I should make clear at this point is that the process of debugging is that of fixing faults in a solution which should work. In other words you must know how the program is supposed to work before you try and fix problems with what it actually does. I have been nearly moved to tears by the sight of people putting in another loop or changing the way their conditions to "see if this will make the program work". Such efforts are always doomed, just like throwing a bunch of electrical components at the wall and expecting a DVD player to land on the floor is also not going to work.
182
Advanced Programming
The End?
context of the development. For example, a fault in a video game is much less of a problem than one in an air traffic control system. The key to making software that is as perfect as possible is to make sure that you have a good understanding of the problem that you are solving, that you know how to solve it before you start writing code and that you manage your code production process carefully. Read some of the recommended texts at the end of this document for more on this aspect of programming.
5.10.1
Continuous Development
A good programmer has a deliberate policy of constantly reviewing their expertise and looking at new things. If you are serious about this business you should be reading at least one book about the subject at any one time. I have been programming for as long as I can remember but I have never stopped learning about the subject. And I've never stopped programming, reading books about programming and looking at other people's code.
5.10.2
Further Reading
Code Complete Second Edition: Steve McConnell
Published by Microsoft: ISBN 0-7356-1967-0 Not actually a book about C#. More a book about everything else. It covers a range of software engineering and programming techniques from the perspective of "software construction". If you have any serious intention to be a proper programmer you should/must read/own this book.
How to be a programmer
This web site is also worth a read, as it covers the behaviours of a programmer very well indeed: http://samizdat.mines.edu/howto/HowToBeAProgrammer.html
183
Glossary of Terms
The End?
6 Glossary of Terms
Abstract
Something which is abstract does not have a "proper" existence as such. When writing programs we use the word to mean "an idealised description of something". In the case of component design an abstract class contains descriptions of things which need to be present, but it does not say how they are to be realised. In C# terms a class is abstract if it is marked as such, or if it contains one or more method which is marked as abstract. You can't make an instance of an abstract class, but you can use it as the basis of, or template for, a concrete one. For example, we may decide that we need many different kinds of receipt in our transaction processing system: cash receipt, cheque receipt, wholesaler receipt etc. We don't know how each particular receipt will work inside, but we do know those behaviours which it must have to make it into a receipt. We can therefore create an abstract Receipt class which serves as the basis of all the concrete ones. Each "real" receipt class is created by extending the parent, abstract one. This means that it is a member of the receipt family (i.e. it can be treated as a Receipt) but it works in its own way.
Accessor
An accessor is a method which provides access to the value managed within a class. Effectively the access is read only, in that the data is held securely in the class but code in other classes may need to have access to the value itself. An accessor is implemented as a public method which will return a value to the caller. Note that if the thing being given access to is managed by reference the programmer must make sure that it is OK for a reference to the object is passed out. If the object is not to be changed it may be necessary to make a copy of the object to return to the caller.
Base
base is a C# keyword which has different meanings depending on the context in which it is given. It is used in a constructor of a child class to call the constructor in the parent. It is also used in overriding methods to call the method which they have overridden.
Call
When you want to use a method, you call it. When a method is called the sequence of execution switches to that method, starting at the first statement in its body. When the end of the method, or the return statement, is reached the sequence of execution returns to the statement immediately following the method call.
Class
A class is a collection of behaviours (methods) and data (properties). It can be used to represent a real world item in your program (for example bank account). Whenever you need to collect a number of things into a single unit you should think in terms of creating a class.
184
Glossary of Terms
The End?
Code Reuse
A developer should take steps to make sure that a given piece of program is only written once. This is usually achieved by putting code into methods and then calling them, rather than repeating the same statements at different parts of a program. The use of class hierarchies is also a way of reusing code. You only need to override the methods that you want to update.
Cohesion
A class has high cohesion if it is not dependent on/coupled to other classes.
Collection
The C# library has the idea of a collection as being a bunch of things that you want to store together, for example all the players in a football team or all the customers in a bank. One form of a collection is an array. Another is the hashtable, which allows you to easily find a particular item based on a key value in that item. A collection class will support enumeration which means that it can be asked to provide successive values to the C# foreach construction. Whenever you want to store a number of things together you should consider using a collection class to do this for you. The collection classes can be found in the System.Collections namespace.
Compiler
A compiler takes a source file and makes sense of it. The compiler will produce an executable file which is run. Writing compilers is a specialised business, they used to be written in assembly language but are now constructed in high level languages (like C#!). A compiler is a large program which is specially written for a particular computer and programming language. Most compilers work in several phases. The first phase, the pre-processor, takes the source which the user has written and then finds all the individual keywords, identifiers and symbols producing a stream of program source which is fed to the "parser" which ensures that the source adheres to the grammar of the programming language in use. The final phase is the code generator, which produces the executable file which is later run by the host.
Component
A component is a class which exposes its behaviour in the form of an interface. This means that rather than being thought of in terms of what it is (for example a BabyCustomerAccount) it is thought of in terms of what it can do (implement the IAccount interface to pay in and withdraw money). When creating a system you should focus on the components and how they interact. Their interactions are expressed in the interfaces between them.
Constructor
A constructor is a method in a class which is called as a new instance of that class is created. Programmers use constructors to get control when an instance is created and set up the values inside the class. If a class is a member of a hierarchy, and the parent class has a constructor, it is important when making the child that you ensure the parent constructor is called correctly. Otherwise the compiler will refuse to compile your program.
185
Glossary of Terms
The End?
Coupling
If a class is dependent on another the two classes are said to be coupled. Generally speaking a programmer should strive to have as little coupling in their designs as possible, since it makes it harder to update the system. Coupling is often discussed alongside cohesion, in that you should aim for high cohesion and low coupling.
Creative Laziness
It seems to me that some aspects of laziness work well when applied to programming. Code reuse, where you try and pick up existing code, is a good example of this. Making sure the spec. is right before you do anything is another way of saving on work. However, structuring the design so that you can get someone else to do a lot of the work is probably the best example of creative laziness in action.
Delegate
A delegate is a type safe reference to a method. A delegate is created for a particular method signature (for example this method accepts two integers and returns a float). It can then be directed at a method in a class which matches that signature. Note that the delegate instance holds two items, a reference to the instance/class which contains the method and a reference to the method itself. The fact that a delegate is an object means that it can be passed around like any other. Delegates are used to inform event generators (things like buttons, timers and the like) of the method which is to be called when the event they generate takes place.
Dependency
In general, too much dependency in your designs is a bad thing. A dependency relationship exists between two classes when a change in code in one class means that you might have to change the other as well. It usually means that you have not properly allocated responsibility between the objects in your system and that two objects are looking after the same data. As an example see the discussion of the CustomerAccount and ChildAccount Load method on page 145. Dependency is often directional. For example a user interface class may be dependent on a business object class (if you add new properties to the business object you will need to update the user interface). However, it is unlikely that changes to the way that the user interface works will mean that the business object needs to be altered.
Event
An event is some external occurrence which your program may need to respond to. Events include things like mouse movement, keys being hit, windows being resized, buttons being pressed, timers going tick etc. Many modern programs work on the basis of events which are connected to methods. When the event occurs the method is called to deliver notification. Windows components make use of delegates (a delegate is a type safe reference to a method) to allow event generators to be informed of the method to be called when the event takes place.
Exception
An exception is an object that describes something bad that has just happened. Exceptions are part of the way that a C# program can deal with errors. When a running
186
Glossary of Terms
The End?
program gets to a position where it just cant continue (perhaps a file cannot be opened or an input value makes no sense) it can give up and throw an exception:
throw new Exception("Oh Dear");
The Exception object contains a Message property that is a string which can be used to describe what has gone wrong. In the above example the message would be set to Oh Dear. If the exception is not caught the program will end at that point. You can make a program respond to exceptions by enclosing code that might throw an exception in a try catch construction. When the exception is thrown the program transfers execution to the code in the catch clause:
try { // code that might throw an exception } catch (Exception e) { // code that is obeyed if an exception is thrown // e is a reference to the exception that was thrown } finally { // code in here is always obeyed, whether the exception // was thrown or not }
A try catch construction can also contain a finally clause, which contains code that is executed whether or not the exception is thrown.
Hierarchy
A hierarchy is created when a parent class is extended by a child to produce a new class with all the abilities of the parent plus new and modified behaviours specific to the requirements of the child. Extending the child produces a further level of hierarchy. The classes at the top of the hierarchy should be more general and possibly abstract (for example BankAccount) and the classes at the lower levels will be more specific (for example ChildBankAccount).
Immutable
An immutable object cannot be changed. If an attempt is made to change the content of an immutable object a new object is created with the changed content and the "old" one
187
Glossary of Terms
The End?
remains in memory. The string class is immutable. This gives strings a behaviour similar to value types, which makes them easier to use in programs.
Inheritance
Inheritance is the way in which a class extends a parent to allow it to make use of all the behaviours and properties the parent but add/customise these for a slightly different requirement. For more detail see the description of hierarchy.
Interface
An interface defines a set of actions. The actions are defined in terms of a number of method definitions. A class which implements an interface must contain code for each of the methods. A class which implements an interface can be referenced purely in terms of that interface. Interfaces make it possible to create components. We don't care precisely what the component is, as long as it implements the interface it can be thought of purely in terms of that ability.
Library
A library is a set of classes which are used by other programs. The difference between a library and a program is that the library file will have the extension .dll (dynamic link library) and will not contain a main method.
Machine code
Machine Code is the language which the processor of the computer actually understands. It contains a number of very simple operations, for example move an item from the processor into memory, or add one to an item in the processor. Each particular range of computer processors has its own specific machine code, which means that machine code written for one kind of machine cannot be easily used on another.
Member
A member of a class is declared within that class. It can either do something (a method) or hold some data (variable). Methods are sometimes called behaviours. Data members are sometimes called properties.
Metadata
Metadata is data about data. It operates at all kinds of levels. The fact that the age value is held as an integer is metadata. The fact that it cannot be negative is more metadata. Metadata must be gathered by the programmer in consultation with the customer when creating a system.
Method
A method is a block of code preceded by a method signature. The method has a particular name and may return a value. It may also accept a parameter to work on. Methods are used to break a large program up into a number of smaller units, each of which performs one part of the task. They are also used to allow the same piece of program to be used in lots of places in a large development. If a method is public it can be called by code other classes. A public method is how an object exposes its behaviours. A message is delivered to an object by means of a call of a method inside that object.
188
Glossary of Terms
The End?
Mutator
A mutator is a method which is called to change the value of a member inside an object. The change will hopefully be managed, in that invalid values will be rejected in some way. This is implemented in the form of a public method which is supplied with a new value and may return an error code.
Namespace
A namespace is an area within which a particular name has a particular meaning. Namespaces let you reuse names. A programmer creating a namespace can use any name in that namespace. A fully qualified name of a resource is prefixed by the namespace in which the name exists. A namespace can contain another namespace, allowing hierarchies to be set up. Note that a namespace is purely logical in that it does not reflect where in the system the items are physically located, it just gives the names by which they are known. C# provides the using keyword to allow namespaces to be "imported" into a program.
Overload
A method is overloaded when one with the same name but a different set of parameters is declared in the same class. Methods are overloaded when there is more than one way of providing information for a particular action, for example a date can be set by providing day, month, year information or by a text string or by a single integer which is the number of days since 1st Jan. Three different, overloaded, methods could be provided to set the date, each with the same name. In that case the SetDate method could be said to have been overloaded.
Override
Sometimes you may want to make a more specialized version of an existing class. This may entail providing updated versions of methods in the class. You do this by creating a child class which extends the parent and then overriding the methods which need to be changed. When the method is called on instances of the child class, the new method is called, not the overridden one in the parent. You can use the base keyword to get access to the overridden method if you need to.
Portable
When applied to computer software, the more portable something is the easier it is to move it onto a different type of computer. Computers contain different kinds of processors and operating systems which can only run programs specifically written for them. A portable application is one which can be transferred to a new processor or operating system with relative ease. High Level languages tend to be portable, machine code is much harder to transfer.
Private
A private member of a class is only visible to code in methods inside that class. It is conventional to make data members of a class private so that they cannot be changed by code outside the class. The programmer can then provide methods or C# properties to manage the values which may be assigned to the private members. The only reason for not making a data member private is to remove the performance hit of using a method to access the data.
189
Glossary of Terms
The End?
Property
A property is an item of data which is held in an object. An example of a property of a BankAccount class would be the balance of the account. Another would be the name of the account holder. The C# language has a special construction to make the management of properties easy for programmers.
Protected
A protected member of a class is visible to methods in the class and to methods in classes which extend this class. It is kind of a half way house between private (no access to methods outside this class) and public (everyone has access). It lets you designate members in parent classes as being visible in the child classes.
Public
A public member of a class is visible to methods outside the class. It is conventional to make the method members of a class public so that they can be used by code in other class. A public method is how a class provides services to other classes.
Reference
A reference is a bit like a tag which can be attached to an instance of a class. The reference has a particular name. C# uses a reference to find its way to the instance of the class and use its methods and data. One reference can be assigned to another. If you do this the result is that there are now two tags which refer to a single object in memory.
Signature
A given C# method has a particular signature which allows it to be uniquely identified in a program. The signature is the name of the method and the type and order of the parameters to that method: void Silly(int a, int b) has the signature of the name Silly and two int parameters. void Silly(float a, int b) has the signature of the name Silly and an float parameter followed by an integer parameter. This means that the code: Silly(1, 2) ; - would call the first method, whereas: Silly(1.0f, 2) ; - would call the second. Note that the type of the method has no effect on the signature.
Source file
You prepare a source file with a text editor of some kind. It is text which you want to pass through a compiler to produce a program file for execution.
Static
In the context of C# the keyword static makes a member of a class part of a class rather than part of an instance of the class. This means that you dont need to create an
190
Glossary of Terms
The End?
instance of a class to make use of a static member. It also means that static members are accessed by means of the name of their class rather than a reference to an instance. Static members are useful for creating class members which are to be shared with all the instances, for example interest rates for all the accounts in your bank.
Stream
A stream is an object which represents a connection to something which is going to move data for us. The movement might be to a disk file, to a network port or even to the system console. Streams remove the need to modify a program depending on where the output is to be sent or input received from.
Structure
A structure is a collection of data items. It is not managed by reference, and structures are copied on assignment. Structures are also passed by value into methods. Structures are useful for holding chunks of related data in single units. They are not as flexible as objects managed by reference, but they are more efficient to use in that accessing structure items does not require a reference to be followed in the same way as for an object.
Subscript
This is a value which is used to identify the element in an array. It must be an integer value. Subscripts in C# always start at 0 (this locates, confusingly, the first element of the array) and extend up to the size of the array minus 1. This means that if you create a four element array you get hold of elements in the array by subscript values of 0,1,2 or 3. The best way to regard a subscript is the distance down the array you are going to move to get the element that you want. This means that the first element in the array must have a subscript value of 0.
Syntax Highlighting
Some program editors (for example Visual Studio) display different program elements in different colours, to make it easier for the programmer to understand the code. Keywords are displayed in blue, strings in red and comments in green. Note that the colours are added by the editor, and there is nothing in the actual C# source file that determines the colour of the text.
Test harness
The test harness will contain simulations of those portions of the input and output which the system being tested will use. You put your program into a test harness and then the program thinks it is in the completed system. A test harness is very useful when debugging as it removes the need for the complete system (for example a trawler!) when testing.
This
this is a C# keyword which has different meanings depending on the context in which it is given. It is used in a constructor of a class to call another constructor. It is also used as a reference to the current instance, for use in non-static methods running inside that instance.
191
Glossary of Terms
The End?
Typesafe
We have seen that C# is quite fussy about combining things that should not be combined. Try to put a float value into an int variable and the compiler will get cross at this point. The reason for this is that the designers of the language have noticed a few common programming mistakes and have designed it so that these mistakes are detected before the program runs, not afterwards when it has crashed. One of these mistakes is the use of values or items in contexts where it is either not meaningful to do this (put a string into a bool) or could result in loss of data or accuracy (put a double into a byte). This kind of fussiness is called type safety and C# is very big on it. Some other languages are much more relaxed when it comes to combining things, and work on the basis that the programmer knows best. They assume that just because code has been written to do something, that thing must be the right thing. C# is very keen on this (as am I). I think it is important that developers get all the help they can to stop them doing stupid things, and a language that stops you from combining things in a way that might not be sensible is a good thing in my book. Of course, if you really want to impose your will on the compiler and force it to compile your code in spite of any type safety issues you can do this by using casting.
Unit test
A unit test is a small test which exercises a component and ensures that it performs a particular function correctly. Unit tests should be written alongside the development process so that they can be applied to code just after (or in test drive development just before) the code is written.
Value type
A value type holds a simple value. Value types are passed as values into method calls and their values are copied upon assignment; i.e. x = y causes the value in y to be copied into x. Changes to the value in x will not affect the value of y. Note that this is in contrast to reference types where the result of the assignment would make x and y refer to the same instance. Virtual Method A method is a member of a class. I can call the method to do a job. Sometimes I may want to extend a class to produce a child class which is a more specialized version of that class. In that case I may want to replace (override) the method in the parent with a new one in the child class. For this to take place the method in the parent class must have been marked as virtual. Only virtual methods can be overridden. Making a method virtual slightly slows down access to it, in that the program must look for any overrides of the method before calling it. This is why not all methods are made virtual initially.
192
Glossary of Terms
Index
(
() 20, 22
/
/* 38
;
; 21
{
{ 20
+
+ 23
A
abstract classes and interfaces 116 methods 115 references to abstract classes 118 accessor 93, 127 ArrayList 134 access element 135 find size 135 remove element 135 search 136 arrays 61 elements 62 subscripts 62 two dimensional 63 assignment 21 assignments 49
tests 126 class 19 class members 90 Close 73 code reuse 109 color 155 column printing 51 comments 38 compiler 15 component properties 154 components 104, 154 computer 6 data processing 7 embedded system 8 hardware & software 6 program 7 programming 8 condition 39 Console 21 constants 41 constructor 98 chaining 115 custom 99 default 98 failure 102 management 101 overloading 100 parameters 99 context 24 continue 46 CPU 165 custom constructors 99
D
data 7, 26 data protection 112 default 70 default constructor 98 delegates 162 pointers 162 Dictionary 137 double 20
B
base method 112, 120 block 41 boolean 31 brace 20 break 46 button 156 button events 157
E
edit form 159 elements 62 enumerated types 76 Equals method 121 escape sequence 29 event management 158 events 162 exception 171 class 172 multiple catches 173 throwing 172 type 172 expressions 32 data types 35
C
C 14 camel case 32 case 70 casting 34 chain saw 14 char 29, 124 case 125
Glossary of Terms
operands 33 operators 33
M
member protection 91 MessageBox 161 metadata 11 methods 17, 52 base method 112 Main 17 overriding 111 replace 113 sealed 114 stopping overriding 114 virtual 111 mutator 91, 127
F
files streams 141 foreach 143 Form 153, 159 Dispose 161 modal 161 fridge 6 fully qualified name 73
G
Generics 134, 136 global namespace 178 gozzinta 21 graphical user interface 153 GUID 109
N
namespace 19, 177 global 178 nesting 179 separate files 179 using 179 namespaces 73 narrowing 34 nested blocks 58 nesting namespaces 179 new 85, 98
H
hash table 131 Hashtable 132
I
identifier 17, 31 if 39 immutable 124 information 7 inheritance 109 integers 27 interface abstraction 104 design 108 implementing 106 implementing multiple 108 reference 106
O
object class 119 object oriented 15 objects 83, 89, 115 container 129 equals 121 factory method 141 key 133 properties 127 this 123 operands 33 operating system 7 operators 33 combining logical 40 priority 33 relational 39 unary 48 out parameters 56 overflow 27 overloading constructors 100 overriding 111
K
keyword 17
L
label 154 Length 125 library 175 List class 136 literal values 26, 35 loops 43 break 46 continue 46 do - while 43 for 44 while 44
P
parameters 22, 53 parenthesis 23 Parse 22 pause 169 plumber 9 pointers 162 print formatting 50
Glossary of Terms
print placeholders 50 priority 33 private 91, 92 program Main 19 program flow 38 programmer 6 Programmers Point Always provide an equals behaviour 122 Avoid Many Dimensions 65, 67, 68 Block Copy is Evil 110 Break Down Your Conditions 41 Bug fixes cause bugs 182 Casts Add Clarity 36 Check your maths 27 Choose Variable Types Carefully 31 Clever is not always Clever 46 Construction Should Be Planned 102, 103 Data Structures are Important 89 Delegates are strong magic 164, 165 Design wth Methods 55 Design Your Class Construction Process 115 Design your error exceptions yourself 173 Design your fault reporting 180 Document your Side Effects 56 Dont use new fangled stuff just because it is there 138 Don't Replace Methods 114 Enums are Good 76, 78 Every Message Counts 153 Flipping Conditions 47 Fully Qualified Names are Good 180 Give Your Variables Sensible Names 32 Good Communicators 13 Great Programmers 16 Importance of Hardware 8 Importance of Specification 10 Interfaces are just promises 109 Internationalise your code 104, 105 Know Your Data Source 20 Langauges do Help 57 Make sure you use the right equals 121 Metadata 11 Metadata members Members and Methods 92 Not everything should be possible 90 Pace Your Comments 38 Plan for Failure 58 Plan Your Variables 61 private data and public methods 92, 93, 94 Production Code 148 Program Layout 25 Programming Languages 14 Programs often fail in the error handlers 174 Static Data Members are Useful and Dangerous 96 Static Method Members can be used to make libraries 98 Streams are wonderful 142 Strive for Simplicity 49 Structures are Crucial 82 Stupid Computers 14
Switches are Good 71 There is only so much you can do 141 Try to avoid the garbage man 87 Use break With Care 46 Use Numbers Not Messages 150 Use Simple Variable Types 29 Users have strong opinions about the user interface 161 Version Control and Change Management 177, 180, 182 programming languages 13 properties 90, 126 in interfaces 128 public 92 punctuation 25
R
ReadLine 21 recipie 16 reference 84, 85, 87 parameters 56 to abstract class 118 replacing methods 113 return 53
S
scope 76, 87 sealed 114 searching 131 semicolon 21 source files 174 Star Trek 7 statement 17 returning values 49 static 19, 94 data 95 methods 96 story telling 37 stream 71 streams 141 StreamWriter 72 string 29, 124 comparison 125 editing 125 immutable 124 Length 125 literal 23 StringBuilder 126 structures 79 accessing 80 defining 79 subscripts 62 switch 68, 69 case 70 System namespace 19
Glossary of Terms
T
text based editing 152 TextBox 155 this 123 Thread 165 Monitor 169 mutex 168 Sleep 169 Start 166 threads 163 ThreadStart 166 ToString 119 ToUpper 125 Trim 126
U
unicode 30 user 6 using 19
V
value parameters 55 variable scope 58 variables 16, 26 arrays 61 assignment 32 bool 31 char 29 declaring 26 double 20 float 28 list 20 string 30 structures 79 text 29 types 26 verbatim 30 virtual methods 111 void 19
W
widening 34 WriteLine 23