Mark Dixon, SoCCE SOFT 131Page 1 07 – Constants, Arrays, & 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.
1 Visual Basic Programming II Lecture 3 MIS233 Instructor – Larry Langellier.
Mark Dixon Page 1 22 – Object Oriented Programming in ASP.
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 15 – Object Associations.
Lab4 ListBox Control Properties, Methods and Events.
CVEV 118/698 Visual Basic Lecture 1 Prof. Mounir Mabsout Expert 1: Elsa Sulukdjian Expert 2: Walid El Asmar.
VB Default Controls List Box, Combo Box
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 19 – Procedure Parameters.
Mark Dixon, SoCCE SOFT 131Page 1 16 – Object Associations.
Mark Dixon Page 1 21 – Object Oriented Programming in ASP.
Mark Dixon, SoCCE SOFT 131Page 1 10 – Variable Scope, and Arrays of Structures.
Mark Dixon, SoCCE SOFT 131Page 1 08 – Iterative Execution.
Mark Dixon, SoCCE SOFT 131Page 1 05 – Conditional Execution.
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 131Page 1 18 – Structured Query Language.
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 05 – Information Processing: Data-types, Variables, Operators & Functions.
VBA & Excel Barry L. Nelson IEMS 465 Fall Quarter 2003.
Chapter 8 Using Repetition with Loops and Lists. Class 8: Loops and Lists Write Do loops to execute statements repeatedly Write For loops to execute statements.
Adding Controls to User Forms. Adding Controls A user form isn’t much use without some controls We’re going to add controls and write code for them Note.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
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.
Ch 11: Userforms CP212 Winter Topics Designing User Forms o Controls Setting Properties o Tab Order o Testing Writing Event Handlers o Userform_Initialize.
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.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
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 Page 1 10 – Array Variables. Mark Dixon Page 2 C1 – In-class test Results mixed: –quite a few fails 34% (13/38) –some very good (91% max) ask.
© 1999, by Que Education and Training, Appendix A, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
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.
Tutorial 6 The Repetition Structure
Arrays. Overview u General Discussion  Uses  Structure  Declaration u Searching u Control Arrays.
Mark Dixon, SoCCE SOFT 131Page 1 23 – Object Associations.
Mark Dixon, SoCCE SOFT 131Page 1 24 – Datatypes and Object Association.
Applications Development
Mark Dixon, SoCCE SOFT 131Page 1 04 – Information Processing: Expressions, Operators & Functions.
Chapter 7 P 1 Lists and Loops List boxes and combo boxes List box - list of items - useful for a list which does not change Combo box - list of items -
Two Forms Please use speaker notes for additional information!
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
Mark Dixon, SoCCE SOFT 131Page 1 18 – Enumerated Data Types and Arrays of Structures.
VAT Calculator program Controls Properties Code Results.
Programming with Microsoft Visual Basic th Edition
CN2180 Chapter 4 Kemtis Kunanuraksapong MSIS with Distinction, A+ MCTS, MCDST, MCP Kemtis Kunanuraksapong MSIS with Distinction, A+ MCTS, MCDST, MCP.
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.
Understanding Visual Basic Fundamentals CHAPTER 13 Understanding Visual Basic Fundamentals.
1 Dynamic Arrays ListBoxes, Dynamic Arrays, Dynamic Control Arrays ListBoxes are on pp and dynamic arrays are in Chapter 7 of Deitel, Deitel and.
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.
Using Forms and Form Elements In Visual Basic.NET.
Mark Dixon, SoCCE SOFT 136Page 1 08 – Arrays & Structures.
Visual Basic Fundamental Concepts
2. Understanding VB Variables
Visual Basic..
To understand what arrays are and how to use them
8 Records 25/07/2019.
Presentation transcript:

Mark Dixon, SoCCE SOFT 131Page 1 07 – Constants, Arrays, & Structures

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

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

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 * * radius area = * (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

Mark Dixon, SoCCE SOFT 131Page 5 Example 2: CirCalc v2 Private Sub Form_Load() … Private Sub btnOK_Click() Const PI = 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

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

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) = x(7) = Heart Rate

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

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

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

Mark Dixon, SoCCE SOFT 131Page 11 Example 4 Confidential

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

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.

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.

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

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

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

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”

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