18
Introduction to Programming with Vi sual Basic 6.0 by McKeown and Pierc y 1 Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arr ays List Arrays Finding Ite ms in Array s Multiple Fo rms 2-Dimension al Arrays Chapter 6: Using Arrays

Chapter 6 : Using Arrays

  • Upload
    arawn

  • View
    45

  • Download
    3

Embed Size (px)

DESCRIPTION

Chapter 6 : Using Arrays. Using Control Arrays. 1. A control array is group of the same type control which is assigned a single name. 2. The individual controls are identified by the control array name and an index value. - PowerPoint PPT Presentation

Citation preview

Page 1: Chapter 6 :  Using Arrays

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

1Copyright © 2001 by Wiley. All rights reserved.

Chapter 6: Using Arrays

Control Arrays

List Arrays

Finding Items in Arrays

Multiple Forms

2-Dimensional Arrays

Chapter 6: Using Arrays

Page 2: Chapter 6 :  Using Arrays

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

2Copyright © 2001 by Wiley. All rights reserved.

Chapter 6: Using Arrays

Control Arrays

List Arrays

Finding Items in Arrays

Multiple Forms

2-Dimensional Arrays

Using Control Arrays

1. A control array is group of the same type control which is assigned a single name.2. The individual controls are identified by the control array name and an index value.3. The Case decision structure can be useful in working with a control array.4. To create a control array, just enter the same name for more than one control and reply “yes” to the query.5. The order in which the controls are entered determines the index values for the controls in the control array.

Page 3: Chapter 6 :  Using Arrays

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

3Copyright © 2001 by Wiley. All rights reserved.

Chapter 6: Using Arrays

Control Arrays

List Arrays

Finding Items in Arrays

Multiple Forms

2-Dimensional Arrays

Using List Arrays

1. Arrays are lists or tables of data which have a single name. Individual array elements are identified by the array name and one or more subscripts or index values.2. To be used in Visual Basic, an array must be declared; arrays can be fixed size or dynamic size (we use only fixed arrays.)3. The default lower limit on an array index is zero but this can be changed to one or any other value.4. It is not possible to exceed the upper limit or go below the lower limit on the array index values.

Page 4: Chapter 6 :  Using Arrays

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

4Copyright © 2001 by Wiley. All rights reserved.

Chapter 6: Using Arrays

Control Arrays

List Arrays

Finding Items in Arrays

Multiple Forms

2-Dimensional Arrays

Comparing Listboxes and Arrays

curPrices(0)

curPrices(1)

curPrices(3)

curPrices(2)

curPrices(4)

curPrices(6)

curPrices(5)

curPrices(7)

curPrices(8)

curPrices(9)

Array curPrices

Page 5: Chapter 6 :  Using Arrays

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

5Copyright © 2001 by Wiley. All rights reserved.

Chapter 6: Using Arrays

Control Arrays

List Arrays

Finding Items in Arrays

Multiple Forms

2-Dimensional Arrays

Entering Array Data

1. Arrays can be input with any of the three types of loops discussed earlier--event driven, for-next, or while/until loops.2. Event-driven loops and For-Next loops are good for keyboard input; While or Until loops are good for input from a file.3. In any case, each array element must be input using its subscript value.4. With an event driven loop, each click of a button increments a counter and inputs the corresponding array element.5. With a For-Next loop, you must input the number of array elements. The array values must be input with an Inputbox with the For-next counter variable matching the array index.6. With an Until loop, you can input from a file. You must increment a counter that matches the index for the array element.

Page 6: Chapter 6 :  Using Arrays

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

6Copyright © 2001 by Wiley. All rights reserved.

Chapter 6: Using Arrays

Control Arrays

List Arrays

Finding Items in Arrays

Multiple Forms

2-Dimensional Arrays

Code to Input from For-Next and Until Loops

For-Next LoopDim intCounter As IntegerintNumPrices = CInt(InputBox("How many prices?"))For intCounter = 0 To intNumPrices - 1 curPrices(intCounter ) = CCur(InputBox("Next _ price:"))Next

Until LoopOpen "a:\prices.txt" For Input As #1Do Until EOF(1) Input #1, curPrices(intNumPrices) intNumPrices = intNumPrices + 1Loop

Page 7: Chapter 6 :  Using Arrays

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

