CSCI 3327 Visual Basic Chapter 7: Arrays UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous lecture slides. – Xiang Lian
Objectives In this chapter, you will: Learn how arrays are used to store elements Get familiar with the declaration, initialization, and reference to elements of the array Know how to pass arrays to methods Learn how to use For Each … Next statement to iterate through elements in the array
Arrays An array is a group of variables (called elements) containing values that all have the same data type For most languages, the number of elements are static and defined at declaration In Visual Basic, arrays can be re-dimensioned at the execution time Single dim, two dim and multiple dim
Example of an Array The first element is the zeroth element C(0) -45 C(1) 6 C(2) C(3) 72 C(4) 34 C(5) 39 C(6) 98 C(7) -1345 C(8) 939 C(9) 10 C(10) 40 C(11) 33 The first element is the zeroth element The highest position number is array’s upper bound Array C on RHS: 11 Access array elements Use index (or subscript) E.g., C(8)
Declaring an Array Declaration (different ways) Dim Grades As Integer() Grades = New Integer(0 to 11) {} {} initializer list Grades = New Integer(11) {} Dim Grades() As Integer Declare and initialize with initializer lists Dim Grades() As Integer={90,80,88,90,70} Local type inference Dim Grades ={90,80,88,90,70}
Default Initialization When you do not provide initializer list, elements are initialized to default values Numeric primitive data type: 0 Boolean data type: False String data type: Nothing
Referring to an Element Name and position Grades(6) = 85 First element is 0th index (subscript) Index must be a nonnegative integer Index may have a calculated value like Grades(i+4*2) Know the difference between index and value stored there Sum = Grades(1) + Grades(2) + Grades(3)
Array is a Class Class is in System.Array X = C.GetUpperBound(0) -45 C(1) 6 C(2) C(3) 72 C(4) 34 C(5) 39 C(6) 98 C(7) -1345 C(8) 939 C(9) 10 C(10) 40 C(11) 33 Class is in System.Array X = C.GetUpperBound(0) Will give 11 (see the declaration) X= C.GetLength(0) Will give 12 X=C.Length
Example 7.2: InitializeArrays.vb URL: http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html GetUpperBound(0)
Example 7.3: SumArray.vb URL: For i = 0 To values.GetUpperBound(0) http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html For i = 0 To values.GetUpperBound(0) Total+= values(i) Next
Example 7.4: StudentPoll.vb URL: http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html IndexOutOf Range Exception Try … statements Catch ex As IndexOutOfRangeException 'handling exceptions End Try
Example 7.5: DiceStatistics.vb URL: http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html Summarize statistics of the dice CType
Example 7.6: Flag Quiz URL: Properties of ComboBox http://media.pearsoncmg.com/ph/esm/deitel/vb_htp_2010/codeexamples.html Properties of ComboBox DropDownStyle: DropDownList (not editable) MaxDropDownItems: the maximum number of items displayed at one time CountriesComboBox.DataSource=countries 'array CountriesComboBox.Selected Index = 0 Replace method of String: country.Replace(" ","")
Passing Arrays Always as byVal (it is just a pointer to the 0th element) Declaration Dim scores(80) As Integer Function standardDeviation(ByVal scores() As Integer, ByVal numscores As Integer) As Double Call stdDev = standardDeviation(scores, numScores)
Manipulating Array Using Loops For i = 0 To scores.GetUpperBound(0) Total += scores(i) Next
For Each … Next Statement For Each grade In gradeArray Total += grade Next ----------------------------------------------------------- For Each grade As Integer In gradeArray