Arrays Code Arrays Controls Control Arrays Picture Box
Arrays Code: Arrays Controls: Control Arrays, Picture. Box, Timer
Seeding Random Number Generators n n Because the numbers are given by an algorithm, the same numbers come up each time the algorithm runs A “seed” is a parameter in the random number generator; n n changing the seed, changes algorithm results Randomize seeds the generator off the clock so the program is different at different times n Randomize should only be done once, perhaps in a form_initialize method
Variable Arrays n If a group of variables are n n n of the same type (e. g. all int’s) (Note: same type does not mean same value) and play a similar role (i. e. the code for them is nearly identical), it is convenient to make them an array For example, grade 1, grade 2, … becomes grade(0), grade(1), …. Chapter 7 in Deitel, Deitel and Nieto
Arrays in Memory n n n Recall variables correspond to memory locations The elements of an array are stored in consecutive memory locations When you refer to the entire array, you “point” to the first memory location
Before Arrays Dim Dim Dim Grade 1 Grade 2 Grade 3 Grade 4 Grade 5 Grade 6 As As As Integer Integer BECOMES Dim Grade(5) As Integer
After Arrays Average = (Grade 1 + Grade 2 + Grade 3 + _ Grade 4 + Grade 5 + Grade 6) / 6 BECOMES Average = 0 For i = 0 To 5 Average = Average + Grade(i) Next i Average = Average / 6
Array Vocabulary n n Array: refers to the entire set of related variables Element: refers to one member of the array Index: (a. k. a. subscript) is an integer (0, 1, 2, …) that is associated with each element Grade is the array, Grade(3) is an element in that array, that element has an index 3 (note: it is the fourth item)
More Array Vocabulary n Option Base: n n n If you declare Grade(5), VB makes an array with six elements Grade(0) through Grade(5), that is, it starts indexing at 0 If you want to start indexing at 1, you can type Option Base 1 (just below Option Explicit) Note: Control Arrays are always zero based (regardless of Option Base)
Average, High and Low
Average, High and Low
Average, High and Low
Average, High and Low Option Explicit Const SCORENUMBER As Integer = 5 Private Sub cmd. Enter_Click() Dim Score(SCORENUMBER - 1) As Integer Dim i As Integer Dim Sum As Integer Dim Min As Integer Dim Max As Integer Dim Average As Double
Average, High and Low Score(0) = Input. Box("Enter Score 1: ") Sum = Score(0) Min = Score(0) Max = Score(0) ‘Enter first score outside of loop and make it the ‘min and the max
Average, High and Low For i = 1 To SCORENUMBER - 1 Score(i) = Input. Box("Enter Score " & i + 1 & ": ") Sum = Sum + Score(i) If Score(i) < Min Then Min = Score(i) End If If Score(i) > Max Then Max = Score(i) End If Next i
Average, High and Low Average = Sum / SCORENUMBER txt. Average. Text = Average txt. High. Text = Max txt. Low. Text = Min End Sub Private Sub Form_Load() lbl. Instruct. Caption = "Click the button below to begin " & _ "entering the " & SCORENUMBER & " scores and “ & _ “calculate " & “their average, high and low. " End Sub
Scaling n n n To scale something is to change its size (e. g. the number of elements in an array) If a program is written in a way which facilitates various size changes, it is said to be “scalable” The use of the constant SCORENUMBER in the previous code made it scalable, since the code would only one change if the size of the score array was varied
Array as argument of function 1 Const NUMBEROFGRADES As Integer = 5 Private Sub cmd. Enter_Click() Dim Grades(NUMBEROFGRADES-1) As Integer Dim Ave As Double Dim i As Integer For i = 0 To NUMBEROFGRADES-1 Grades(i) = Input. Box("Enter Grade " & i, "Grades") Next i Ave = Average(Grades) txt. Average. Text = Ave End Sub
Array as argument of function 2 Private Function Average(m. Array() As Integer) As Double Dim i As Integer Average = 0. 0 For i = LBound(m. Array) To UBound(m. Array) Average = Average + m. Array(i) Next i Average = Average / (UBound(m. Array) - LBound(m. Array) + 1) End Function
Bounds n n The Lower Bound is the lowest index in an array The Upper Bound is the highest index in an array Lbound(Array. Name) finds the lower bound Ubound(Array. Name) finds the upper bound of an array
Shuffle 'randomly shuffles an array of strings Private Sub Shuffle(m. Array() As String) ‘Strings only Dim i As Integer Dim Swap As String Dim j As Integer dim lb as Integer lb = LBound(m. Array) dim ub as Integer ub = UBound(m. Array) For i = lb To ub j = lb + Int((ub - lb + 1) * Rnd()) Swap = m. Array(i) = m. Array(j) = Swap Next i End Sub
By Reference n n n In VB the default situation is that variables are passed to a function “by reference” (whether or not they are arrays) That is, if the variable is changed in the function, it is also changed in the caller More about this in a future lecture
Control array n If a group of controls are n n of the same type (e. g. all Command. Buttons) and play a similar role (i. e. the code for them is nearly identical), it is convenient to make them an array (p. 274 in Deitel, Deitel and Nieto)
Array of Text. Boxes
Code for Array of Textboxes Option Explicit Private Sub cmd. Sum. Hours_Click() Dim i As Integer 'Counter Dim Total As Integer Total = 0 For i = 0 To txt. Hours. Ubound ‘Ubound is a property NOT function Total = Total + txt. Hours(i). Text Next i txt. Sum. Text = Total End Sub
Which button was pressed?
Which button revisited (code) Option Explicit Dim Store. Button. Index As Integer Private Sub cmd. Button_Click(Index As Integer) Store. Button. Index = Index Comes automatically and End Sub tells us which button Private Sub cmd. Which_Click() was pressed txt. Which. Text = cmd. Button(Store. Button. Index). Caption & _ " was last pressed. " End Sub
Collection of Forms n n n The index property comes into play when a control is an element in an array Notice that forms do not have an index property However there is a way to apply these scaling ideas to forms Forms have an order, the order in which they are loaded The third form loaded can be referred to as Forms(2) (zero-based counting) n Doesn’t matter what it was named
Toward Scaling with Forms
Option Explicit Dim Form. Index As Integer Private Sub opt. Form_Click(Index As Integer) Form. Index = Index + 1 End Sub Private Sub cmd. Go. To. Form_Click() Me. Hide Forms(Form. Index). Show End Sub Private Sub Form_Load() 'the order in which forms are loaded 'is their order in the collection Call Load(frm. Number 1) Call Load(frm. Number 2) Load frm. Number 3 ‘note alternate syntax for calling subs End Sub
Picture. Box n n The picture. Box control (not Image control) is found near the top of the control in the VB IDE The properties we will use n Picture: refers to the picture to be shown n We’ll load the pictures during runtime instead of during development Index: since we will make an array of Picture. Boxes Tag: an internal labeling, we can give matching Picture. Boxes the same Tag and use that property to test for a match
Picture. Box (Cont. ) n We will load the picture using the function Load. Picture(filename) which loads an image file n n n include “the complete path” e. g. Load. Picture(“a: dice 2. gif”) If the image files are in the project folder, one can use App. Path e. g. Load. Picture(App. Path & “dice 2. gif”) Load. Picture() empties the Picture. Box n Note: no argument in Load. Picture()
Craps Example
Craps code Option Explicit Private Sub cmd. Roll_Click() Dim i As Integer Dim Die_Value As Integer For i = 0 To pic. Die. UBound Die_Value = Int(Rnd() * 6 + 1) pic. Die(i). Picture = Load. Picture(App. Path & "dice" & Die_Value & ". gif") Next i End Sub Private Sub cmd. Clear_Click() Dim i As Integer For i = 0 To pic. Die. UBound pic. Die(i). Picture = Load. Picture() Next i End Sub
Timer n Important Properties n n n Interval: the amount of time in milliseconds between Timer events Enabled: When True, Timer events occur, when False, no Timer events occur Events/Methods n Timer: when the timer is enabled a Timer event occurs roughly every N milliseconds, where N is the Interval property
Stop Watch
Stop Watch Code Option Explicit Private Sub cmd. Start_Click() tmr. Stop. Watch. Enabled = Not tmr. Stop. Watch. Enabled If tmr. Stop. Watch. Enabled Then cmd. Start. Caption = "Stop" Else cmd. Start. Caption = "Start" End If End Sub Private Sub tmr. Stop. Watch_Timer() txt. Timer. Text = txt. Timer. Text + 0. 1 ‘interval = 100 End Sub
- Slides: 36