Download presentation
Presentation is loading. Please wait.
1
Mark Dixon, SoCCE SOFT 131Page 1 10 – Variable Scope, and Arrays of Structures
2
Mark Dixon, SoCCE SOFT 131Page 2 Session Aims & Objectives Aims –To introduce the idea of variable scope –To introduce the idea of an array of structures Objectives, by end of this week’s sessions, you should be able to: –Determine whether a variable or procedure is in or out of scope at a given point in a piece of code –Appropriately Select a variable’s scope in their own program –create and use an array of structures
3
Mark Dixon, SoCCE SOFT 131Page 3 Scope (what) Scope – accessibility/visibility –Local (declared within procedure) –Form/module/unit (general declarations) Things that have scope: –Variables –Procedures –Functions (subject of future lecture)
4
Mark Dixon, SoCCE SOFT 131Page 4 Variable Scope (How) Module variables –general declarations (top) Local variables: –in procedures Option Explicit Dim mv as long Private Sub btnCalc_Click() Dim lv1 as long... End Sub Private Sub btnAdd_Click() Dim lv2 As Long... End Sub Scope animation
5
Mark Dixon, SoCCE SOFT 131Page 5 Scope (why) In short – Robustness of code/software –Protection from accidental outside interference One of many responses to code that is –Difficult to maintain, and –Unreliable –House of cards phenomenon Prevent: –Uncontrolled and ad hoc interactions between code Always define things at lowest level needed
6
Mark Dixon, SoCCE SOFT 131Page 6 Exercise: Variable Scope In the following: Option Explicit Private Sub btnCalc_Click() Dim x As Integer x = 0 lblTotal.Caption = "£" & x End Sub Private Sub btnQuit_Click() x = 0 lblTotal.Caption = "£" & x End Sub Variable not found error
7
Mark Dixon, SoCCE SOFT 131Page 7 Exercise: Variable Scope Will this compile? Option Explicit … Dim x As integer … Private Sub thing() Dim z As Integer x = 23 y = "there" z = 12 end Private Sub btnTest_Click() Dim y As String y = "hello" x = 67 z = 53 End Sub Is x in scope? Is y in scope? Is z in scope? Is y in scope? Is x in scope? Is z in scope? Yes No Yes No
8
Mark Dixon, SoCCE SOFT 131Page 8 Example: Counter Option Explicit Dim Counter As Long Private Sub Form_Load() Counter = 0 Me.lblCounter.Caption = Counter End Sub Private Sub btnReset_Click() Counter = 0 Me.lblCounter.Caption = Counter End Sub Private Sub btnUp_Click() Counter = Counter + 1 Me.lblCounter.Caption = Counter End Sub Private Sub btnDown_Click() Counter = Counter - 1 Me.lblCounter.Caption = Counter End Sub Counter
9
Mark Dixon, SoCCE SOFT 131Page 9 Example: Employee Data Need to keep a record of employee details –e.g. surname forenames date of birth address telephone number salary
10
Mark Dixon, SoCCE SOFT 131Page 10 Example: User Interface Must respond to following events: Click Previous button: move to previous employee’s details Click Next button: move to next employee’s details Type in fields: change current employee’s details
11
Mark Dixon, SoCCE SOFT 131Page 11 Example: Code Design 2 layers: Layer 1 Event Handler Procedures Layer 2 General Procedures btnPrevious Click btnNext Click Form Load Employee Display Employee Store
12
Mark Dixon, SoCCE SOFT 131Page 12 Example: Data Design We could use an array for each piece of employee information: Dim Surnames(1 To 10) As String Dim Forenames(1 To 10) As String Dim Salaries(1 To 10) As Double Surnames: string 5 10 1 Forenames: string 5 10 1 Salaries: double 5 10 1
13
Mark Dixon, SoCCE SOFT 131Page 13 Example: Employees v1 Option Explicit Dim Surnames(1 To 10) As String Dim Forenames(1 To 10) As String Dim Salaries(1 To 10) As Double Dim curEmp As Integer Sub EmpDisplay() lblEmpNum.Caption = curEmp txtSurname.Text = Surnames(curEmp) txtForenames.Text = Forenames(curEmp) txtSalary.Text = Salaries(curEmp) End Sub Sub EmpStore() Surnames(curEmp) = txtSurname.Text Forenames(curEmp) = txtForenames.Text Salaries(curEmp) = Val(txtSalary.Text) End Sub Private Sub Form_Load() curEmp = 1 EmpDisplay End Sub Private Sub btnNext_Click() EmpStore curEmp = curEmp + 1 EmpDisplay End Sub Employees v1
14
Mark Dixon, SoCCE SOFT 131Page 14 Difficulty This design works However, if –all fields were implemented, and –more complex operations were added the code would become difficult to manage –having several separate arrays Arrays allow data to be grouped –however, arrays must be homogenous (same data type) it would be useful to be able to group different (heterogeneous) types of data
15
Mark Dixon, SoCCE SOFT 131Page 15 Structures Groups different types of data Declaration of type: Type TAnimal Name As String Species As String Gender As Boolean End Type Use of type (in variable declaration): Dim myPet As TAnimal Change value of MyPet’s name: myPet.Name = "George" Pet animation
16
Mark Dixon, SoCCE SOFT 131Page 16 Array of Structures Can also have arrays of structures: Dim MyPets(1 To 5) As TAnimal Change value: MyPets(3).Name = "George" Change value using index variable: ind = 2 MyPets(ind).Name = "Fred" Pet animation
17
Mark Dixon, SoCCE SOFT 131Page 17 Exercise: Structures Create a record definition for: –Estate agents: House details (house num., street, price) Write code that will: –Create a variable of the above type –Put data into the elements of that variable Type THouse Num As Long Street As String Price As Double End Type Dim myHouse As THouse myHouse.Street = "Portland Square"
18
Mark Dixon, SoCCE SOFT 131Page 18 Exercise: Structures Create a record definition for: –Police stolen car register: Car details (Reg. number, colour, model) Write code that will: –Create a variable of the above type –Put data into the elements of that variable Type TCar RegNum As String Colour As String Model As String End Type Dim myCar As TCar myCar.RegNum = "GH23 XRB"
19
Mark Dixon, SoCCE SOFT 131Page 19 Example: Data Design We can now use a single array that uses a user defined type/record/structure: Surname: string Employees: TEmployee 5 10 1 Salary: double Forenames: string each row is a TEmployee makes it easier to get details of single employee
20
Mark Dixon, SoCCE SOFT 131Page 20 Example: Employees v2 Option Explicit Private Type TEmployee Surname As String Forenames As String Salary As Double End Type Dim Employees(1 To 10) As TEmployee Dim curEmp As Integer Sub EmpDisplay() lblEmpNum.Caption = curEmp txtSurname.Text = Employees(curEmp).Surname txtForenames.Text = Employees(curEmp).Forenames txtSalary.Text = Employees(curEmp).Salary End Sub Sub EmpStore() Employees(curEmp).Surname = txtSurname.Text Employees(curEmp).Forenames = txtForenames.Text Employees(curEmp).Salary = Val(txtSalary.Text) End Sub Private Sub Form_Load() curEmp = 1 EmpDisplay End Sub Private Sub btnNext_Click() EmpStore curEmp = curEmp + 1 EmpDisplay End Sub Employees v2
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.