Arrays in Visual Basic Week 9 CM30104-1. What is an array ? An array is a data structure that enables us to store a list of values that can be thought.

Slides:



Advertisements
Similar presentations
Lists, Loops, Validation, and More
Advertisements

Lecture Roger Sutton CO331 Visual Programming 13: Multi-dimensional Arrays 1.
VB PROJECT “PROJECT SAMPLES”. For Next Loops Design a VB program that displays in a picture box the first N multiples of an input integer Input 3 exam.
Programming with Microsoft Visual Basic 2005, Third Edition
Introduction to Arrays Chapter 7 Why use arrays? To store & represent lists of homogeneous values To simplify program code To eliminate the need to reread.
Mark Dixon, SoCCE SOFT 131Page 1 07 – Constants, Arrays, & Structures.
Arrays.
Developing Software Applications Introduction to Programming Fundamentals Scoping in VB Simple Ifs in VB.
Chapter 7 - Visual Basic Schneider1 Chapter 7 Arrays.
Chapter 7 - Visual Basic Schneider1 Chapter 7 Arrays.
Lec2 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 2 Back to Index v Basic Data Types v Arithmetic.
To type the VB code behind the command button (named cmdPush), Double-Click on the Push Me (caption) command button As a result the Visual Basic Code Window.
Lec4 P 1 CP2030 Visual Basic For C++ Programmers Copyright © University of Wolverhampton CP2030 VBFC Lecture 4 Back To Index v Iteration - Looping v Arrays.
Control Arrays, Records, and Record Arrays in V.B. Week 10.
3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 2.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
08/09/ Arrays Defining, Declaring & Processing.
University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 1 Lecture Outline Variable Scope Calling another subprogram Programming.
Array Processing Simple Program Design Third Edition A Step-by-Step Approach 7.
Chapter 7 Code Tables. VB Code Box 7-1 Event Procedure for Compute Button Private Sub hsbExemptions_Change() txtExemptions.Text =Str(hsbExemptions.Value)
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
Chapter 4: The Selection Process in Visual Basic.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
MAT Meyer Week 2 Programming VB: ‘basics’ Review & preview: Events, variables, statements, etc. Images, Control arrays, For/Next Assignment: read.
For Loops (ProjFor1, ProjFor2, ProjFor3, ProjFor4, textbox, textbox1) Please use speaker notes for additional information!
Chapter 9 - VB.Net by Schneider1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes, Combo Boxes, and the File-Opening Control The List Box Control.
VBA Lab 2 I ns.Samia Al-blwi. Visual Basic Grammar Object: Visual Basic is an object-oriented language. This means that all the items in Excel are thought.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
19/10/20151 Data Structures Arrays. 219/10/2015 Learning Objectives Explain initialising arrays and reading data into arrays. Design and write routine/s.
VB Core II Conditional statements Exception handling Loops Arrays Debugging.
Chapter Six: Working With Arrays in Visual Basic.
Arrays Code: Arrays Controls: Control Arrays, PictureBox, Timer.
© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
MS Visual Basic Applications Walter Milner. Event-driven programming Standard approach for GUIs Contrast with old character interfaces – program determines.
Arrays Chapter 8. Chapter 8 - Part 12 Variable and Variable Array Variable Stores only one value Variable Array Variable that has 1 symbolic name but.
University of Sunderland CIF 102/FIF102 Fundamentals of DatabasesUnit 15 Programming in Microsoft Access using VBA Using VBA to add functionality.
‘Tirgul’ # 3 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #3.
Random Files Please see speaker notes for additional information!
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Class Average Application Introducing the Do...Loop While and Do...Loop Until.
Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)
Developing Software Applications Iteration in Visual Basic (Loops)
Overview of VBA Programming & Syntax. Programming With Objects u Objects –Properties: attributes or characteristics of an object (e.g., font size, color,
Practical Programming COMP153-08S Week 5 Lecture 1: Screen Design Subroutines and Functions.
Visual Basic Programming Introduction VB is one of the High level language VB has evolved from the BASIC language. BASIC stands for Beginners All-purpose.
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
Select (drop-down list) Inputs. Insert/Form/List Menu.
Lab 6 (2) Arrays ► Lab 5 (1) Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
1 Scripting Languages VBScript - Recognized mainly by Internet Explorer only - Netscape does have a plug-in JavaScript - Recognized by Internet Explorer.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More About Array Processing 8.2 There Are Many Uses of Arrays and Many Programming.
Maximum Profit Please use speaker notes for additional information!
CECS 5020 Computers in Education Visual Basic Variables and Constants.
Chapter 8 P 1 Arrays and Grids Single-dimension arrays Definition An array is a sequence of elements all referred to with a common name. Other terms: table,
Slide 1 Controls v Control naming convention –Label: lblName –Command Button: cmdName –Text Box: txtName.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
Using a Database Access97 Please use speaker notes for additional information!
Knowledge Base. Defining a Variable Dim statement Dim intXX As Integer Public in a Module Public dblNN As Double.
1 Dynamic Arrays ListBoxes, Dynamic Arrays, Dynamic Control Arrays ListBoxes are on pp and dynamic arrays are in Chapter 7 of Deitel, Deitel and.
Using Forms and Form Elements In Visual Basic.NET.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Lab 5 Arrays ► Lab 4 Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
Selection Using IF THEN ELSE CASE Introducing Loops.
VBA - Excel VBA is Visual Basic for Applications
Department Array in Visual Basic
GCSE Computing:: Iteration (For Loops)
8 Records 25/07/2019.
Introduction to Computer Programming IT-104
Presentation transcript:

Arrays in Visual Basic Week 9 CM

What is an array ? An array is a data structure that enables us to store a list of values that can be thought of as a table Enables us to reference several data values by one variable name

Types of Arrays There are 2 types of arrays in VB : Data Arrays Control Arrays

Two types of Arrays in VB Data Arrays Primarily used to store data which is related in some way and of the same type, e.g:  student grades for a class of twenty students  weather details for a series of places. Control Arrays A mechanism for duplicating controls and allowing the same event coding to be triggered by an action to any of the elements of the control array.

Data arrays ~ example iNos iNos (3) Array name Array index Array elements Coding to reference each element

Array Declaration Arrays are declared in the same way as for variables. Dim iNos (1 To 5) As Integer or : Dim iNos (5) As Integer When no start number specified array index begins at 0

An example - Finding largest of a sequence of numbers Using several variables … iNum1, iNum2, iNum3 etc and a complicated Nested If …………. If (iNum1 > iNum2) And (iNum1 > iNum2) And … … And (iNum1 > iNum5) Then iLargest = iNum1 Else If (iNum2 > iNum1) And (iNum2 > iNum3) …… …….. etc

An example - Finding largest of a sequence of numbers Using a For loop and one variable, iNum For iLoopCount = 1 To 5 iNum = InputBox (“Enter next number”) If iNum > iLargest Then iLargest = iNum End If Next iLoopCount Give iLargest an initial very small value iLargest = 0

Finding largest of a sequence of numbers - using an Array Dim iNos (1 To 5) as Integer iLargest = 0 For iCount = 1 To 5 iNos (iCount) = InputBox (“Enter next number”) If iNos (iCount) > iLargest Then iLargest = iNos (iCount) End If Next iCount iNos iLargest 132

Finding largest of a sequence of marks & also storing names associated with each mark iMarks(1 To 5) sNames(1 To 5) Fred Sue Bill Amy Jonathon iHighestMark 98 sBestStudent Sue

Finding largest of a sequence of marks & also storing names associated with each mark iMarks(1 To 5)sNames(1 To 5) Fred Sue Bill Amy Jonathon Fill the arrays first … For iLoop = 1 To 5 sNames(iLoop) = …. iMarks(iLoop) = …. Next iLoop then have other loops to process the data in the arrays

Arrays have many advantages – e.g, we can then sort & list students in order Sue Amy Fred Jonathon Bill Fred Sue Bill Amy Jonathon ORDER OF INPUTARRAYS AFTER SORTING

Example coding ‘fill arrays For iCount = 1 to 5 sNames(iCount) = InputBox(“Name”) iMarks(iCount) = InputBox(“Mark”) Next iCount ‘display to listboxes For iCount = 1 to 5 lstNames.AddItem sNames(iCount) lstMarks.AddItem iMarks(iCount) Next iCount Fred Sue Bill Amy Jonathon

Payroll example Min = 0; Max = 9Vertical Scroll Bar scrEmployee Caption = “Add Employee” Command Button cmdEmployee Caption = “”Label lblPayroll Text = “”Text Box txtSalary Text = “”Text Box txtName PropertiesControl Name Using a form like this to: Input name & salary Store data in an array Scroll through previous data entered

Payroll example Scroll bar set at design stage to min = 0 max = 9 Data stored in 2 arrays: sName (10) As String sSalary (10) As Currency

Payroll example Defining the variables: Private sName (10) As String Private cSalary (10) As Currency Private ID As Integer Private cTotalPayroll As Currency

Program code Private Sub cmdEmployee_Click() ID = scrEmployee.Value ‘Set array index ‘depending on scroll bar sName(ID) = txtName.Text ‘Enter data from text cSalary(ID) = txtSalary.Text ‘boxes into arrays cTotalPayroll = cTotalPayroll + cSalary(ID) ‘Add to total pay scrEmployee.Value = scrEmployee.Value + 1 ‘Move scroll bar ‘on 1 position End Sub Private sName (10) As String Private cSalary (10) As Currency Private ID As Integer Private cTotalPayroll As Currency

Program code Private sName (10) As String Private cSalary (10) As Currency Private ID As Integer Private cTotalPayroll As Currency When the scroll bar is moved : Private Sub scrEmployee_Change() ID = scrEmployee.Value ‘Set array index depending ‘on scroll bar ‘Display name & salary from ‘appropriate position of arrays txtName.Text = sNames(ID) txtSalary.Text = Format(cSalary(ID), "Currency") End Sub

Arrays can have more than 1 dimension Occasionally information can often be presented more clearly by using arrays with more than one dimension Col Row

Arrays can have more than 1 dimension E.g Dim iAllMarks (1 To 5, 1 To 4) As Integer columns rows iAllMarks (2, 4) = 41

Accessing 2-D arrays Usually done using two nested loops For col 1 to 5 For row 1 to 3 Store input in cell (col,row) Next row Next col For row 1 to 3 For col 1 to 5 Store input in cell (col,row) Next col Next row

Single line or column? By keeping the column number the same and varying row – access a single column e.g.txtOutput.Text = marks(3,row) [in loop] By keeping the row number the same and varying col – access a single row e.g.txtOutput.Text = marks(col,1) [in loop]

Array example - sorting In this example a simple set of inputs are set up. Clicking the top button allows data entered to be stored in the array The middle one sorts the data The bottom button puts sorted data in the text boxes

Set Global variables and Initialise data Const cmin = 0 Const cmax = 4 ‘declare data array’ Private iNumbers(cmin To cmax) As Integer Sub Form_Load () Dim i As Integer ‘initialise array elements to zero For i = cmin To cmax iNumbers(i) = 0 Next i ‘initialise text boxes text1 = iNumbers(0) text2 = iNumbers(1) text3 = iNumbers(2) text4 = iNumbers(3) text5 = iNumbers(4) End Sub

Store Numbers Sub cmdAssign_Click () If (text1.Text = "") Or (text2.Text = "") Or (text3.Text = "") Or (text4.Text = "") Or (text5.Text = "") Then Beep MsgBox ("a zero length string is present") Else ‘store data from textboxes into array iNumbers(0) = CInt(text1.Text) iNumbers(1) = CInt(text2.Text) iNumbers(2) = CInt(text3.Text) iNumbers(3) = CInt(text4.Text) iNumbers(4) = CInt(text5.Text) End If End Sub

Sort Numbers Sub cmdRearrange_Click () Dim i As Integer Dim iPass As Integer Dim iTemp As Integer Dim iNoSwitches As Integer iPass = 0 Do iPass = iPass + 1 iNoSwitches = 1 For i = cmin To (cmax - iPass) If iNumbers(i) > iNumbers(i + 1) Then iNoSwitches = 0 iTemp = iNumbers(i) iNumbers(i) = iNumbers(i + 1) iNumbers(i + 1) = iTemp End If Next i Loop Until NoSwitches = 1 End Sub

Redisplay numbers Sub cmdRetrieve_Click () label1.Caption = iNumbers(0) label2.Caption = iNumbers(1) label3.Caption = iNumbers(2) label4.Caption = iNumbers(3) label5.Caption = iNumbers(4) End Sub

Summing up Simple data arrays can be thought of as tables of data Arrays enable us to reference several data items using one variable name They can be 2-dimensional (or 3, or 4 …) They are almost always processed using loops