Download presentation
Presentation is loading. Please wait.
1
Mark Dixon, SoCCE SOFT 131Page 1 07 – Constants, Arrays, & Structures
2
Mark Dixon, SoCCE SOFT 131Page 2 Session Aims & Objectives Aims –To introduce the main concepts involved in handling more complex (multi valued) data Objectives, by end of this week’s sessions, you should be able to: –declare, use, and assign values to, constants, arrays, and structures –create and use ListBoxes
3
Mark Dixon, SoCCE SOFT 131Page 3 Constants similar to variable value given in declaration value can’t be changed useful for removing 'magic numbers' declaration syntax: name used to represent literal value [Global] Const constantname = expression
4
Mark Dixon, SoCCE SOFT 131Page 4 Example 1: CirCalc v1 Private Sub Form_Load() picCircle.ScaleMode = vbMillimeters End Sub Private Sub btnOK_Click() Dim radius As Single Dim circum As Single Dim area As Single radius = Val(txtRadius.Text) circum = 2 * 3.14159265359 * radius area = 3.14159265359 * (radius ^ 2) picCircle.Cls picCircle.Circle (30, 30), radius picInfo.Cls picInfo.Print "Circumference:" picInfo.Print " " & circum & "mm" picInfo.Print "Area:" picInfo.Print " " & area & "mm²" End Sub btnOK txtRadius picCircle picInfo
5
Mark Dixon, SoCCE SOFT 131Page 5 Example 2: CirCalc v2 Private Sub Form_Load() … Private Sub btnOK_Click() Const PI = 3.14159265359 Dim radius As Single Dim circum As Single Dim area As Single radius = Val(txtRadius.Text) circum = 2 * PI * radius area = PI * (radius ^ 2) picCircle.Cls picCircle.Circle (30, 30), radius picInfo.Cls picInfo.Print "Circumference:" picInfo.Print " " & circum & "mm" picInfo.Print "Area:" picInfo.Print " " & area & "mm²" End Sub CirCalc
6
Mark Dixon, SoCCE SOFT 131Page 6 Array Variables (what) multiple values (of same type) – stored in single variable index – identified individual values (called elements) the value of element 3 is 155 IndexValue 0134 1127 2139 3155 4143 5151 6141
7
Mark Dixon, SoCCE SOFT 131Page 7 Array Variables (how) Declaration: Dim varname([first] To [last])[As type] e.g. Dim HR(0 To 6) As Long Dim x(4 To 8) As Double Assignment: arrayname(index) = value e.g. HR(0) = 134 HR(5) = 151 x(5) = 23.87 x(7) = 189.2516 Heart Rate
8
Mark Dixon, SoCCE SOFT 131Page 8 Array Variables (why?) Dim Person(4) As String Person(0) = “Bob” Person(1) = “Sally” Person(2) = “Jo” Person(3) = “Fred” Person(4) = “Alison” Dim Name1 As String Dim Name2 As String Dim Name3 As String Dim Name4 As String Dim Name5 As String Name1 = “Bob” Name2 = “Sally” Name3 = “Jo” Name4 = “Fred” Name5 = “Alison” Single array declaration5 variable declarations
9
Mark Dixon, SoCCE SOFT 131Page 9 Array Variables (why?) Most powerful feature of arrays: –can use for loop to move through all array elements easily Arrays Dim x(3 To 6) As Integer x(3) = 12 x(4) = 12 x(5) = 12 x(6) = 12 Dim x(3 To 6) As Integer Dim i As Integer For i = 3 To 6 x(i) = 12 Next
10
Mark Dixon, SoCCE SOFT 131Page 10 Example 3: Heart Rate v1 Option Explicit Dim HR(0 To 6) As Long Private Sub Form_Load() HR(0) = 134 HR(1) = 127 HR(2) = 139 HR(3) = 155 HR(4) = 143 HR(5) = 151 HR(6) = 141 End Sub Private Sub btnDraw_Click() Dim i As Long picMain.Cls For i = 0 To 6 picMain.Line -(i * 500, HR(i) * 10) Next End Sub Heart Rate
11
Mark Dixon, SoCCE SOFT 131Page 11 Example 4 Confidential
12
Mark Dixon, SoCCE SOFT 131Page 12 List Box Control Used to give user multiple options (can change) Methods –AddItem: adds a new item to the list lstDrinks.AddItem "Coke" –RemoveItem: removes an existing item from the list lstDrinks.RemoveItem 2 –Clear: removes all existing items from the list lstDrinks.Clear Properties: –ListIndex: index of currently selected item, or -1 if nothing is selected –NewIndex: index of last item added –List: stores text of items in list –ItemData: stores (invisible) number for each item in list
13
Mark Dixon, SoCCE SOFT 131Page 13 Example 5: Drinks (Design) User Interface Functionality Trigger (when)Actions (what) load event of Form object Fill list with 8 drinks (as per following table), update list count. click event of Drinks list Display current drink's index (in index label), name (in name text box), & cost of drink (in cost text box). click event of Remove button Remove currently selected item from list, update list count. click event of Reset button Restore initial 8 drinks (as per following table), update list count. click event of Exit button End program, close window.
14
Mark Dixon, SoCCE SOFT 131Page 14 Example 5: Drinks (UI design) IndexText (List)Value (ItemData) 0Coke90 1Lemonade80 2Cider160 3Larger170 4Bitter180 5Whisky190 6Brandy170 7Vodka175 Only the text is displayed on the form.
15
Mark Dixon, SoCCE SOFT 131Page 15 Variable length arrays ReDim [Preserve] varname(subscripts) [As type] Option Explicit Dim Person() As String Sub Form_Load () ReDim Person(0) End Sub Sub cmdAdd_Click () Person(UBound(Person)) = txtName.Text txtName.Text = "" ReDim Preserve Person(UBound(Person) + 1) txtName.SetFocus End Sub Sub cmdShow_Click () Dim index As Integer picNames.Cls For index = 0 To UBound(Person) picNames.Print Person(index) Next index End Sub Sub cmdQuit_Click () End End Sub
16
Mark Dixon, SoCCE SOFT 131Page 16 Structures (what) multiple values (of different type) – stored in single variable each element identified by name the value of the firstname element is 'Bob' ElementValue SurnameSmith FirstnameBob GenderMale Age21
17
Mark Dixon, SoCCE SOFT 131Page 17 Structures (how) Declaration: Type typename elementname As type... End Type e.g. Type Student Surname As String FirstName As String Gender As Boolean End Type Assignment: varname.element = value e.g. Dim curStu As Student curStu.Surname = "Smith" curStu.Firstname = "Bob" Structures
18
Mark Dixon, SoCCE SOFT 131Page 18 Examples Type Student Surname As String FirstName As String Sex As Integer Course As String End Type Dim Candidate As Student Candidate.Surname = “Smith” Candidate.FirstName = “Bob” Candidate.Sex = 0 Candidate.Course = “Alcohol Studies BSc”
19
Mark Dixon, SoCCE SOFT 131Page 19 Remarks Dim counter As Integer' Creates a variable, called ‘counter’. For counter = 1 To 10' counter will start at 1 and go up to 10. picNumbers.Print counter' display the value of counter. Next' counter should take on its next value. Use carefully Relate to problem add something Don't explain how code works Dim counter As Integer ' Displays numbers 1 to 10. For counter = 1 To 10 picNumbers.Print counter Next
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.