Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Variables and Constants
Lecture Roger Sutton CO331 Visual Programming 12: One-dimensional Arrays 1.
OOP: Creating Object-Oriented Programs. VB & Object Oriented Programming Objects have properties, methods, and generate events Classes have been predefined.
VBA Modules, Functions, Variables, and Constants
Case, Arrays, and Structures. Summary Slide  Case Structure –Select Case - Numeric Value Example 1 –Select Case - String Value Example  Arrays –Declaring.
Chapter 8 Arrays and Collections Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Arrays-Part 1. Objectives Declare and initialize a one-dimensional array Store data in a one-dimensional array Display the contents of a one-dimensional.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Data Types and Operations Programming Fundamentals (Writing Code)Programming Fundamentals (Writing Code)
VB Code Statements 3 types of VB statement The Remark statement, known as comments, are used for project documentation only Begin with an apostrophe Not.
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.
Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same.
User-defined Data Types VB provides programmers with the ability to create user-defined types. User-defined Types are collections of related variables.
5.05 Apply Looping Structures
IE 212: Computational Methods for Industrial Engineering
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Chapter 8 Arrays Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
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.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
Chapter 8: Arrays.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
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.
Variables,Constants and Data types Variables temporarily stores values during the execution of an application Variables have a name and data type Declare.
‘Tirgul’ # 3 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #3.
8-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Arrays Chapter 8.
Arrays. Overview u General Discussion  Uses  Structure  Declaration u Searching u Control Arrays.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
JavaScript, Fourth Edition
6-1 Chapter 6 Working with Arrays in VB.NET. 6-2 Learning Objectives Understand the use of list and table arrays in VB.NET projects and the difference.
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 Chapter Arrays, Timers, and More 8.
Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Visual Basic CDA College Limassol Campus COM123 Visual Programming 1 Semester B Lecture:Pelekanou Olga Week 5: Useful Functions and Procedures.
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,
Arrays An array is a list or series of values all referenced by the same name Also referred to as a table An element is an individual item in the array.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Chapter81 For....Next Loops n For i = m To n statement (s) Next i n For statement designates a numeric variable, called control variable. n It is initialized.
Controlling Program Flow with Decision Structures.
Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
Arrays 1.
Programming Right from the Start with Visual Basic .NET 1/e
VBA - Excel VBA is Visual Basic for Applications
Chapter 7: User-Defined Functions II
2. Understanding VB Variables
7 Arrays.
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Arrays.
CIS16 Application Development and Programming using Visual Basic.net
7 Arrays.
Programming In Visual Basic .NET
Introduction to Computer Programming IT-104
Presentation transcript:

Arrays Array of Controls: several controls, of the same type (Class: a prototype for an object indicating the properties and methods), that have the same name Array of Variables: Static/Dynamic Arrays Single-Dimension/Multi-Dimensional Arrays

Control Arrays A group of controls that all have the same name (use with option buttons and check boxes) All controls in an array must be the same Class (a prototype for an object indicating the properties and methods) An advantage of using a Control Array, rather than independent controls, is that the controls share one Click Event (in the arrays Click Event, a Case Structure can be used to determine which option button or check box is selected)

A group of 5 option buttons, to allow the user to choose a colour After creating the 5 controls (in a Frame Group), set the Name Property of the first option button to optColour, furthermore, change the Name Property of the second option button to optColour: At this point a Message Box will appear, asking you if you want to create a Control Array Select ‘Yes’, and the name in the Properties Window becomes optColour(0) for the first control and optColour(1) for the second control The number inside the parenthesis is called an Index and is used to refer to the specific control within the Array; and all subsequent controls, given the same name, will automatically have an index attached In this example, the 5 option buttons are named optColour(0), optColour(1), optColour(2), optColour(3), optColour(4)

Private Sub optColour_Click(Index As Integer) ‘Set the colour to match the selected option button Select Case Index Case 0‘First button is selected Form1.BackColor = vbBlue Case 1‘Second button is selected Form1.BackColor = vbBlack Case 2‘Third button is selected Form1.BackColor = vbRed Case 3‘Fourth button is selected Form1.BackColor = vbWhite Case 4‘Fifth button is selected Form1.BackColor = vbYellow End Select End Sub

Variable Arrays Single-Dimension Arrays A Variable Array can contain a list of values, similar to a List Box or a Combo Box; (VB actually stores the List Property of a List Box or a Combo Box in an Array); therefore, think of an array as a list box without the box Any time a series of variables (multiple values) needs to be kept (stored) for later processing, such as re-ordering, calculating, or printing, an Array needs to be set up An Array is a series of individual variables, all referenced by the same name; sometimes Arrays are referred to as Tables or Subscripted Variables

