Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are...
-
Upload
clyde-morris -
Category
Documents
-
view
212 -
download
0
Transcript of Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are...
Tutorial 10 1
Variable ArraysA group of variables that have the same name and data type and are related in some way
Can have as many as 60 dimensions in Visual Basic; the most commonly used arrays are one-dimensional and two-dimensional
Programmers use arrays to store related data in the internal memory of the computer
Data stored inside the computer can be both written and read much faster than data stored in a file on a disk
Tutorial 10 2
Variable ArraysA one-dimensional array is simply a row (or column) of variables; a two-dimensional array resembles a table in that it has rows and columns
Each element in an array is identified by a subscript
You refer to an array element by the array’s name followed by the element’s subscript
Tutorial 10 3
One-dimensional Array
Nebraska New Jersey New Mexico Tennessee Texas
Nebraska
New Jersey
New Mexico
Tennessee
Texas
Tutorial 10 4
Two-dimensional Array
Nebraska
New Jersey
New Mexico
Tennessee
Texas
Lincoln
Trenton
Santa Fe
Nashville
Austin
Tutorial 10 5
One-dimensional ArrayDim arrayname(lower subscript To upper subscript) As datatype
Public arrayname(lower subscript To upper subscript) As datatype
Lower subscript and upper subscript are numbers that define the lowest and the highest subscript in the array
The Dim (and Public) statements create and initialize the array variables in memory
Tutorial 10 6
One-dimensional ArrayDim strMonthArray(1 To 6) As String
Dim intSquareArray(1 To 5) As Integer
Dim sngNum(1 To 10) As Single
Dim udtEmp(1 To 20) As EmpStruc
Note: It is not necessary for an array name to contain the word “Array”
Tutorial 10 7
One-dimensional ArraystrMonthArray(1) = “Jan”
strMonthArray(2) = “Feb”
strMonthArray(3) = “Mar”
strMonthArray(4) = “Apr”
strMonthArray(5) = “May”
strMonthArray(6) = “June”
Tutorial 10 8
One-dimensional ArrayFor intNum = 1 to 5
intSquareArray(intNum) = intNum * intNum
Next intNum
For intNum = 1 to 10
sngNum(intNum) = Val(InputBox(“Enter number”))
Next intNum
Tutorial 10 9
One-dimensional Array
Do While Not EOF(1)
intNum = intNum + 1
Input #1, udtEmp(intNum).Name, udtEmp(intNum).Salary
Loop
Tutorial 10 10
Calculating the Average
Declare variables
Repeat for intNum = 1 to 20 by 1
add array score to intTotal variable
End repeat for intNum
Calculate the average by dividing intTotal by 20
Display the average
Tutorial 10 11
Finding the Highest ValueDeclare variables
Assign first array value to intHigh variable
Repeat for intNum = 2 to 20 by 1
If current array value > intHigh value then
assign current array value to intHigh
End If
End repeat for intNum
Display the highest value(stored in intHigh)
Tutorial 10 12
Updating an ArrayDeclare variables
Prompt user for the value to add
Repeat for intNum = 2 to 20 by 1
add value to current array value
End repeat for intNum
Display “Updated” message
Open a file
Repeat for intNum = 1 to 20 by 1
Write array value to file
End repeat for intNum
Close the file
Tutorial 10 13
Two-dimensional Array
Dim arrayname(lower subscript To upper subscript, lower subscript To upper subscript) As datatype
Public arrayname(lower subscript To upper subscript, lower subscript To upper subscript) As datatype
Tutorial 10 14
Two-dimensional ArrayDim strNameArray(1 To 6, 1 To 3) As String
Dim intNum(1 To 5, 1 To 10) As Integer
Dim sngSales(1 To 20, 1 To 2) As Single
Note: It is not necessary for an array name to contain the word “Array”
Tutorial 10 15
Two-dimensional ArrayDeclare variables
Open the file
Repeat for intRow = 1 to 4 by 1
Repeat for intCol = 1 To 2 by 1
Read name from file and store it in
the current array element
End repeat for intCol
End repeat for intRow
Close the file
Tutorial 10 16
DataGrid ControlWhen bound to an ADO data control, the DataGrid control displays the information from the recordset created by the ADO data control’s RecordSource property
The information displays in a row and column format, with each database field appearing in a column, and each record appearing in a row
The intersection of a column and a row is called a cell
Tutorial 10 17
DataGrid ControlUse the Components option on the Project menu to add the Microsoft DataGrid Control 6.0 (OLEDB) tool to the toolbox, then place a DataGrid control in the interface
dgd is the three-character ID for a DataGrid control
You bind a DataGrid control to the ADO data control by setting the DataGrid control’s DataSource property
Tutorial 10 18
DataGrid’s Popup Menu
Tutorial 10 19
Sizing Columns
Tutorial 10 20
General Tab
Tutorial 10 21
General Tab Check Boxes (Properties)
AllowAddNew - allows the user to add records while the application is running
AllowDelete - allows the user to delete records while the application is running
AllowUpdate - allows the user to edit existing records while the application is running
ColumnHeaders - controls whether a header appears above each column
Enabled - controls whether the control responds to a user-generated event
Tutorial 10 22
Columns Tab
Tutorial 10 23
Format Tab
Tutorial 10 24
Layout Tab
Tutorial 10 25
Five DataGrid Events
BeforeColUpdate
AfterColUpdate
AfterColEdit
BeforeUpdate
AfterUpdate
Tutorial 10 26
Referring to a Field Within the Fields Collection
Syntax for referring to a field within a Recordset object’s Fields collection:
[form.]adocontrol.Recordset.Fields(field)
field is the name of the field enclosed in quotation marks
Tutorial 10 27
Creating an Object Variable
Dim objectVariableName As datatype
Dim objFlds As Object
Dim objRst As Recordset
datatype can be a specific object type (such as Sheet, Range, or Recordset) or the keyword Object, which can be used to represent a reference to any object
Tutorial 10 28
Assigning an Object Reference to an Object Variable
Set objectVariableName = object
Set objFlds = adoPay.Recordset.Fields
Set objRst = adoPay.Recordset
After assigning the reference to the object variable, you can use the shorter object variable’s name in the code
Tutorial 10 29
Round FunctionRound(expression [,numdecimalplaces])
Returns a number rounded to a specific number of decimal places
expression is a numeric expression
numdecimalplaces is a number indicating how many places to the right of the decimal are included in the rounding
Tutorial 10 30
Function ProcedureReferred to simply as a function
Like a sub procedure, a function is a block of code that performs a specific task
Unlike a sub procedure, a function returns a value
Some functions are intrinsic (Val, Round)
You also can create your own functions, referred to as user-defined functions
Tutorial 10 31
Function SyntaxPrivate Function functionname [(arglist)] As datatype
[statements]
functionname = expression
End Function
The functionname = expression statement assigns the return value to the function
To invoke a function you need simply to include the function’s name in a statement
Tutorial 10 32
Debugging Technique
You can use the Debug menu’s Toggle
Breakpoint and Add Watch commands
to help you debug an application
Tutorial 10 33
Breakpoint Instruction
Tutorial 10 34
Add Watch Dialog Box
Tutorial 10 35
Watches Window
Tutorial 10 36
Watches Window Showing Current Values of Watch Expressions