CIS16 Application Development and Programming using Visual Basic.net

Slides:



Advertisements
Similar presentations
1.
Advertisements

Arrays.
Microsoft Visual Basic 2010: Reloaded Fourth Edition
Programming with Microsoft Visual Basic 2005, Third Edition
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.
Introduction to Programming with C++ Fourth Edition
Chapter 8 Arrays and Strings
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.
Java Unit 9: Arrays Declaring and Processing Arrays.
IE 212: Computational Methods for Industrial Engineering
1 Microsoft Visual Basic 2010 Arrays. 2 Using a One-Dimensional Array Lesson A Objectives After completing this lesson, you will be able to:  Declare.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Chapter 8 Arrays and Strings
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
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.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Chapter 8: Arrays.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
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.
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.
Chapter 8: Arrays Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 8 Arrays.
Chapter 9 Processing Lists with Arrays. Class 9: Arrays Understand the concept of random numbers and how to generate random numbers Describe the similarities.
Advanced Repetition Structure and String Functions (Unit 10) Visual Basic for Applications.
Programming with Microsoft Visual Basic th Edition
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
An Introduction to Programming with C++ Sixth Edition Chapter 12 Two-Dimensional Arrays.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 19 A Ray of Sunshine.
Arrays 1.
7.1 Introduction Arrays Arrays are data structures consisting of data items of the same type “Static” entities They remain the same size once they are.
Arrays Chapter 7.
Programming Right from the Start with Visual Basic .NET 1/e
Chapter 11 - JavaScript: Arrays
An Introduction to Programming with C++ Sixth Edition
Chapter 7: Working with Arrays
Computer Programming BCT 1113
Microsoft Visual Basic 2005: Reloaded Second Edition
IS 350 Arrays.
Visual Basic 2010 How to Program
Chapter 7 Arrays.
2. Understanding VB Variables
JavaScript: Functions.
Visual Basic .NET BASICS
VBScript Session 2 Dani Vainstein.
7 Arrays.
CIS16 Application Development and Programming using Visual Basic.net
CIS16 Application Development Programming with Visual Basic
Object Oriented Programming in java
Chapter 8 Data Structures: Cell Arrays and Structures
CIS 16 Application Development Programming with Visual Basic
Starting Out with Programming Logic & Design
Tutorial 11 Arrays Tutorial 11: Arrays.
Arrays.
Data Structures (CS212D) Week # 2: Arrays.
Arrays Week 2.
VBScript Session 2 Dani Vainstein.
CIS16 Application Development and Programming using Visual Basic.net
Arrays Part 2.
Programming Logic and Design Fifth Edition, Comprehensive
Chapter 8 Data Structures: Cell Arrays and Structures, Sorting
Presentation transcript:

CIS16 Application Development and Programming using Visual Basic.net Chapter Eight Arrays

Arrays

Variables Why use an array Simple variable (or scalar variable): a variable that is unrelated to any other variable in memory Array A group of variables with the same name and data type that are related in some way Why use an array temporarily store related data in memory Increases the efficiency of a program

Arrays Variables in an array are used just like any other variables assign values to them Use them in calculations display their contents The most commonly used arrays in business applications are: one-dimensional like a rows of seats two-dimensional like many rows of seats

ONE-Dimensional Arrays

One-Dimensional Arrays The variables (elements) in an array are stored in consecutive locations in the computer’s main memory. You distinguish one variable in a one-dimensional array from another variable in the same array by using a unique number, called a subscript. The subscript: is always an integer indicates the variable’s position in the array is zero based The first variable in a one-dimensional array is assigned a subscript of 0, the second a subscript of 1, and so on Is specified in a set of parentheses immediately following the array name

One-Dimensional Arrays Declaring a One-Dimensional Array Use either {Dim | Private | Static} keywords Depends on whether you are creating a procedure-level array or a class-level array Must specify the data type, name, and highest subscript to be used Two syntax versions

One-Dimensional Arrays – syntax version 1 {Dim | Private | Static} arrrayName(highestSubscript) As data Type highestSubscript is an integer that specifies the highest subscript in the array. The first element in a one-dimensional array has a subscript of 0 and the array will contain one element more than the number specified in the highestSubscript argument. In other words, an array whose highest subscript is 2 will contain three elements.

One-Dimensional Arrays – syntax version 2 {Dim | Private | Static} arrrayName() As data Type={initialValues} initialValues is a comma-separated list of values you want assigned to the array elements. does not include the highestSubscript argument; instead, an empty set of parentheses follows the array name. The computer automatically calculates the highest subscript based on the number of values listed in the initialValues section.

Array Data types STRING data types If the array’s data type is String, each element is initialized using the keyword Nothing. Variables initialized to Nothing do not actually contain the word Nothing; rather, they contain no data at all. NUMERIC data types (Decimal,double, single, integer, long,short,byte) If the array’s data type is numeric, each element is initialized to the number zero (0) BOOLEAN data type (true or false) Elements in a Boolean array are initialized to the keyword “False”