The same notation is used with Variable Arrays as with Control Arrays, therefore, in an array for storing names, called stName, you may have stName(0), stName(1), stName(2), …. Each individual Variables is called an Element of the Array The individual Elements of an Array are treated the same as any other Variable and may be used in any assignment statement The Subscript (which may also be called an Index) inside the parenthesis, is the position of the Element within the Array Subscripts may be constants, variables, or numeric expressions; although the Subscripts must be Integers, VB will round any Non- Integer Subscript

To specify the number of Elements in an Array, you need to use the Dim statement Dim ArrayName( [LowerSubscript To] UpperSubscript) As DataType The Dim statement allocates storage for the specified number of Elements and initialises each numeric variable to zero (0); (in the case of String Arrays, each Element is set to an empty string [zero characters]) Furthermore, it is not necessary to specify the Lower Subscript value; if no value is set for the Lower Subscript, then the lowest Subscript is zero (0)

Dim stName(0 To 25) As String Dim cBalance(10) As Currency Dim iCollected(iCount) As Integer Any Array dimensioned in this way is referred to as a Static Array A Static Array may be dimensioned only once in a project Any attempt to change the size of a Static Array after its first use (project execution) causes the following error message: “Array Already Dimensioned”

Need to explicitly initialise the elements of the array with assignment statements Numbers(0) = 77 Numbers(1) = 68 etc Repetition statements can also be used (0) (1) (2) (3) (4) (5) Index Values Numbers Numbers(0) = 77 Numbers(1) = 68 Numbers(2) = 55 Numbers(3) = 72 Numbers(4) = 79 Numbers(5) = 89

Arrays Arrays occupy space in memory The compiler reserves the required amount of space in memory through the declaration statement Arrays can be declared Globally (Public) Module (Dim/Private) Local (Dim/Static)

Arrays The LBound function returns the lower bound (the lowest numbered index value) of the array The UBound function returns the upper bound (the highest numbered index value) of the array

Arrays Instead of having Array(0), there maybe a need to refer to the first element of an array as Array(1) This means changing the Lower Bound index value of the array from, the default, 0 to 1 Dim Array(0 TO 9) As Integer Dim Array(1 TO 10) As Integer An alternative solution is to use the Option Base statement Option Base sets the Lower Bound index value to 0 or 1 only and is placed in [General] [Declarations] This introduces the concept of Direct Reference for elements in an array

Option Base set in General Declarations can be used to set/change the lower bound to 0 or 1 ( no value other that these) Array(1) instead of Array(0) for the first element Option Base must be placed before the array declaration

For Each/Next Statements There is a need to have a method of reference to each Element in an Array A For/Next Loop can be used, however, another useful looping construct is the For Each/Next Loop; the significant advantage of using the For Each/Next Loop,is that the Subscripts of the Array do not have to be manipulated For Each ElementName In ArrayName Statement(s) in Loop Next ElementName

VB automatically references each Element of the Array, assigns its value to ElementName, and makes one pass through the loop If the Array has n Elements, the loop will execute n times The Variable used for ElementName must be a Variant Data Type For Each ElementName In ArrayName Statement(s) in Loop Next ElementName

With an array named stName, each element of the array is printed Dim vOneName As Variant For Each vOneName In stName Print vOneName‘Print one element of the array Next vOneName The For Each/Next loop will execute if the array has at least one element; All the statements within the body of the loop are executed for the each element in the array Furthermore, an Exit For Statement may be used within the loop to exit early, just as in a For/Next loop

Using a For…Next Loop Dim X As Integer For X = LBound(stName) To UBound(stName) Print stName(X) Next X

User-Defined Data Types Integer, String, Currency, etc…….. are what are known as VB data types You can define your own data types by combining multiple fields of data into a single unit; A User-Defined Data Type can be used to combine several fields of related information For example, a Product Data Type might contain a Product Description, a Product Number, Quantity, and Price The fields can be combined into a User-Defined Data Type by using the Type and End Type Statements

Type NameOfNewDataType List of Fields End Type Type Product stDescription As String stProductNumber As String iQuantity As Integer cPrice As Currency End Type Once you have created your own Data Type, you may use it to declare variables just as you would use any other Data Type

Type Statements can appear only at the Module Level in the General Declarations section of a Standard Code Module or a Form Module When Type Statements are placed in the Standard Code Module, they are Public by default; If Type Statements are placed at the Form/Module Level, they must be declared as Private Example of a Type Statement in a Form Module: Private Type …… ……….. End Type