Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 1999, by Que Education and Training, Chapter 8, pages 393-429 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.

Similar presentations


Presentation on theme: "© 1999, by Que Education and Training, Chapter 8, pages 393-429 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach."— Presentation transcript:

1 © 1999, by Que Education and Training, Chapter 8, pages 393-429 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Processing Related Data n If the Toy number is given, a long, complex decision can be used to assign Toy name: If ToyID = “1” Then Toy = “Bean bag toys” ElseIf ToyID = “2” Then Toy = “Kids’ books” : ElseIf ToyID = “14” Then Toy = “Footballs” Else Toy = “Basketballs” End If

2 © 1999, by Que Education and Training, Chapter 8, pages 393-429 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Processing Related Data n For a small number of items, using a nested If or Select Case to determine the desired toy given the Toy ID may not seem too bad. n What happens if we have 100 or 1000 or 20000 toys? n A case for arrays: i Data for arrays is typically entered by the user or is already stored in an external file or database. i Once the data is loaded into the Toy() array, the toy name can be retrieved through a single statement (assume ToyID has been set to the correct number):SelectedToy = Toy (ToyId)

3 © 1999, by Que Education and Training, Chapter 8, pages 393-429 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Array Definitions n A group of related, contiguous memory locations with the same name and type. i Also called tables or subscripted variables n Subscript i Used to reference a specific array element by specifying the amount of offset (from the first element) i Appear between parentheses, e.g. 12 in Qz(12) i Like ListIndex property of list controls n Element i Refers to a single element of the array at the specified subscript location

4 © 1999, by Que Education and Training, Chapter 8, pages 393-429 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Declarations: Static Arrays n Declaring one-dimensional, static arrays i {Private | Dim} MyArray(Lsub To Usub) As Type u If Lsub is not specified, it is assumed to be zero u Describes a column of data at locations Lsub to Usub

5 © 1999, by Que Education and Training, Chapter 8, pages 393-429 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Manually Populating the Toy Array Private fToy(1 To 15) As String Private Sub Form_Load() fToy(1) = “Bean bag toys” fToy(2) = “Kids’ books” fToy(3) = “Kids’ software” fToy(4) = “Bicycles” fToy(5) = “Baseballs” fToy(6) = “Mitts” fToy(7) = “Bats” fToy(8) = “Board games” fToy(9) = “Water toys” fToy(10) = “Dress-up gear” fToy(11) = “Crayons” fToy(12) = “Paper supplies” fToy(13) = “Virtual pets” fToy(14) = “Footballs” fToy(15) = “Basketballs” End Sub

6 © 1999, by Que Education and Training, Chapter 8, pages 393-429 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Dynamic Arrays n Declaring static arrays reserves a fixed number of memory locations, which may be much more or much less than what you typically need. n Dynamic arrays can expand and shrink as needed during execution. n Use UBound and LBound to keep track of the size of the array at any point during execution.

7 © 1999, by Que Education and Training, Chapter 8, pages 393-429 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Working with Dynamic Arrays n Declaration {Private | Dim} MyArray() As Type n Initialization Private Sub Form_Load ReDim MyArray(0 To 0) ‘ unused array element End Sub n Addition LastSub = UBound(MyArray) + 1 ReDim Preserve MyArray(0 To LastSub) MyArray(LastSub) = NewValue n Deletion If UBound(MyArray) > 0 Then LastSub = UBound(MyArray) - 1 ReDim Preserve MyArray(0 To LastSub) Else Call MsgBox(“There is nothing to delete -- array is empty.”) End If

8 © 1999, by Que Education and Training, Chapter 8, pages 393-429 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Avoiding Subscript Errors n An attempt to use a nonexistent subscript results in a subscript out of range error. n Example: Assume that the array Test() has elements at subscripts 1 through 5 i A reference to Test(0), Test(6), Test(x), where x is 10, all result in a subscript out of range error n UBound function for above Test array i UBound(Test) returns 5 n LBound function for above Test array i LBound(Test) returns 1

9 © 1999, by Que Education and Training, Chapter 8, pages 393-429 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach For Next Loop to Process Array n Have index vary through all subscripts to do any of the following: i Input values into each array element i Display the content of each array element i Assign each array element a fixed value i Assign each array element a value that varies by its index ‘ Use UBound & LBound to prevent errors: FirstSub = LBound(MyArray) LastSub = UBound(MyArray) ‘ Process all array elements: For Index = FirstSub To LastSub Step 1 steps that access MyArray(Index) Next Index steps to process after loop ends False True Steps to process after loop ends Steps that access MyArray(Index) Index > LastSub? Set Index = FirstSub Index = Index + 1

10 © 1999, by Que Education and Training, Chapter 8, pages 393-429 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach For Each/Next Statement n Like For/Next loop, but you don’t have to write code to go through all the subscripts False True Steps to process after loop ends Steps that access MyArray via ElemName Process all elements? ‘ Declare element index Dim ElemName As Variant ‘ Process all array elements: For Each ElemName In MyArray steps that access ElemName Next ElemName steps to process after loop ends

