Title: 07
107 Constants, Arrays, Structures
2Session Aims Objectives
- Aims
- To introduce the main concepts involved in
handling more complex (multi valued) data - Objectives,by end of this weeks sessions, you
should be able to - declare, use, and assign values to, constants,
arrays, and structures - create and use ListBoxes
3Constants
- similar to variable
- value given in declaration
- value cant be changed
- useful for removing 'magic numbers'
- declaration syntax
- name used to represent literal value
Global Const constantname expression
4Example 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
txtRadius
btnOK
picCircle
picInfo
5Example CirCalc v2
CirCalc
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
txtRadius
btnOK
picCircle
picInfo
6Array Variables (what)
- multiple values (of same type) stored in single
variable - index identified individual values (called
elements) - the value of element 3 is 155
Index Value
0 134
1 127
2 139
3 155
4 143
5 151
6 141
7Array Variables (how)
- Declaration Dim varname(first To
last)As typee.g. Dim HR(0 To 6) As
Long Dim x(4 To 8) As Double - Assignment arrayname(index) valuee.g.
HR(0) 134 HR(5) 151 x(5) 23.87
x(7) 189.2516
Heart Rate
8Array Variables (why?)
Single array declaration
5 variable declarations
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
Dim Person(4) As String Person(0)
Bob Person(1) Sally Person(2)
Jo Person(3) Fred Person(4) Alison
9Array Variables (why?)
- Most powerful feature of arrays
- can use for loop to move through allarray
elements easily
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
Arrays
10Example 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
11Example
Confidential
12List 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
13Example 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.
14Example Drinks (UI design)
Index Text (List) Value(ItemData)
0 Coke 90
1 Lemonade 80
2 Cider 160
3 Larger 170
4 Bitter 180
5 Whisky 190
6 Brandy 170
7 Vodka 175
Only the text is displayed on the form.
15Variable 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
16Structures (what)
- multiple values (of different type) stored in
single variable - each element identified by name
- the value of the firstname element is 'Bob'
Element Value
Surname Smith
Firstname Bob
Gender Male
Age 21
17Structures (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 valuee.g.
Dim curStu As Student curStu.Surname
"Smith" curStu.Firstname "Bob"
Structures
18Examples
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
19Remarks
- 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
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.