© 1999, by Que Education and Training, Chapter 8, pages 393-429 of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.

Slides:



Advertisements
Similar presentations
Modeling using VBA. Covered materials -Userforms -Controls -Module -Procedures & Functions -Variables -Scope.
Advertisements

1 Visual Basic Programming II Lecture 3 MIS233 Instructor – Larry Langellier.
 “Regular” variable ◦ Holds a single value ◦ For example Dim Student1 as String Dim Student2 as String Dim Student3 as String … Dim Studentn as String.
Chapter 9. 2 Objectives You should be able to describe: Addresses and Pointers Array Names as Pointers Pointer Arithmetic Passing Addresses Common Programming.
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.
An Introduction to Programming with C++ Fifth Edition
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.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 8 Arrays.
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.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
C++ for Engineers and Scientists Third Edition
Chapter 7: Working with Arrays
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.
VB .NET Programming Fundamentals
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Processing Arrays Lesson 8 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
IE 212: Computational Methods for Industrial Engineering
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
Using Arrays and File Handling
Chapter 17: Arrays Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
A First Book of ANSI C Fourth Edition
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
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.
© 2012 EMC Publishing, LLC Slide 1 Chapter 8 Arrays  Can store many of the same type of data together.  Allows a collection of related values to be stored.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Arrays Group of variables that have a similar type
Chapter Six: Working With Arrays in Visual Basic.
‘Tirgul’ # 3 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #3.
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.
1 Working with Data Structures Kashef Mughal. 2 Chapter 5  Please review on your own  A few terms .NET Framework - programming model  CLR (Common.
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.
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.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
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.
Visual Basic Programming I 56:150 Information System Design.
Chapter 9 Processing Lists with Arrays. Class 9: Arrays Understand the concept of random numbers and how to generate random numbers Describe the similarities.
CHAPTER 9 PART II. MULTIDIMENSIONAL ARRAYS Used to represent tables of values arranged in rows and columns. Table element requires two indexes: row and.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Processing Arrays Lesson 9 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
1 Chapter 7 Arrays. 2 Outline and Objective In this chapter we will Learn about arrays One-dimensional arrays Two-dimensional arrays Learn about searching.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
A FIRST BOOK OF C++ CHAPTER 8 ARRAYS AND POINTERS.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Lab 5 Arrays ► Lab 4 Exercise Review ► Array Concept ► Why Arrays? ► Array Declaration ► An Example of Array ► Exercise.
Objectives You should be able to describe: One-Dimensional Arrays
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Chapter 11 - JavaScript: Arrays
VBA - Excel VBA is Visual Basic for Applications
Chapter 7 Arrays.
Arrays.
Objectives You should be able to describe: Addresses and Pointers
CIS16 Application Development and Programming using Visual Basic.net
Introduction to Computer Programming IT-104
Presentation transcript:

© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Processing Related Data n If the Toy number is given, a long, complex decision can be used to assign Toy name: If ToyID = “1” Then Toy = “Bean bag toys” ElseIf ToyID = “2” Then Toy = “Kids’ books” : ElseIf ToyID = “14” Then Toy = “Footballs” Else Toy = “Basketballs” End If

© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Processing Related Data n For a small number of items, using a nested If or Select Case to determine the desired toy given the Toy ID may not seem too bad. n What happens if we have 100 or 1000 or toys? n A case for arrays: i Data for arrays is typically entered by the user or is already stored in an external file or database. i Once the data is loaded into the Toy() array, the toy name can be retrieved through a single statement (assume ToyID has been set to the correct number):SelectedToy = Toy (ToyId)

© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Array Definitions n A group of related, contiguous memory locations with the same name and type. i Also called tables or subscripted variables n Subscript i Used to reference a specific array element by specifying the amount of offset (from the first element) i Appear between parentheses, e.g. 12 in Qz(12) i Like ListIndex property of list controls n Element i Refers to a single element of the array at the specified subscript location

© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Declarations: Static Arrays n Declaring one-dimensional, static arrays i {Private | Dim} MyArray(Lsub To Usub) As Type u If Lsub is not specified, it is assumed to be zero u Describes a column of data at locations Lsub to Usub

© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Manually Populating the Toy Array Private fToy(1 To 15) As String Private Sub Form_Load() fToy(1) = “Bean bag toys” fToy(2) = “Kids’ books” fToy(3) = “Kids’ software” fToy(4) = “Bicycles” fToy(5) = “Baseballs” fToy(6) = “Mitts” fToy(7) = “Bats” fToy(8) = “Board games” fToy(9) = “Water toys” fToy(10) = “Dress-up gear” fToy(11) = “Crayons” fToy(12) = “Paper supplies” fToy(13) = “Virtual pets” fToy(14) = “Footballs” fToy(15) = “Basketballs” End Sub

© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Dynamic Arrays n Declaring static arrays reserves a fixed number of memory locations, which may be much more or much less than what you typically need. n Dynamic arrays can expand and shrink as needed during execution. n Use UBound and LBound to keep track of the size of the array at any point during execution.

© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Working with Dynamic Arrays n Declaration {Private | Dim} MyArray() As Type n Initialization Private Sub Form_Load ReDim MyArray(0 To 0) ‘ unused array element End Sub n Addition LastSub = UBound(MyArray) + 1 ReDim Preserve MyArray(0 To LastSub) MyArray(LastSub) = NewValue n Deletion If UBound(MyArray) > 0 Then LastSub = UBound(MyArray) - 1 ReDim Preserve MyArray(0 To LastSub) Else Call MsgBox(“There is nothing to delete -- array is empty.”) End If

© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Avoiding Subscript Errors n An attempt to use a nonexistent subscript results in a subscript out of range error. n Example: Assume that the array Test() has elements at subscripts 1 through 5 i A reference to Test(0), Test(6), Test(x), where x is 10, all result in a subscript out of range error n UBound function for above Test array i UBound(Test) returns 5 n LBound function for above Test array i LBound(Test) returns 1

© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach For Next Loop to Process Array n Have index vary through all subscripts to do any of the following: i Input values into each array element i Display the content of each array element i Assign each array element a fixed value i Assign each array element a value that varies by its index ‘ Use UBound & LBound to prevent errors: FirstSub = LBound(MyArray) LastSub = UBound(MyArray) ‘ Process all array elements: For Index = FirstSub To LastSub Step 1 steps that access MyArray(Index) Next Index steps to process after loop ends False True Steps to process after loop ends Steps that access MyArray(Index) Index > LastSub? Set Index = FirstSub Index = Index + 1

© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach For Each/Next Statement n Like For/Next loop, but you don’t have to write code to go through all the subscripts False True Steps to process after loop ends Steps that access MyArray via ElemName Process all elements? ‘ Declare element index Dim ElemName As Variant ‘ Process all array elements: For Each ElemName In MyArray steps that access ElemName Next ElemName steps to process after loop ends

© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Multi-dimensional Static Arrays n A group of related, contiguous memory locations with the same name and type with multiple subscripts required to reference a single cell. n Two-dimensional array has a dimensionality of 2 i Two subscripts are required to reference a specific element n Example {Private | Dim} MyArray(L1 To U1, L2 To U2) As Type i If L1 or L2 are not specified, they are assumed to be zero i Dim StQuizzes(1 To 95, 1 To 10) As Single u The 95 rows represents 95 students u The 10 columns represent 10 different quizzes u StQuizzes(25,7) represents the 25th student’s 7th quiz grade n Three-dimensional array: rows, columns & planes i Dimensionality of 3 requires 3 subscripts for single element

© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Two-dimensional Array Dim SeatChart(2,3) As String allocates a 3 x 4 grid of string elements named SeatChart

© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach User-defined Data Types n Every element in a multi-dimensional array must be the same type. n The case for user-defined types i Allow storage of multiple sets of related information of different types as a single unit i Must be in the General Declarations section of code window i Can be procedure/function argument as long as passed ByRef i Cannot use For Each with arrays of a user-defined type n Type statement & related declaration {Public|Private} Type MyType FieldName1 As Type FieldName2 As Type : End Type Dim VarName As MyType ‘ simple variable of MyType Dim MyArray() As MyType ‘ dynamic array of MyType

© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach User-defined Data Type Example n User-defined type for storing student data Private Type SinfoType Name As String Class As Integer Quiz(1 To 10) As Single Grade As String End Type Dim Sinfo(1 To 100) As SinfoType ‘ 1-column table n Assigning values to elements of user-defined type array Sinfo(5).Name = “John Doe” Sinfo(5).Class = 5 Sinfo(5).Quiz(3) = 7.5 Sinfo(5).Grade = “C” Sinfo(100) = Sinfo(5) ‘ copies John Doe’s info to 100th cell

© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Array LookUp (Sequential Search) n Use a loop to vary through all array elements in sequence until a match is found or the last element is reached. False MyArray(Index) = SearchVal Or Index >= EndSub Set Index = StartSub True Found Actions Index = Index + 1 True MyArray(Index) = SearchVal False Not Found Actions

© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Array Example - Toy Store

© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Toy Store Example: Form-scope Declarations Option Explicit Private Type ToyType ID As Byte Name As String Cost As Currency Qty As Integer End Type Private fToy(1 To 15) As ToyType Private fSearchToyID As Byte Private fTotalToys As Integer Private fTotalPurchase As Currency

© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Toy Store Example: Form Load Private Sub Form_Load() Dim Index As Integer fToy(1).Name = "Bean bag toys" fToy(1).Cost = 5.95 ‘ replace with manual assignments to elements 2-14 fToy(15).Name = "Basketballs" fToy(15).Cost = For Index = 1 To 15 Step 1 fToy(Index).ID = Index fToy(Index).Qty = 25 Next Index fTotalToys = 0 fTotalPurchase = 0 End Sub

© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Toy Store Example: Lookup ID Private Sub cmdLookUp_Click() Dim Index As Byte If ToyIDValid Then fSearchToyID = CByte(txtToyID.Text) Index = 1 Do Until fToy(Index).ID = fSearchToyID Index = Index + 1 Loop If fToy(Index).ID = fSearchToyID Then ‘ do Found processing lblToyName.Caption = fToy(Index).Name If fToy(Index).Qty = 0 Then Call MsgBox("No " & fToy(Index).Name & " in stock.", _ vbOKOnly + vbInformation) cmdAdd.Enabled = False Else cmdAdd.Enabled = True End If Else ‘ do Not Found processing Call MsgBox("No such Toy ID.", vbOKOnly + vbInformation) cmdAdd.Enabled = False End If End Sub

© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach Toy Store Example: Add to Cart Private Sub cmdAdd_Click() Dim Qty As Integer If QtyValid Then Qty = CInt(txtQty.Text) fTotalToys = fTotalToys + Qty fToy(fSearchToyID).Qty = fToy(fSearchToyID).Qty - Qty fTotalPurchase = fTotalPurchase + fToy(fSearchToyID).Cost * Qty cmdShow.Enabled = True End If End Sub