11 © 1999, by Que Education and Training, Chapter 8, pages 393-429 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Multi-dimensional Static Arrays n A group of related, contiguous memory locations with the same name and type with multiple subscripts required to reference a single cell. n Two-dimensional array has a dimensionality of 2 i Two subscripts are required to reference a specific element n Example {Private | Dim} MyArray(L1 To U1, L2 To U2) As Type i If L1 or L2 are not specified, they are assumed to be zero i Dim StQuizzes(1 To 95, 1 To 10) As Single u The 95 rows represents 95 students u The 10 columns represent 10 different quizzes u StQuizzes(25,7) represents the 25th student’s 7th quiz grade n Three-dimensional array: rows, columns & planes i Dimensionality of 3 requires 3 subscripts for single element

12 © 1999, by Que Education and Training, Chapter 8, pages 393-429 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Two-dimensional Array Dim SeatChart(2,3) As String allocates a 3 x 4 grid of string elements named SeatChart

13 © 1999, by Que Education and Training, Chapter 8, pages 393-429 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach User-defined Data Types n Every element in a multi-dimensional array must be the same type. n The case for user-defined types i Allow storage of multiple sets of related information of different types as a single unit i Must be in the General Declarations section of code window i Can be procedure/function argument as long as passed ByRef i Cannot use For Each with arrays of a user-defined type n Type statement & related declaration {Public|Private} Type MyType FieldName1 As Type FieldName2 As Type : End Type Dim VarName As MyType ‘ simple variable of MyType Dim MyArray() As MyType ‘ dynamic array of MyType

14 © 1999, by Que Education and Training, Chapter 8, pages 393-429 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach User-defined Data Type Example n User-defined type for storing student data Private Type SinfoType Name As String Class As Integer Quiz(1 To 10) As Single Grade As String End Type Dim Sinfo(1 To 100) As SinfoType ‘ 1-column table n Assigning values to elements of user-defined type array Sinfo(5).Name = “John Doe” Sinfo(5).Class = 5 Sinfo(5).Quiz(3) = 7.5 Sinfo(5).Grade = “C” Sinfo(100) = Sinfo(5) ‘ copies John Doe’s info to 100th cell

15 © 1999, by Que Education and Training, Chapter 8, pages 393-429 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Array LookUp (Sequential Search) n Use a loop to vary through all array elements in sequence until a match is found or the last element is reached. False MyArray(Index) = SearchVal Or Index >= EndSub Set Index = StartSub True Found Actions Index = Index + 1 True MyArray(Index) = SearchVal False Not Found Actions

16 © 1999, by Que Education and Training, Chapter 8, pages 393-429 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Array Example - Toy Store

17 © 1999, by Que Education and Training, Chapter 8, pages 393-429 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Toy Store Example: Form-scope Declarations Option Explicit Private Type ToyType ID As Byte Name As String Cost As Currency Qty As Integer End Type Private fToy(1 To 15) As ToyType Private fSearchToyID As Byte Private fTotalToys As Integer Private fTotalPurchase As Currency

18 © 1999, by Que Education and Training, Chapter 8, pages 393-429 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Toy Store Example: Form Load Private Sub Form_Load() Dim Index As Integer fToy(1).Name = "Bean bag toys" fToy(1).Cost = 5.95 ‘ replace with manual assignments to elements 2-14 fToy(15).Name = "Basketballs" fToy(15).Cost = 21.17 For Index = 1 To 15 Step 1 fToy(Index).ID = Index fToy(Index).Qty = 25 Next Index fTotalToys = 0 fTotalPurchase = 0 End Sub

19 © 1999, by Que Education and Training, Chapter 8, pages 393-429 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Toy Store Example: Lookup ID Private Sub cmdLookUp_Click() Dim Index As Byte If ToyIDValid Then fSearchToyID = CByte(txtToyID.Text) Index = 1 Do Until fToy(Index).ID = fSearchToyID Index = Index + 1 Loop If fToy(Index).ID = fSearchToyID Then ‘ do Found processing lblToyName.Caption = fToy(Index).Name If fToy(Index).Qty = 0 Then Call MsgBox("No " & fToy(Index).Name & " in stock.", _ vbOKOnly + vbInformation) cmdAdd.Enabled = False Else cmdAdd.Enabled = True End If Else ‘ do Not Found processing Call MsgBox("No such Toy ID.", vbOKOnly + vbInformation) cmdAdd.Enabled = False End If End Sub

20 © 1999, by Que Education and Training, Chapter 8, pages 393-429 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Toy Store Example: Add to Cart Private Sub cmdAdd_Click() Dim Qty As Integer If QtyValid Then Qty = CInt(txtQty.Text) fTotalToys = fTotalToys + Qty fToy(fSearchToyID).Qty = fToy(fSearchToyID).Qty - Qty fTotalPurchase = fTotalPurchase + fToy(fSearchToyID).Cost * Qty cmdShow.Enabled = True End If End Sub


Download ppt "© 1999, by Que Education and Training, Chapter 8, pages 393-429 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach."

Similar presentations


Ads by Google