Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft CSharp Programming For The Absolute Beginner (2002) [eng]-1.pdf
Скачиваний:
46
Добавлен:
16.08.2013
Размер:
15.71 Mб
Скачать

Figure 8.1: The player within the square outline has the ball. He can either pass to another player or make a shot on the goal.

Introducing Arrays

Although you can’t tell by looking at the Soccer game from the outside, the game relies heavily on lists of information. Lists are so useful that every major programming language supports them in one way or another. Of course, programmers love fancy words and refuse to use the straightforward term list. They much prefer the more esoteric term array. An array is not complicated. It is simply a list of variables with the same name and the same type of data. If you’ve played golf (or even miniature golf), you’ve probably seen a scorecard that looks something like the one in Figure 8.2.

204

Figure 8.2: A golf scorecard is a good example of an array in everyday life.

A small golf course might have nine holes. These holes are usually numbered. Each golfer keeps track of how many strokes it takes to get the ball in the cup. The golfer might refer to the first score as the score for hole 1. The next score would be the score for hole 2, and so on. Programming languages usually refer to this kind of structure as an array. C# refers to an array with a bracket ([ ]) notation. If you wanted to make a golfing game in C# (hey, that’s a great idea, but I’ve already thought up the game for this chapter), you would make an array of integers named score. You could assign the score for the first hole by writing score[1] = 3;. If you wanted to know the score for the fourth hole, you could write something like MessageBox.Show(score[4].ToString());.

Exploring the Counter Program

A sample program can clarify the concept of arrays, so take a look at the Counter program featured in Figures 8.3 and 8.4. The program counts to 4 and then repeats. The counting behavior itself is unremarkable. The more interesting problem is how to get the text values One, Two, and so on, in the right order. You guessed it. An array is the secret to this program’s magic.

205

Figure 8.3: The label says zero before the user presses any buttons.

206

Figure 8.4: After the user presses the button, the value changes.

Creating an Array of Strings

The Counter program has only a few controls. The label holding the numeric text is named lblOutput, and the button is named btnNext. I added two variable declarations inside the class definition:

private int counter;

private string[] numbers = new String[5];

The counter variable is used to count numerically from 0 to 4 and also serves as an index in the array. Whenever you use an array, usually you also use an integer variable as an index.

The second statement declares numbers as an array of five strings. Making an array is different from making ordinary variables because an array is a type of object in C#. The term string[] indicates that you want to make an array of string objects, instead of an ordinary string variable. The = new String[5] part of the statement creates the array in memory and sets aside enough memory for five elements.

Trick If you don’t know how many elements will be in your array, you can look up the more flexible ArrayList object. This object allows you to have an array with an indeterminate length. However, it requires much more work on the processor, so it can slow down your programs. Normal arrays are fine for most work.

207

Соседние файлы в предмете Программирование на C++