Download presentation
Presentation is loading. Please wait.
Published byRosa Johnson Modified over 9 years ago
1
Arrays1 From time to time an object (a variable, a picture, a label or a command) does not serve as well as a set of objects of a similar kind addressed with a common name. For example, at the Pantherburger Shop, rather than having hamburgers, fries and drinks each with its own price and quantity measures we might better describe the situation as an array of objects.
2
Arrays2 Data Arrays (1) A variable like Stuff defines a data object into which a single quantity can be placed. A variable like Product defines a data object into which an array of information can be placed.
3
Arrays3 Data Arrays (2) A data array is defined the same way a normal variable is: Dim intI as Integer Dim strProduct(3) as String Dim curValue(3) as Currency Dim intNmbr(3) as Integer For intI = 0 to 3 intNmbr(intI) = 0 Next intI
4
Arrays4 To Learn More... Help on Data Arrays can be found in the Textbook on pages 183 - 230 (chapter 6) Help can also be found by searching the Visual Basic Documentation on the system with the keyword “dim”. (“dim” is just short for dimension.)
5
Arrays5 Arrays of Objects Just as there are data arrays, there may be arrays of objects, such as command buttons, labels or picture boxes. An array of labels might be made with one label for each product, or for each price. An array of command might be made to handle adding products. Making such arrays involves some trickery...
6
Arrays6 Object Trickery (1) Create an toolbox object, perhaps a command, changing its (name) and caption.
7
Arrays7 Object Trickery (2) 1.Copy and paste the object (Ctrl-C)(Ctrl-V) or Edit/Copy then Edit/Paste. 2.The message “Do you want to create a control array?” appears. Answer “yes”. 3.Type Edit/Paste or (Ctrl-V) several more times to make an array of commands.
8
Arrays8 Object Trickery (3) The Properties Window shows it all. Note that the 4th command created is shown as “cmdButn(3)”
9
Arrays9 Object Trickery (4) If you line them up carefully [put cmdButn(0) on top and cmdButn(3) on the bottom] you have an array of commands. Double click on any one of them and you get the code window.
10
Arrays10 Object Trickery (5) The window is a bit strange. There is now a “parameter” inside the normally blank parentheses: Private Sub cmdButn_Click(Index As Integer) lblOutput.Caption = "## " & Index & " ##" End Sub (I later added the second line, using the parameter “Index” as a variable.)
11
Arrays11 Object Trickery (6) Run the process Click each button Observe the effect
12
Arrays12 Arrays of Objects Using the same procedure, you can make arrays of labels, or of pictures, or of check boxes, etc. You can change the PantherBurger program to make arrays of products, commands, product counts, and total costs.
13
Arrays13 Combination Effects
14
Arrays14 Initialization Code Option Explicit Dim strProd(3) As String Dim curVal(3) As Currency Private Sub Form_Load() strProd(0) = "Burger” : strProd(1) = ”Ch’burger" strProd(2) = "Fries” : strProd(3) = "SoftDrink" curVal(0) = 0.99 : curVal(1) = 1.27 curVal(2) = 0.83 : curVal(3) = 1.02 End Sub
15
Arrays15 Selling the Burgers Private Sub cmdAdder_Click(Index As Integer) lblNmbr(Index).Caption = lblNmbr(Index).Caption + 1 cmdTotalize End Sub Private Sub cmdSubber_Click(Index As Integer) lblNmbr(Index).Caption = lblNmbr(Index).Caption - 1 If blNmbr(Index).Caption < 0 Then lblNmbr(Index).Caption = 0 End If cmdTotalize End Sub
16
Arrays16 Doing the Totals Private Sub cmdTotalize() curSubtot = 0 For intI = 0 To 3 lblCost(i).Caption = curValu(intI) * _ lblNmbr(intI).Caption If lblCost(i).Caption > 0 Then lblCost(i).Visible = True Else lblCost(i).Visible = False End If curSubtot = curSubtot + lblCost(intI).Caption Next intI lblSubtt.Caption = Format(curSubtot, "$#0.00")..
17
Arrays17 Increasing the Menu (1) Option Explicit Dim strBprod(3) As String, strLprod(3) As String Dim curBval(3) As Currency, curLval(3) As Currency Private Sub Form_Load() strBprod(0) = ”Eggs” : strBprod(1) = ”Bacon" strBprod(2) = ”Toast” : strBprod(3) = ”Coffee" strLprod(0) = "Burger” : strLprod(1) = "cheesburger" strLprod(2) = "Fries” : strLprod(3) = "SoftDrink" curBval(0) = 1.99 : curBval(1) = 1.11 curBval(2) = 0.77 : curBval(3) = 1.02 curLval(0) = 0.99 : curLval(1) = 1.27 curLval(2) = 0.83 : curLval(3) = 1.02 End Sub
18
Arrays18 Increasing the Menu (2) If we have commands “Breakfast” and “Lunch” the entire menu can be altered: Private Sub cmdBreakfast_Click() For intI = 0 to 3 strProd(intI) = strBprod(intI) curVal(intI) = curBval(intI) Next intI End Sub Private Sub cmdLunch_Click() For intI = 0 to 3 strProd(intI) = strLprod(intI) curVal(intI) = curLval(intI) Next intI End Sub
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.