Download presentation
Presentation is loading. Please wait.
1
Computer Memory 21 oneGrade integer We can declare, assign and manipulate individual variables with ease…
2
Computer Memory 12 21 oneGrade integer twoGrade ThreeGrade integer 23 If we have a “list” of variables, we don’t currently have an elegant way to handle this… 21 fourGrade integer
3
Computer Memory 21 Grades integer What we need is a single variable that can handle multiple values (of the same type) This is an Array 122321
4
Dim ArrayName(UpperBound) As Datatype An array is declared using the following syntax: An array’s name obeys the same rules as when declaring a variable: it begins with a letter and may be followed by any number of letters, underscores, or digits. An array name can be as small as one letter or as large as 255 letters, underscores, and digits. Individual values within an array are selected by using an index. The lowest index of an array is 0, while the upper bound of the array is the highest index in the array. An index must be a Short, an Integer, or a Long data type. The data type is any valid variable like a Short, an Integer, or a String.Arrays
5
Computer Memory shtGrades (short) Index0123 Values0000 Dim shtGrades(3) As Short
6
Computer Memory shtGrades (short) Index0123 Values21000 shtGrades(0) = 21 We can assign an individual variable by using its name, and index. Index and subscript are synonymous
7
Computer Memory shtGrades (short) Index0123 Values21122321 shtGrades(0) = 21 shtGrades(1) = 12 shtGrades(2) = 23 shtGrades(3) = 21 shtGrades(0) = 21 shtGrades(1) = 12 shtGrades(2) = 23 shtGrades(3) = 21
8
Computer Memory shtGrades (short) Index0123 Values21122321 shtGrades(4) = 21 It is our responsibility to make sure that we are always addressing a legal location in the array. shtGrades(-1) = 1 would also crash our program.
9
strSimpsonNames (String) Index01234 Values“Homer”“Marge”“Bart”“Lisa”“Maggie” Arrays can contain any variable type Dim strSimpsonNames(4) As String strSimpsonNames(0) = “Homer" strSimpsonNames(1) = “Marge" strSimpsonNames(2) = “Bart" strSimpsonNames(3) = “Lisa" strSimpsonNames(4) = “Maggie" Dim strSimpsonNames(4) As String strSimpsonNames(0) = “Homer" strSimpsonNames(1) = “Marge" strSimpsonNames(2) = “Bart" strSimpsonNames(3) = “Lisa" strSimpsonNames(4) = “Maggie"
10
strSimpsonNames (Boolean) Index01234 ValuesTrueFalseTrueFalse Arrays can contain any variable type Dim blnSimpsonSexIsMale(4) As Boolean blnSimpsonSexIsMale(0) = True blnSimpsonSexIsMale(1) = False blnSimpsonSexIsMale(2) = True blnSimpsonSexIsMale(3) = False blnSimpsonSexIsMale(4) = False Dim blnSimpsonSexIsMale(4) As Boolean blnSimpsonSexIsMale(0) = True blnSimpsonSexIsMale(1) = False blnSimpsonSexIsMale(2) = True blnSimpsonSexIsMale(3) = False blnSimpsonSexIsMale(4) = False
11
strSimpsonNames (String) Index01234 Values“Homer”“Marge”“Bart”“Lisa”“Maggie” We can use array variables just like regular variables bntRedDemo.Text = strSimpsonNames(0) & “ Simpson” If (strSimpsonNames(3) = “Lisa”) Then bntRedDemo.Text = “Oldest of the Simpson girls” End If bntRedDemo.Text = strSimpsonNames(0) & “ Simpson” If (strSimpsonNames(3) = “Lisa”) Then bntRedDemo.Text = “Oldest of the Simpson girls” End If But the real power of arrays comes from using them with loops
12
Dim blnSimpsonSexIsMale(4) As Boolean blnSimpsonSexIsMale(0) = True blnSimpsonSexIsMale(1) = False blnSimpsonSexIsMale(2) = True blnSimpsonSexIsMale(3) = False blnSimpsonSexIsMale(4) = False Dim intLoopCount As Integer Dim strSex As String = "" For intLoopCount = 0 To 4 If blnSimpsonSexIsMale(intLoopCount) Then strSex = strSex & "M" Else strSex = strSex & "F" End If Next intLoopCount bntRedDemo.Text = strSex
13
Dim blnSimpsonSexIsMale() As Boolean = {True, False, True, False, False} It is often convenient to initialize an array in the same line of code as you declare the array. You can initialize an array by setting it equal to the values you wish to initialize the array to enclosed within curly braces and separated by commas. Note that there is no upper bound used. This value is inferred by counting the number of items in the list
14
Dim blnSimpsonSexIsMale() As Boolean = {True, False, True, False, False} Dim intLoopCount As Integer Dim strSex As String = "" For intLoopCount = 0 To Ubound(blnSimpsonSexIsMale) If blnSimpsonSexIsMale(intLoopCount) Then strSex = strSex & "M" Else strSex = strSex & "F" End If Next intLoopCount bntRedDemo.Text = strSex It is convenient to use the UBound function to determine the upper bound of an array. By passing an array to UBound, the upper bound of the array is returned.
15
Dim strSimpsonNames() As String = {"Homer", "Marge", "Bart", "Lisa", "Maggie"} Dim intLoopCount As Integer For intLoopCount = 0 To UBound(strSimpsonNames) bntRedDemo.Text = strSimpsonNames(intLoopCount) & " Simpson" Next intLoopCount …. Note: Without a call to the Sleep function, this happens so fast all we see is “Maggie Simpson”
16
Dim strSimpsonNames() As String = {"Homer", "Marge", "Bart", "Lisa", "Maggie"} Dim blnSimpsonSexIsMale() As Boolean = {True, False, True, False, False} Dim intLoopCount As Integer For intLoopCount = 0 To UBound(strSimpsonNames) If blnSimpsonSexIsMale(intLoopCount) Then bntRedDemo.Text = "Male : " & strSimpsonNames(intLoopCount) & " Simpson" Else bntRedDemo.Text = "Female : " & strSimpsonNames(intLoopCount) & " Simpson" End If Next intLoopCount ….
17
Useful Exercises Can you extend the code above to count the number of males in the Simpson family? Can you add a new Array variable to hold the Simpson's ages, then find the average age of a Simpson family member? Can you produce this output… Homer Simpson Male: At 36 he is consider old Maggie Simpson Female: At 2 she is consider young ….
18
Homer Simpson Male: At 36 he is consider old Maggie Simpson Female: At 2 she is consider young ….
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.