Mark Dixon, SoCCE SOFT 131Page 1 10 – Variable Scope, and Arrays of Structures.

Slides:



Advertisements
Similar presentations
Mark Dixon, SoCCE SOFT 131Page 1 15 – Object Oriented Analysis, Design, and Programming.
Advertisements

Mark Dixon, SoCCE SOFT 131Page 1 22 – Object Oriented Analysis, Design, and Programming.
Mark Dixon Page 1 16 – Passing Data between pages: Forms, Sessions, & Query Strings.
Mark Dixon, School of Computing SOFT 120Page Object Associations (part 2): Inheritance and Polymorphism Priestley (2000) sections 6.5, 6.6, and 6.7.
Mark Dixon, SoCCE SOFT 131Page 1 09 – Procedures.
Mark Dixon Page 1 22 – Object Oriented Programming in ASP.
Mark Dixon Page 1 05 – Constants and Variables. Mark Dixon Page 2 Admin: Test (next week) In class test –teaching week 6 50 mins short answer (5 - 6 words.
Mark Dixon, SoCCE SOFT 136Page 1 11 – User Defined Functions.
Mark Dixon, SoCCE SOFT 131Page 1 13 – Object Oriented Analysis, Design, and Programming.
Mark Dixon, SoCCE SOFT 131Page 1 07 – Iterative Execution.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Expressions, Operators & Functions.
Mark Dixon, SoCCE SOFT 131Page 1 06 – Iteration. Mark Dixon, SoCCE SOFT 131Page 2 Session Aims & Objectives Aims –Revise Variables and Conditional execution.
Mark Dixon, SoCCE SOFT 131Page 1 10 – Enumerated Data Types & Procedure Parameters.
Mark Dixon, SoCCE SOFT 131Page 1 03 – Debugging & Graphics.
Mark Dixon, SoCCE SOFT 131Page 1 06 – Iteration. Mark Dixon, SoCCE SOFT 131Page 2 Session Aims & Objectives Aims –To introduce the main concepts involved.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Data-types, Variables, Operators & Functions.
Mark Dixon, SoCCE SOFT 131Page 1 07 – Constants, Arrays, & Structures.
Mark Dixon, SoCCE SOFT 131Page 1 19 – Procedure Parameters.
Mark Dixon Page 1 21 – Object Oriented Programming in ASP.
Mark Dixon, SoCCE SOFT 131Page 1 08 – Procedures.
Mark Dixon, SoCCE SOFT 131Page 1 02 – Software Development Lifecycle, & User Interface Design.
Mark Dixon, SoCCE SOFT 131Page 1 08 – Iterative Execution.
Mark Dixon, SoCCE SOFT 131Page 1 16 – Passing Data between pages: Sessions, Query Strings, & Self Posting.
Mark Dixon Page 1 02 – Queries: Query by Example.
Mark Dixon, SoCCE SOFT 131Page 1 10 – User Defined Functions.
Mark Dixon Page 1 13 – Object Oriented Programming.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Constants and Variables.
Mark Dixon, SoCCE SOFT 131Page 1 12 – Enumerated Data-Types & Pass-by-reference.
Mark Dixon, SoCCE SOFT 131Page 1 13 – Control Arrays & Container Controls.
Mark Dixon, SoCCE SOFT 136Page 1 06 – Information Processing: Data-types, Constants, Variables.
Mark Dixon Page 1 20 – Web applications: Writing data to Databases using ASP.
Mark Dixon, SoCCE SOFT 131Page 1 09 – User Defined Procedures: Scope, and Parameters.
Mark Dixon, SoCCE SOFT 131Page 1 11 – Arrays of Structures & Modules.
Mark Dixon, SoCCE SOFT 131Page 1 03 – Debugging, Sound, and Graphics.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Information Processing: Data-types, Variables, Operators & Functions.
Mark Dixon, SoCCE SOFT 131Page 1 20 – Web applications: Writing data to Databases using ASP.
Mark Dixon Page 1 10 – Iterative Execution. Mark Dixon Page 2 Questions: Variables Write a line of code to declare a variable called h Write a line of.
1 Subroutines and Functions Chapter 6 in Deitel, Deitel and Nieto.
Mark Dixon Page 1 23 – Web applications: Writing data to Databases using PhP.
Mark Dixon Page 1 24 – Object Oriented Programming in ASP.
Mark Dixon 1 07 – Variables. Mark Dixon 2 Questions: Conditional Execution What is the result of (txtFah.value is 50): (txtFah.value >= 40) What will.
Mark Dixon 1 22 – Web applications: Writing data to Databases using ASP.Net.
Mark Dixon Page 1 23 – Web applications: Writing data to Databases using ASP.
Mark Dixon Page 1 09 – Arrays. Mark Dixon Page 2 Questions: Loops What is the value of t, after this code executes? t = 0 For x = 4 To 6 t = t + x Next.
Mark Dixon, SoCCE SOFT 136Page 1 9 – Procedures. Mark Dixon, SoCCE SOFT 136Page 2 Session Aims & Objectives Aims –To introduce the main concepts involved.
Mark Dixon 1 11 – Array Variables. Mark Dixon 2 Questions: Loops What is the value of t, after this code executes? t = 0 For x = 4 To 6 t = t + x Next.
Mark Dixon 1 08 – Variables. Mark Dixon 2 Questions: Conditional Execution What is the result of (txtFah.value is 50): (txtFah.value >= 40) What will.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Variables. Mark Dixon, SoCCE SOFT 131Page 2 Admin: Test (next week) In class test –teaching week 6 –university week.
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.
Mark Dixon, SoCCE SOFT 131Page 1 23 – Object Associations.
Mark Dixon, SoCCE SOFT 131Page 1 24 – Datatypes and Object Association.
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Expressions, Operators & Functions.
Two Forms Please use speaker notes for additional information!
Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
Mark Dixon, SoCCE SOFT 131Page 1 18 – Enumerated Data Types and Arrays of Structures.
Mark Dixon Page 1 08 – Variables. Mark Dixon Page 2 Questions: Conditional Execution What is the result of (txtFah.value is 50): (txtFah.value >= 40)
Mark Dixon Page 1 08 – Variables. Mark Dixon Page 2 Questions: Conditional Execution What is the result of (txtFah.value is 50): (txtFah.value >= 40)
Mark Dixon SOFT 131Page 1 12 – Object Oriented Analysis, Design, and Programming.
Mark Dixon 1 22 – Object Oriented Programming. Mark Dixon 2 Questions: Databases How many primary keys? How many foreign keys? 3 2.
05/02/ Records. 205/02/2016 Learning Objectives State: The difference between records and arrays. The difference between records and arrays. How.
Mark Dixon, SoCCE SOFT 131Page 1 17 – Procedures.
Mark Dixon 1 13 – Parameters. Mark Dixon 2 Question: Arrays How many array variables are in the following code: Dim x Dim y Dim f(4) x = 12 y = 6 f(2)
Mark Dixon, SoCCE SOFT 131Page 1 25 – Soft 131 Examination Revision.
Mark Dixon 1 Soft051 Examination Sample Questions.
Mark Dixon Page 1 09 – Iterative Execution. Mark Dixon Page 2 Questions: Variables Write a line of VBScript code to declare a variable called h Write.
Lab 5 Arrays ► Lab 4 Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
Mark Dixon, SoCCE SOFT 136Page 1 08 – Arrays & Structures.
Department Array in Visual Basic
8 Records 25/07/2019.
Presentation transcript:

Mark Dixon, SoCCE SOFT 131Page 1 10 – Variable Scope, and Arrays of Structures

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

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)

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

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

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

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

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

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

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

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

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 Forenames: string Salaries: double

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

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

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

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

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"

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"

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 Salary: double Forenames: string each row is a TEmployee makes it easier to get details of single employee

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