Download presentation
Presentation is loading. Please wait.
2
“Regular” variable ◦ Holds a single value ◦ For example Dim Student1 as String Dim Student2 as String Dim Student3 as String … Dim Studentn as String ◦ If you have 10 students, you can also have variables representing students’ grades You can total up and average these grades for the “class” of 10 students ClassAvg = StudentGrade1 + StudentGrade2 + … What happens if you have 1000 students?
3
An array is a set of values that are logically related to each other ◦ Monthly rainfall for the year ◦ Number of students in each grade in a grade school ◦ Number of employees in each department of an organization ◦ Employees within a single department Arrays let us refer to values by the same name ◦ Individual items are referred to as elements and we use a number to indicate which specific element in the array we want ◦ Element number is called the “index” or “subscript”
4
Dim Students(6) as Integer ◦ Creates an array using the name “Students” with “containers” called “elements” ◦ Elements start number at “0” 7 elements exist in the Students array Elements of the "students" array
5
Assignment statements are used to place a value into a specific element ◦ We just need to specify which element Students(4) = 18 Places the value of 18 into the 5 th element (we start counting at 0) of the array Retrieving a value is as simple as specifying the element number ◦ System.Console.WriteLine( Cstr( Students(4) ) )
6
Assuming that we have an array “Students” which represents the grades kindergarten through 6 th grade (kindergarten being grade “0”) Dim Students(6) as Integer Students(0)=11 Students(1)=14 Students(2)=17 Students(3)=11 Students(4)=19 Students(5)=21 Students(6)=10 Students 11 14 17 11 19 21 10 0123456
7
We can get the total number of students in the school by adding the values in all the elements together TotalStudents = Students(0) + Students(1) + Students(2) + Students(3) + Students(4) + Students(5) + Students(6) ◦ Typical processing of an array is by using a “LOOP” Dim Grade,TotalStudents as Integer TotalStudents = 0 For Grade = 0 to 6 TotalStudents = TotalStudents + Students(Grade) Next Grade Students 11 14 17 11 19 21 10 0123456
9
Students 11 14 17 11 19 21 10 0123456
10
If you decide to include up to the 8 th Grade ReDim Students(8) Anything in the array will be lost!
11
Suppose we have a company with 100 employees ◦ Each employee has an Employee ID # A “whole counting number” ◦ We wish to keep track of his/her salary Could be a decimal Create an array to hold this information Dim Employee(2,100) as Single Create a 3 x 101 matrix Note: data is of type “single” even though employee number is an integer Salary is a decimal type (non-integer), so employee # 21 is 21.00 in the array We don’t have to use the “0 th ” row or column, they can remain empty Alternatively Dim Employee(1,99) as Single
12
Add as many pairs of parentheses after the variable name as there are levels of nested arrays ◦ Dim x as Integer( ) ( ) = New Integer( ) ( ) { } Add the same number of paired parentheses to the “New” clause ◦ You don’t need more that one pair of braces if you’re not giving an element values
13
Two dimensional, but not “rectangular” ◦ Array of months Each element is an array of days Dim sales()() As Double = New Double(11)( ) { } Dim month As Integer Dim days As Integer For month = 0 To 11 days = DateTime.DaysInMonth(Year(Now), month + 1) sales(month) = New Double(days - 1) {} Next month declares an array variable to hold an array of arrays with elements of the Double Data Type Each element or the array “sales” is itself an array representing a month Each month holds values for each day in that month
14
Technically you cannot do this ◦ Arrays are of the same type The type we will use is “Object” Dim x as Object( ) = New Object( ) { } Example Dim employeeData(3) as Object employeeData(0) = “John Doe” employeeData(1) = “13 East North Rd.” employeeData(2) = 22 employeeData(3) = #04/01/1982# Age = CInt( employeeData(2) ) DOB = CDate( employeeData(3) ) Negative performance consideration
15
Errors in declaring / initializing ◦ Supplying the “new” clause after specifying dimension lengths ◦ Omitting “new” when specifying element values ◦ Using the “new” clause without braces Out of bounds ◦ Going beyond the upper or lower bounds of the dimensions Specifying dimension ◦ GetLowerBound and GetUpperBound methods are “0-based” ◦ Lbound and Ubound are “1-based”
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.