7Copyright © 2001 by Wiley. All rights reserved.

Chapter 6: Using Arrays

Control Arrays

List Arrays

Finding Items in Arrays

Multiple Forms

2-Dimensional Arrays

Processing Arrays1. Typical array operations include summing and averaging the array values and finding the largest or smallest value in the array.2. Working with arrays usually involves a For-Next loop.3. Summing curPrices array elements involves using a statement of the form: curSum = curSum + curPrices(intCounter) 4. Averaging curPrices array elements involves dividing the curSum by the number of elements.5. Finding the largest or smallest value involves multiple comparisons of array elements.6. To find the maximum Price, you must compare each array value to the current highest price; if it is higher, it becomes the highest price. Do this using For-Next loop and If-then If curMax < curPrice(intCounter) then curMax = curPrice(intCounter) End if

Page 8: Chapter 6 :  Using Arrays

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

8Copyright © 2001 by Wiley. All rights reserved.

Chapter 6: Using Arrays

Control Arrays

List Arrays

Finding Items in Arrays

Multiple Forms

2-Dimensional Arrays

Code to Find MaximumValue in List

Private Sub cmdFindMax_Click() Dim intCounter As Integer Dim curLargest As Currency curLargest = curPrices(0) For intCounter = 1 To intNumPrices - 1 If curPrices(intCounter) > curLargest Then curLargest = curPrices(intCounter) End If Next txtMaxPrice.Text = Format(curLargest, _ "currency")End Sub

Page 9: Chapter 6 :  Using Arrays

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

9Copyright © 2001 by Wiley. All rights reserved.

Chapter 6: Using Arrays

Control Arrays

List Arrays

Finding Items in Arrays

Multiple Forms

2-Dimensional Arrays

Finding Items and Working With Multiple Lists

1. It is possible to work with multiple list arrays by matching the index values for the arrays.2. Finding a specific array value involves comparing each array element to the desired value. A flag is often used to indicate whether a match was found.3. The MsgBox can be used as a function by including multiple parameters within parentheses.4. Parameters can be internal constants.5. The MsgBox function returns a value which can be checked to determine which button was clicked.

Page 10: Chapter 6 :  Using Arrays

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

10Copyright © 2001 by Wiley. All rights reserved.

Chapter 6: Using Arrays

Control Arrays

List Arrays

Finding Items in Arrays

Multiple Forms

2-Dimensional Arrays

Pseudocode to Find Price for Part Identifier

Begin procedure to find part identifierInput part identifier Set Flag to falseRepeat for each part in parts list

If part identifier = identifier on parts list thenFlag = TrueSave index of part identifier on parts list

End decisionEnd repeatIf Flag = true

Use saved index to display part identifier and price Else

Display message that part not on parts listEnd decision

End procedure

Page 11: Chapter 6 :  Using Arrays

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

11Copyright © 2001 by Wiley. All rights reserved.

Chapter 6: Using Arrays

Control Arrays

List Arrays

Finding Items in Arrays

Multiple Forms

2-Dimensional Arrays

Code to Find Price for a Part Identifier

Private Sub cmdFind_Click() Dim intCounter As Integer, intResults As Integer Dim strFindID as String, blnFound As Boolean Dim intPriceIndex as Integer strFindID = InputBox("Input part identifier to find") blnFound = False For intCounter = 0 To intNumPrices – 1 If strFindID = strPartId(intCounter) Then blnFound = True intPriceIndex = intCounter Exit For End If Next If blnFound Then txtFound.Text = strPartId(intPriceIndex ) & " " & _ Format(curPrices(intPriceIndex ), "Currency") Else intResults = MsgBox("Part not found", VbExclamation, _ "Price Search") End IfEnd Sub

Page 12: Chapter 6 :  Using Arrays

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

12Copyright © 2001 by Wiley. All rights reserved.

Chapter 6: Using Arrays

Control Arrays

List Arrays

Finding Items in Arrays

Multiple Forms

2-Dimensional Arrays

Code to Save Index of Part with Highest Price

