arrays. overview u general discussion uses structure declaration u searching u control arrays
TRANSCRIPT
![Page 1: Arrays. Overview u General Discussion Uses Structure Declaration u Searching u Control Arrays](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56649f135503460f94c279ec/html5/thumbnails/1.jpg)
Arrays
![Page 2: Arrays. Overview u General Discussion Uses Structure Declaration u Searching u Control Arrays](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56649f135503460f94c279ec/html5/thumbnails/2.jpg)
Overview
General Discussion Uses Structure Declaration
Searching Control Arrays
![Page 3: Arrays. Overview u General Discussion Uses Structure Declaration u Searching u Control Arrays](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56649f135503460f94c279ec/html5/thumbnails/3.jpg)
Arrays
From time to time an object (a variable, a picture, a label or a command) does not serve as well as a set of objects of a similar kind addressed with a common name.
For example, in the grade point calculator, rather than having hamburgers, fries and drinks each with its own price and quantity measures, we might better describe the situation as an array of elements
![Page 4: Arrays. Overview u General Discussion Uses Structure Declaration u Searching u Control Arrays](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56649f135503460f94c279ec/html5/thumbnails/4.jpg)
3
Data Arrays A variable like Stuff
defines a data object into which a single quantity can be placed
A variable like Product defines a data object into which an array of information can be placed
210 Product
Stuff
![Page 5: Arrays. Overview u General Discussion Uses Structure Declaration u Searching u Control Arrays](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56649f135503460f94c279ec/html5/thumbnails/5.jpg)
Arrays
Arrays are useful for when there are many instances of similar data
Instead of ten integer variables holding ten values, a single array can handle all ten
intdintc inte
intj
intf
intb
intainti
inthintgintArray
![Page 6: Arrays. Overview u General Discussion Uses Structure Declaration u Searching u Control Arrays](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56649f135503460f94c279ec/html5/thumbnails/6.jpg)
Arrays
Each variable location in an array is called an element Use Dim statement to create, define dimension and type
Dim intArray(1 to 10) as Integer creates the structure below
Treat as normal variables; Reference using subscripts: intArray(4) = “114”
intArray
114
![Page 7: Arrays. Overview u General Discussion Uses Structure Declaration u Searching u Control Arrays](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56649f135503460f94c279ec/html5/thumbnails/7.jpg)
Arrays
Arrays allow programmers to write code once, but have it applied to many different data elements
Compare adding all the integers stored in the variables…
intdintc inte
intj
intf
intb
intainti
inthintgintArray
intSum = inta + intb + intc _+ intd + inte + intf + intg _+ inth + inti + inj
![Page 8: Arrays. Overview u General Discussion Uses Structure Declaration u Searching u Control Arrays](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56649f135503460f94c279ec/html5/thumbnails/8.jpg)
Arrays
Arrays allow programmers to write code once, but have it applied to many different data elements
…to adding all the elements of the array
intdintc inte
intj
intf
intb
intainti
inthintgintArray
For intI = 1 to 10 intSum = intSum + intArray(intI)Next
![Page 9: Arrays. Overview u General Discussion Uses Structure Declaration u Searching u Control Arrays](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56649f135503460f94c279ec/html5/thumbnails/9.jpg)
Arrays Arrays come in different
dimensions. Two dimensional arrays
require two subscripts elements are listed (row, column)
Example DIM intSales(1 to 3, 1 to 4) as
integer What is intSales(2, 3)?
1
2
3
1 2 3 4
1402
7532
![Page 10: Arrays. Overview u General Discussion Uses Structure Declaration u Searching u Control Arrays](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56649f135503460f94c279ec/html5/thumbnails/10.jpg)
Arrays
Three dimensional arrays require three subscripts
Example Dim intSales (1 to 3, 1 to 4, 1 to 2) as Integer
1402
2134
4521
3425
So the number 2134 is found where? the number 4521 is ______ the number 1402 is ______ the number 3425 is ______
![Page 11: Arrays. Overview u General Discussion Uses Structure Declaration u Searching u Control Arrays](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56649f135503460f94c279ec/html5/thumbnails/11.jpg)
Parallel Arrays
Dim strName (1 to 5) as String
Dim intSales (1 to 5) as Integer
So, if we know that Smith is in strName(2), we also know his or her sales is in intSales(2)
strName intSales
Jones
Smith
Frank
Able
Zean
1402
2301
0231
6762
0199
![Page 12: Arrays. Overview u General Discussion Uses Structure Declaration u Searching u Control Arrays](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56649f135503460f94c279ec/html5/thumbnails/12.jpg)
Creating Arrays Dim statement
Array name; Dimensions; Bounds; TypeDim strName (1 to 3, 1 to 4) as String
Using Any integer literal or variable can be used to subscript Must stay within the bounds Must have the correct number of subscripts Must assign the correct type of data
Given: Dim strName (1 to 3, 1 to 4) as String Is strName (0,0) = “Jones” valid? Is strName (2) = “Smith” valid? Is strName (4,3) = “Larry” valid? Is strName(3,4) = 1402 valid?
![Page 13: Arrays. Overview u General Discussion Uses Structure Declaration u Searching u Control Arrays](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56649f135503460f94c279ec/html5/thumbnails/13.jpg)
Searching Arrays
strTarget = “Q” intFound = 0For intI = 1 to 10 If strTarget = strArray(intI) Then intFound = intI Exit For End IfNext strArray
A F S V W Q Z X Y L
• Searching implies looking for a target• A simple sequential search looks through the
array for the target
![Page 14: Arrays. Overview u General Discussion Uses Structure Declaration u Searching u Control Arrays](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56649f135503460f94c279ec/html5/thumbnails/14.jpg)
Dynamic Arrays VB allows a programming to re-dimension arrays during
the run of the program. Rules for Dynamic Arrays
Use empty dimension list: intArray() as Integer Use ReDim to assign dimensions:
ReDim intArray (1 to intN, 1 to 2) Use ReDim to change Bounds
ReDim intArray (1 to (intN + 10), 1 to 3)
Use Preserve keyword to save data in arrayReDim Preserve intArray(1 to intN, 1 to
5)Preserve only allows changes in the last dimension
![Page 15: Arrays. Overview u General Discussion Uses Structure Declaration u Searching u Control Arrays](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56649f135503460f94c279ec/html5/thumbnails/15.jpg)
Control Arrays
VB allows the programmer to create arrays of controls on the form Just as there are data arrays, there may be arrays of objects, such
as command buttons, labels or picture boxes An array of labels might be made with one label for each product,
or for each price An array of command might be made to handle adding products
Characteristics Name with subscripted elements .Index property is the subscript
![Page 16: Arrays. Overview u General Discussion Uses Structure Declaration u Searching u Control Arrays](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56649f135503460f94c279ec/html5/thumbnails/16.jpg)
Control Arrays
Create an toolbox object, perhaps a command button, changing its (name) and caption.
![Page 17: Arrays. Overview u General Discussion Uses Structure Declaration u Searching u Control Arrays](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56649f135503460f94c279ec/html5/thumbnails/17.jpg)
Control Arrays1. Copy and paste the object (Ctrl-C)(Ctrl-V) or Edit/Copy then Edit/Paste
2. The message “Do you want to create a control array?” appears. Answer “yes”
3. Type Edit/Paste or (Ctrl-V) several more times to make an array of commands
![Page 18: Arrays. Overview u General Discussion Uses Structure Declaration u Searching u Control Arrays](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56649f135503460f94c279ec/html5/thumbnails/18.jpg)
Control Arrays
The Properties Window shows it all.
Note that the 4th command created is shown as “cmdButn(3)”
![Page 19: Arrays. Overview u General Discussion Uses Structure Declaration u Searching u Control Arrays](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56649f135503460f94c279ec/html5/thumbnails/19.jpg)
Control Arrays
Arrange the buttons on the form as desired
Double clicking on any one of them opens the code window
![Page 20: Arrays. Overview u General Discussion Uses Structure Declaration u Searching u Control Arrays](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56649f135503460f94c279ec/html5/thumbnails/20.jpg)
Control Arrays
The event handler is a bit strange. There is now a “parameter” inside the normally blank parentheses:
Private Sub cmdButn_Click(Index As Integer)
lblOutput.Caption = "## " & Index & " ##"
End Sub
![Page 21: Arrays. Overview u General Discussion Uses Structure Declaration u Searching u Control Arrays](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56649f135503460f94c279ec/html5/thumbnails/21.jpg)
Control Arrays
Run the process
Click each button
Observe the effect
![Page 22: Arrays. Overview u General Discussion Uses Structure Declaration u Searching u Control Arrays](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56649f135503460f94c279ec/html5/thumbnails/22.jpg)
Control Arrays
Using the same procedure, you can make arrays of labels, or of pictures, or of check boxes, etc.
You can change the GPA calculator program to make arrays of products, commands, product counts, and total costs
![Page 23: Arrays. Overview u General Discussion Uses Structure Declaration u Searching u Control Arrays](https://reader036.vdocuments.mx/reader036/viewer/2022062304/56649f135503460f94c279ec/html5/thumbnails/23.jpg)
Summary Arrays are sets of variables know as elements These elements operate just like simple variables Arrays are created with a DIM statement that defines; boundaries,
dimensions and type When referencing elements in an array, you must have the correct
number of subscripts and their values must be within the bounds of the dimensions
The operation of looking through an array for a “target” is called searching