Populating an array Assigning initial values to an array is called populating the array You can list the initial values in the initialValues section of the syntax, using commas to separate the values, and you enclose the list of values in curly braces { } After an array is declared, you can store the data in the array To enter data into an array, specify the subscript and use an assignment statement

Populating an array using equations or user provided data

Array size The Number of Elements in an Array is determined by examining the Arrays length property One way to determine the highest subscript is by subtracting the number 1 from the array’s Length property. The GetUpperBound method in the below figure returns an integer that represents the highest subscript in the specified dimension of the array.

Using length to determine highest subscript The highest subscript in a one-dimensional array is always one number less than its number of elements (its length) determine the highest subscript by subtracting the number 1 from the array’s Length property. This is useful to traverse an array using a loop

Using GetUpperBound to determine highest subscript The highest subscript in a one-dimensional array is always one number less than its number of array elements. The GetUpperBound method returns an integer that represents the highest subscript in the specified dimension of the array.

Determining the Highest Subscript in a One-Dimensional Array When used with a one-dimensional array, the specified dimension, which appears between the parentheses after the method’s name, is always 0.

Sorting a One-Dimensional Array Sort Ascending Array.Sort() method—sorts the values in a one-dimensional array in ascending order. Sort Descending first use the Array.Sort method to sort the values ascending then use the Array.Reverse method to reverse the sorted values.

Processing an array using the For Each..Next loop The For Each..Next loop is a convenient way of coding a loop whose instructions you want processed for each element in a group, such as for each variable in an array. code does not need to keep track of the array subscripts nor know the number of array elements. the instructions in a For Each...Next statement can only read the array values; they cannot permanently modify the values. the elementVariable that appears in the For Each .. Next clauses is any name you wish to give the variable that the computer will use to keep track of each element in the array . the variable’s data type is specified in the As dataType portion of the For Each clause -- it must be the same as the arrays’ data type.

Resizing an Array Redimensioning Syntax: change the size of one or more dimensions of an array that has already been declared Reallocates storage space for an array variable –free or acquire Syntax: ReDim [ Preserve ] name(boundlist) [ ,  name(boundlist) [, ... ] ] Preserve - preserve the data in the existing array when you change the size of only the last dimension. Name - Name of the array variable Boundlist - List of bounds of each dimension of the redefined array

PARALLEL one dimensional Arrays

Parallel One-Dimensional Arrays Two or more arrays whose elements are related by their position in arrays (by their subscripts) One-to-One correlation between the two Arrays may be different data types

Parallel One-Dimensional Arrays (cont.)

ARRAYS and collections

Arrays, loops and Collection Controls Collection controls and arrays are similar and frequently used together Similarities Groups of individual objects treated as one unit Each object is identified by a unique number The first index in a collection and the first array subscript are both 0 Items in a list box or combo box belong to the Collection class and can can be associated with arrays

Arrays, loops and Collection Controls

Two-Dimensional Arrays

Two-Dimensional Arrays Resembles a table with rows and columns Each element is identified by a unique combination of two subscripts: (row, column) Subscripts are zero-relative Refer to an element using the name followed by the (row, column) pair in parentheses

Two-Dimensional Arrays (cont'd.) Two-dimensional array are declared with highest row subscript and highest column subscript (zero-relative) Number of rows = highest row subscript + 1 Number of columns = highest column subscript + 1 Can specify initial values for array elements If no initial values are declared, array elements are automatically initialized

Declaring a Two-Dimensional Array – syntax version 1 {Dim | Private | Static} arrrayName(highestRowSub,highestColumnSub) As data type highestRowSub and highestColumnSub are integers that specify the highest row and column subscripts, respectively, in the array. When the array is created, it will contain one row more than the number specified in the highestRowSub argument and one column more than the number specified in the highestColumnSub argument. This is because the first row and column subscripts in a two-dimensional array are 0.

Declaring a Two-Dimensional Array – syntax version 2 {Dim | Private | Static} arrrayName(,) As data Type = {{initialValues},…{initialValues}} used when you want to specify each variable’s initial value. include a comma within the parentheses that follow the array’s name, indicating that the array is a two-dimensional array Include a separate initialValues section, enclosed in braces, for each row in the array, and within the individual initialValues sections, enter one or more values separated by commas and all of the initialValues sections must be enclosed in a set of braces. The number of values to enter corresponds to the number of columns in the array.

Storing data in Two-Dimensional Arrays After an array is declared, you can use another statement to store a different value in an array element. Examples of such statements include assignment statements and statements that contain the TryParse method.

Storing data in Two-Dimensional Arrays

Determining the highest subscripts in a Two-Dimensional Array

Traversing Two-Dimensional Arrays Use an outer loop and an inner nested loop. Outer loop: keeps track of the row subscript Nested inner loop: keeps track of the column subscript. For Each…Next loop can only read the array values cannot permanently modify the values

Traversing Two-Dimensional Arrays using loops

Traversing Two-Dimensional Arrays using For Each..Next

Two-Dimensional Arrays

Two-Dimensional Array Example (cont'd.)