Private Sub cmdFindMax_Click() Dim intCounter As Integer Dim curLargest As Currency Dim intMaxIndex as Integer curLargest = curPrices(0) For intCounter = 1 To intNumPrices - 1 If curPrices(intCounter) > curLargest Then curLargest = curPrices(intCounter) intMaxIndex = intCounter End If Next txtMaxPrice.Text = strPartId(intMaxIndex) & _ " " & Format(curPrices(intMaxIndex), _ "Currency")End Sub

Page 13: Chapter 6 :  Using Arrays

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

13Copyright © 2001 by Wiley. All rights reserved.

Chapter 6: Using Arrays

Control Arrays

List Arrays

Finding Items in Arrays

Multiple Forms

2-Dimensional Arrays

Working with Multiple Forms

1. It is possible to have multiple forms in a project. To display a form, use Object.ShowTo hide the form, use Object.Hide2. A new form can be added using the Project|New Form menu option or the New Form icon on the toolbar.3. If a control on one form is referred to on another form, the form name must be included as a part of the control reference.

Page 14: Chapter 6 :  Using Arrays

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

14Copyright © 2001 by Wiley. All rights reserved.

Chapter 6: Using Arrays

Control Arrays

List Arrays

Finding Items in Arrays

Multiple Forms

2-Dimensional Arrays

Searching in Strings

* Searching for strings using a loop requires that you use UCase() or LCase() to convert all strings to the same case.* To search for a sub-string within a string, you use the function: (Instr(string to search, search string) For example, InStr(“Go Dogs”, “D “) returns 4 because “D” is in the 4th position. If the character does not exist, InStr() returns a zero.*NOTE: InStr() is case sensitive!!!

Page 15: Chapter 6 :  Using Arrays

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

15Copyright © 2001 by Wiley. All rights reserved.

Chapter 6: Using Arrays

Control Arrays

List Arrays

Finding Items in Arrays

Multiple Forms

2-Dimensional Arrays

Code to Search for a Partial Video NamePrivate Sub cmdSearch_Click() Dim strVideoName As String, intCounter As Integer Dim intNumMatches As Integer strVideoName = txtSearch.Text lstVideos.Clear lstVideos.AddItem "Video Name" For intCounter = 0 To intNumVideos If InStr(UCase(Videos(intCounter)), _ UCase(strVideoName)) > 0 Then intNumMatches = intNumMatches + 1 lstVideos.AddItem strVideos(intCounter) End If Next If intNumMatches = 0 Then MsgBox ("No matching videos found! Try again.") ElseIf intNumMatches <= 5 Then lstVideos.AddItem Str(intNumMatches) + " videos found" Else lstVideos.Clear MsgBox ("Too many matching videos!") End IfEnd Sub

Page 16: Chapter 6 :  Using Arrays

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

16Copyright © 2001 by Wiley. All rights reserved.

Chapter 6: Using Arrays

Control Arrays

List Arrays

Finding Items in Arrays

Multiple Forms

2-Dimensional Arrays

Code to Display Video Information

Private Sub lstVideos_Click() Dim strVideoName As String Dim intCounter As Integer strVideoName = lstVideos.Text lstVideos.Clear For intCounter = 0 To intNumVideos If strVideoName = strVideos(intCounter) Then lstVideos.AddItem strVideoName & " " & _ Format(strVideoPrice(intCounter), _ "currency") & " " & strVideoLoc(intCounter) Exit For End If NextEnd Sub

Page 17: Chapter 6 :  Using Arrays

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

17Copyright © 2001 by Wiley. All rights reserved.

Chapter 6: Using Arrays

Control Arrays

List Arrays

Finding Items in Arrays

Multiple Forms

2-Dimensional Arrays

Two-Dimensional Arrays

• When you are working with a table instead of a list, you have a 2-dimensional array.

• 2-D arrays use two subscripts with the first subscript referring to the row value and the second subscript referring to the column value.

• Nested For-next loops are often used to work with 2-D arrays with the inner loop matching the columns and the outer loop matching the rows.

Page 18: Chapter 6 :  Using Arrays

Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

18Copyright © 2001 by Wiley. All rights reserved.

Chapter 6: Using Arrays

Control Arrays

List Arrays

Finding Items in Arrays

Multiple Forms

2-Dimensional Arrays

Using the Step Commands for Debugging

The Step Commands

Step intoStepout of

Step Over

If you select Step Into, you can “step” through thecode, line by line and note the result of the code in thevarious windows--Watch, Locals, or Immediate.