Programming In Visual Basic .NET

Slides:



Advertisements
Similar presentations
Chapter 8 Arrays and Collections Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Advertisements

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.
Programming In Visual Basic.NET
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.
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.
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
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Using Arrays and File Handling
Chapter 8 Arrays Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
MAT Meyer Week 2 Programming VB: ‘basics’ Review & preview: Events, variables, statements, etc. Images, Control arrays, For/Next Assignment: read.
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 Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
© 1999, by Que Education and Training, Chapter 8, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
‘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.
McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 8 Arrays.
1 Working with Data Structures Kashef Mughal. 2 Chapter 5  Please review on your own  A few terms .NET Framework - programming model  CLR (Common.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
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.
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.
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 Programming In Visual Basic.NET.
Irwin/McGraw-Hill Copyright© 2000 by the McGraw-Hill Companies, Inc. PowerPoint® Presentation to accompany prepared by James T. Perry University of San.
Chapter 9 Processing Lists with Arrays. Class 9: Arrays Understand the concept of random numbers and how to generate random numbers Describe the similarities.
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.
Processing Arrays Lesson 9 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
7-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
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.
Visual Basic CDA College Paphos Campus COM123 Visual Programming 1 Lecture: Charalambous Sotiris Week 8: COM123 Visual Programming 1 Lecture: Charalambous.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline 1 Introduction 2 Arrays 3Declaring Arrays 4Processing Array Contents 5 Multiple-Subscripted.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Arrays 1.
Chapter 8 Arrays Brief description on how to navigate within this presentation (PPT) The first time a Key Term from the chapter is used in the PPT, it.
Chapter 8 Arrays and More. Chapter 8 Arrays and More.
Programming Right from the Start with Visual Basic .NET 1/e
Chapter 6: Using Arrays.
VBA - Excel VBA is Visual Basic for Applications
Two Dimensional Array Mr. Jacobs.
IS 350 Arrays.
Chapter 7 Arrays.
2. Understanding VB Variables
Chapter 8 Arrays, Timers, and More.
Visual Basic .NET BASICS
Brief description on how to navigate within this presentation (ppt)
Chapter : Arrays.
CSCI 3327 Visual Basic Chapter 8: Introduction to LINQ and Collections
Visual Basic 2010 How to Program
Starting Out with Programming Logic & Design
Arrays.
CIS16 Application Development and Programming using Visual Basic.net
EXCEL Creating An Array In VBA
Programming Logic and Design Fifth Edition, Comprehensive
Chapter 7 Lists, Loops, and Printing
C++ Array 1.
Introduction to Computer Programming IT-104
Presentation transcript:

Programming In Visual Basic .NET Chapter 8 Arrays Programming In Visual Basic .NET

Single-Dimension Arrays List or series of values all referenced by the same name Similar to list of values for list boxes and combo boxes - without the box Use an array to store multiple values May be referred to as a table or subscripted variable © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Array Terms Element Subscript (or index) Boundaries Individual item in the array Subscript (or index) Zero based number used to reference the specific elements in the array Must be an integer Boundaries Lower Subscript, 0 by default Upper Subscript © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Array Example nameString Array (0) Janet Baker (1) George Lee (2) (3) (4) (5) (6) (7) (8) (9) Janet Baker George Lee Sue Li Samuel Hoosier Sandra Weeks William Macy Andy Harrison Ken Ford Denny Franks Shawn James © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

The Dim Statement for Arrays - General Form Dim statement allocates storage for specific number of elements and initializes numeric variables to 0 and string array elements to empty string (zero characters) Elements in an array may be assigned values in the Dim statement, cannot declare upper subscript and initial values Dim ArrayName(UpperSubscript) As Datatype Dim ArrayName( ) As Datatype = {InitialValueList} Dim ArrayName As Datatype( ) = {InitialValueList} © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Dim Statement for Arrays Examples Dim nameString(25) As String Dim balanceDecimal(10) As Decimal Dim productString(99) As String Dim indexInteger( ) As Integer = {1, 5, 12, 18, 20} Dim indexInteger As Integer( ) = {1, 5, 12, 18, 20} Dim departmentsString( ) As String = {"Accounting", "Marketing"} Private categoryString(10) As String Public idNumberString(5) As String © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Valid Subscripts Subscript must reference a valid element of an array VB rounds fractional subscripts VB throws exceptions for subscripts that are out of range © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

For Each/Next Statements Use Loops to reference each element in the array For / Next or For Each/Next VB references EACH element of the array and assigns its value to ElementName Variable used for ElementName must be same datatype as array elements or an Object datatype Makes one pass through the loop per element Use Exit For statement within loop to exit early © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

The For Each and Next Statements – General Form For Each ElementName In ArrayName ' Statement(s) in loop. Next [ElementName] © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

The For Each and Next Statements - Example ' Assumes array nameString is dimensioned and holds data. Dim eachNameString As String For Each eachNameString In nameString ' Write one element of the array. Debug.WriteLine(eachNameString) Next eachNameString © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Structures Combine multiple fields of data to create a new structure Similar to defining a new data type Define using Structure, End Structure Declaration (by default a Structure is Public) Cannot be declared inside a procedure Generally place with module-level declarations © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

The Structure and End Structure Statements - General Form [Public|Private] Structure NameOfStructure Dim FirstField As Datatype Dim SecondField As Datatype . . . End Structure © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

The Structure and End Structure Statements - Example Structure Employee Dim lastnameString As String Dim firstNameString As String Dim socialSecurityNumberString As String Dim streetString As String Dim stateString As String Dim zipCodeString As String Dim hireDate As Date Dim payCodeInteger As Integer End Structure ' Declaring a variable based on the Structure. Dim officeEmployee As Employee © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

The Structure and End Structure Statements - Example Public Structure Product Dim descriptionString As String Dim idString As String Dim quantityInteger As Integer Dim priceDecimal As Decimal End Structure ' Declaring a variable array based on the structure. Dim inventoryProduct(100) As Product © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Accessing the Elements in a Structure Variable Each field of data in Structure is an Element To access specify Variable.Element Examples officeEmployee.lastNameString officeEmployee.hireDate inventoryProduct(indexInteger).descriptionString inventoryProduct(indexInteger).quantityInteger inventoryProduct(indexInteger).priceDecimal © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Including An Array In A Structure Arrays can be included as elements within a Structure VB does not allow you to declare the number of elements in the array within the Structure declaration Use the ReDim statement inside a procedure to define the size of the array © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

ReDim Code Example ' Module-level declarations. Structure SalesDetail Dim saleDecimal( ) As Decimal End Structure Dim houseWaresSalesDetail As SalesDetail ' Inside a procedure. ' Establish the number of elements in the array. ReDim houseWaresSalesDetail.saleDecimal(6) ' In processing. houseWaresSalesDetail.saleDecimal _ (dayIndexInteger) = currentDaySalesDecimal © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Using Array Elements for Accumulators (pgs. 325-327) groupNumberInteger=(groupTextBox.Text - 1) totalInteger array (0) (1) (2) (3) (4) (5) (6) (7) 10 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Debugging Array Programs View the array elements in break time in the Autos window, click plus sign to left of array name to view individual array elements © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Table Lookup (pgs. 327-330) Often values used to identify a series of elements are not sequential Use a table lookup process to find the correct element in the array Establish a structure and dimension an array of the structure © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Table Lookup (continued) indexInteger=2 arrayGroup array (0) (1) (2) (3) (4) (5) (6) (7) 101 0 103 0 110 10 115 0 121 0 123 0 130 0 145 0 © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Using List Boxes With Arrays Use List Boxes or Combo Boxes rather than text boxes to look up information in the array Use the list's SelectedIndex property to determine the array subscript © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Using List Boxes With Arrays (continued) groupNumberInteger = groupListBox.SelectedIndex © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Multidimensional Arrays Dim statement specifies number of rows and columns in two-dimensional array May specify number of elements OR initial values Specify row with first subscript, column with second subscript © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

The Dim Statement for Two-Dimensional Arrays - General Form Dim ArrayName(HighestSubscript, Highest Subscript) As Datatype Dim ArrayName( , ) As Datatype = {ListOfValues} © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

The Dim Statement for Two-Dimensional Arrays - Examples Dim nameString(2, 3) As String Dim nameString( , ) As String = {{"James", "Mary", "Sammie", "Sean"}, _ {"Tom", "Lee", "Leon", "Larry"}, {"Maria", "Margaret", "Jill", "John"}} ' Both statements establish an array of 12 elements. (0, 0) James (0, 1) Mary (0, 2) Sammie (0, 3) Sean (1, 0) Tom (1, 1) Lee (1, 2) Leon (1, 3) Larry (2, 0) Maria (2, 1) Margaret (2, 2) Jill (2, 3) John © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Initializing Two-Dimensional Arrays Initializing/Reinitializing Use nested For/Next loop Printing a Two-Dimensional Table Use For Each/Next loop Summing a Two-Dimensional Table Include a total field for each row and each column Sum the figures in both directions (double-check totals) © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Nested For/Next Example Dim rowInteger As Integer Dim columnInteger As Integer For rowInteger = 0 To 2 For columnInteger = 0 To 3 ' Initialize each element. nameString(rowInteger, columnInteger) = " " Next columnInteger Next rowInteger © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Printing a Two-Dimensional Table ' Print one name per line. For Each elementString In nameString ' Set up a line. e.Graphics.DrawString(elementString, printFont, _ Brushes.Black, horizontalPrintLocationSingle, _ verticalPrintLocationSingle) ' Increment the Y position for the next line. verticalPrintLocationSingle += lineHeightSingle Next elementString © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Summing a Two-Dimensional Table amountDecimal (0) (1) (2) (3) (4) (5) rowTotalDecimal (0) (1) (2) (3) (0) (1) (2) (3) ROW TOTALS COLUMN TOTALS columnTotalDecimal (0) (1) (2) (3) (4) (5) © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Summing Code Example ' Crossfoot Total a 2D table. Dim amountDecimal( , ) As Decimal = {{2.5D, 3D, 1.2D, 2.2D, 4.5D, 3.5D}, _ {2D, 2D, 2D, 2D, 2D, 2D}, {3D, 3.1D, 3.2D, 3.3D, 3.4D, 3.5D}, _ {4.4D, 4.5D, 4.6D, 4.7D, 4.8D, 4.9D}} Dim rowTotalDecimal(3) As Decimal Dim columnTotalDecimal(5) As Decimal Dim rowInteger As Integer Dim columnInteger As Integer For rowInteger = 0 to 3 For columnInteger = 0 to 5 rowTotalDecimal(rowInteger) += amountDecimal(rowInteger, columnInteger) columnTotalDecimal(columnInteger) += amountDecimal(rowInteger, columnInteger) Next columnInteger Next rowInteger © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Lookup Operations for Two-Dimensional Tables Use same techniques as for single dimensional arrays Direct Reference (if meaningful row and column subscripts are available) Table Lookup Many 2D tables used for lookup will require additional one-dimensional arrays or lists to aid in the lookup process © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.

Lookup Example Using List Box (page 335) weightIndexInteger uses weightListBox.SelectedIndex zoneIndexInteger uses zoneListBox.SelectedIndex © 2005 by The McGraw-Hill Companies, Inc. All rights reserved.