30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 7 Arrays 289<br />

13 For Each grade In gradeArray<br />

14<br />

15 If grade < lowGrade Then<br />

16 lowGrade = grade<br />

17 End If<br />

18<br />

19 Next<br />

20<br />

21 Console.WriteLine("The minimum grade is: {0}", lowGrade)<br />

22 End Sub ' Main<br />

23<br />

24 End Module ' modForEach<br />

The minimum grade is: 68<br />

Fig. Fig. 7.19 7.19 Using For Each/Next with an array (part 2 of 2).<br />

The program behaves similarly <strong>to</strong> procedure Minimum of Fig. 7.17, but consolidates<br />

the nested For structures in<strong>to</strong> one For Each structure. The header of the For Each repetition<br />

structure (line 13) specifies a variable, grade, and an array, gradeArray. The<br />

For Each/Next structure iterates through all the elements in gradeArray, sequentially<br />

assigning each value <strong>to</strong> variable grade. The values are compared <strong>to</strong> variable lowGrade<br />

(line 15), which s<strong>to</strong>res the lowest grade in the array.<br />

For rectangular arrays, the repetition of the For Each/Next structure begins with the<br />

element whose indices are all zero, then iterates through all possible combinations of<br />

indices, incrementing the rightmost index first. When the rightmost index reaches its upper<br />

bound, it is reset <strong>to</strong> zero, and the index <strong>to</strong> the left of it is incremented by 1. In this case,<br />

grade takes the values as they are ordered in the initializer list in line 8. When all the<br />

grades have been processed, lowGrade is displayed.<br />

Although many array calculations are handled best with a counter, For Each is useful<br />

when the indices of the elements are not important. For Each/Next particularly is useful<br />

for looping through arrays of objects, as we discuss in Chapter 10, Object-Oriented <strong>Program</strong>ming:<br />

Polymorphism<br />

In this chapter, we showed how <strong>to</strong> program with arrays. We mentioned that <strong>Visual</strong><br />

<strong>Basic</strong> .<strong>NET</strong> arrays are objects. In Chapter 8, Object-Based <strong>Program</strong>ming, we show how <strong>to</strong><br />

create classes, which are essentially the “blueprints” from which objects are instantiated<br />

(i.e., created).<br />

SUMMARY<br />

An array is a group of contiguous memory locations that have the same name and are of the same<br />

type.<br />

The first element in every array is th e zeroth element (i.e., element 0).<br />

The position number in parentheses more formally is called the index (or the subscript). An index<br />

must be an integer or an integer expression.<br />

All arrays have access <strong>to</strong> the methods and properties of class System.Array, including the<br />

GetUpperBound method and the Length property.<br />

To reference the ith element of an array, use i - 1 as the index.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!