Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are...

36
Tutorial 10 1 Variable Arrays A 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

Transcript of Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are...

Page 1: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

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

Page 2: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

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

Page 3: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

Tutorial 10 3

One-dimensional Array

Nebraska New Jersey New Mexico Tennessee Texas

Nebraska

New Jersey

New Mexico

Tennessee

Texas

Page 4: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

Tutorial 10 4

Two-dimensional Array

Nebraska

New Jersey

New Mexico

Tennessee

Texas

Lincoln

Trenton

Santa Fe

Nashville

Austin

Page 5: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

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

Page 6: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

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”

Page 7: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

Tutorial 10 7

One-dimensional ArraystrMonthArray(1) = “Jan”

strMonthArray(2) = “Feb”

strMonthArray(3) = “Mar”

strMonthArray(4) = “Apr”

strMonthArray(5) = “May”

strMonthArray(6) = “June”

Page 8: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

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

Page 9: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

Tutorial 10 9

One-dimensional Array

Do While Not EOF(1)

intNum = intNum + 1

Input #1, udtEmp(intNum).Name, udtEmp(intNum).Salary

Loop

Page 10: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

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

Page 11: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

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)

Page 12: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

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

Page 13: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

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

Page 14: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

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”

Page 15: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

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

Page 16: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

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

Page 17: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

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

Page 18: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

Tutorial 10 18

DataGrid’s Popup Menu

Page 19: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

Tutorial 10 19

Sizing Columns

Page 20: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

Tutorial 10 20

General Tab

Page 21: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

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

Page 22: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

Tutorial 10 22

Columns Tab

Page 23: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

Tutorial 10 23

Format Tab

Page 24: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

Tutorial 10 24

Layout Tab

Page 25: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

Tutorial 10 25

Five DataGrid Events

BeforeColUpdate

AfterColUpdate

AfterColEdit

BeforeUpdate

AfterUpdate

Page 26: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

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

Page 27: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

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

Page 28: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

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

Page 29: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

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

Page 30: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

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

Page 31: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

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

Page 32: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

Tutorial 10 32

Debugging Technique

You can use the Debug menu’s Toggle

Breakpoint and Add Watch commands

to help you debug an application

Page 33: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

Tutorial 10 33

Breakpoint Instruction

Page 34: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

Tutorial 10 34

Add Watch Dialog Box

Page 35: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

Tutorial 10 35

Watches Window

Page 36: Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.

Tutorial 10 36

Watches Window Showing Current Values of Watch Expressions