1 / 18

Chapter 6 : Using Arrays

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.

arawn
Download Presentation

Chapter 6 : Using Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 6: Using Arrays Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

  2. 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. Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

  3. 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. Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

  4. Array curPrices curPrices(0) curPrices(1) curPrices(2) curPrices(3) curPrices(4) curPrices(5) curPrices(6) curPrices(7) curPrices(8) curPrices(9) Comparing Listboxes and Arrays Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

  5. 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. Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

  6. Code to Input from For-Next and Until Loops For-Next Loop Dim intCounter As Integer intNumPrices = CInt(InputBox("How many prices?")) For intCounter = 0 To intNumPrices - 1 curPrices(intCounter ) = CCur(InputBox("Next _ price:")) Next Until Loop Open "a:\prices.txt" For Input As #1 Do Until EOF(1) Input #1, curPrices(intNumPrices) intNumPrices = intNumPrices + 1 Loop Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

  7. Processing Arrays 1. 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 Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

  8. 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 Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

  9. 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. Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

  10. Pseudocode to Find Price for Part Identifier • Begin procedure to find part identifier • Input part identifier • Set Flag to false • Repeat for each part in parts list • If part identifier = identifier on parts list then • Flag = True • Save index of part identifier on parts list • End decision • End repeat • If Flag = true • Use saved index to display part identifier and price • Else • Display message that part not on parts list • End decision • End procedure Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

  11. 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 If End Sub Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

  12. 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 Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

  13. Working with Multiple Forms 1. It is possible to have multiple forms in a project. To display a form, use Object.Show To hide the form, use Object.Hide 2. 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. Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

  14. 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!!! Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

  15. Code to Search for a Partial Video Name Private 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 If End Sub Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

  16. 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 Next End Sub Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

  17. 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. Two-Dimensional Arrays Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

  18. Using the Step Commands for Debugging Step into Stepout of The Step Commands Step Over If you select Step Into, you can “step” through the code, line by line and note the result of the code in the various windows--Watch, Locals, or Immediate. Introduction to Programming with Visual Basic 6.0 by McKeown and Piercy

More Related