Programming In Visual Basic.NET

Slides:



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

Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
VBA Array-1 CS 105 Spring 2010 VBA Arrays What is an array? How can you identify an array by looking at the code? What is an element of an array and where.
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 7 Lists, Loops, and Printing
Chapter 8 Arrays and Collections Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
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.
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 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 8 Using Repetition with Loops and Lists. Class 8: Loops and Lists Write Do loops to execute statements repeatedly Write For loops to execute statements.
Apply Sub Procedures/Methods and User Defined Functions
ISAT 252 Introduction to Arrays. Should have read 2 Chapter 8 –pp , and pp
Chapter 8 Arrays and More.
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.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Using Arrays and File Handling
Chapter 17: Arrays Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Chapter 8 Arrays Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
CSCI Chapter 8 Arrays Instructor: Bindra Shrestha University of Houston – Clear Lake.
Week Arrays, Timers, and More 9. 2 Introduction Arrays are like groups of variables that allow you to store sets of similar data A single dimension array.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and 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.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
Chapter Six: Working With Arrays in Visual Basic.
© 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.
‘Tirgul’ # 3 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #3.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 8- 1.
8-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Arrays Chapter 8.
Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 8 Arrays and More.
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.
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.
4-1 Chapter 4 The Selection Process in VB.NET. 4-2 Learning Objectives Understand the importance of the selection process in programming. Describe the.
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.
Tutorial 101 Variable Arrays A group of variables that have the same name and data type and are related in some way Can have as many as 60 dimensions.
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.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 7 Where Can I Store This?
Arrays Chapter 8. Overview u General discussion u Variable arrays u Control arrays u Multi-dimensional variable arrays  Two-dimensional  Three-dimensional.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
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.
Arrays and More CHAPTER 8. INTRODUCTION Arrays are like groups of variables that allow you to store sets of similar data A single dimension array is useful.
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 Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
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
VBA - Excel VBA is Visual Basic for Applications
2. Understanding VB Variables
Brief description on how to navigate within this presentation (ppt)
Chapter : Arrays.
Arrays.
CIS16 Application Development and Programming using Visual Basic.net
Programming In Visual Basic .NET
Introduction to Computer Programming IT-104
Presentation transcript:

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

Case Structure Best alternative for testing a single variable or expression for multiple values Any decisions coded with nested If statements can also be coded using Case structure Case Structure is simpler, cleaner, more efficient than the nested If © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.

Select Case General Form (also notice indenting) Select Case expression Case ConstantList [ code to run] [Case ConstantList [ code to run]] [Case Else ] [code to run]] End Select If expression=value in constant list If expression=value in constant list If expression< >values in any of the preceding constant lists ** Case Else is an optional clause © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.

Select Case - Numeric Value Example 1 Private Function CalculateGrade(ByVal intScore As Integer) as String Select Case intScore Case Is >= 90 Return "A" Case 80 to 89 Return "B" Case 70 to 79 Return "C" Case 60 to 69 Return "D“ Case Else 'accommodate all other values Return "F" End Select End Function © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.

Select Case - Numeric Value Example 2 Select Case intListIndex Case 0 HandleItemZero( ) Case 1, 2, 3 HandleItems( ) Case Else HandleNoSelection( ) End Select © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.

Select Case - String Value Example Select Case mstrSelectedButton Case "radQuarter“: intRow = 0 Case "radHalf“: intRow = 1 Case "radFull“ : intRow = 2 Case Else ‘ use default intRow = 0 End Select © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.

Sharing an Event Procedure If the code for multiple controls is very similar,, the controls can share an event procedure Use the Handles Clause to add multiple controls to a handler Private Sub radBlack_CheckChanged(ByVal sender as System.Object, _ byVal e as System.Event.Args) Handles radBlack.CheckChanged, _ radBlue.CheckChanged, radRed.CheckChanged, _ radWhite.CheckChanged, radYellow.CheckChanged [block of code] End Sub © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.

sender Argument sender argument is the key to using the shared event sender is a System.Object type with Name property Possible Problem if Option Strict is turned on, reference to sender.Name will cause a compile error for Late Binding (i.e. type cannot be determined until run time rather than at compile time) Solution use CType function to convert sender to a specific object type, thus providing Early Binding © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.

CType Function Converts object from one type to another General Form CType (ValueToConvert, NewType) Example Dim radSelected As RadioButton radSelected = CType(sender, RadioButton) Select Case radSelected .Name Case "radBlue" . . . © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.

Sharing Event Example ' Declare a module level variable for storing color Dim mColorNew as Color ‘declare it module level . . . Private Sub radBlack_CheckChanged(ByVal sender as System.Object, _ byVal e as System.Event.Args) Handles radBlack.CheckChanged, _ radBlue.CheckChanged, radRed.CheckChanged, _ radWhite.CheckChanged, radYellow.CheckChanged Dim radSelected as RadioButton radSelected = CType (sender, RadioButton) ‘early binding Select Case radSelected.Name Case "radBlack“ : mColorNew = Color.Black Case "radBlue“ : mColorNew = Color.Blue Case "radRed“ : mColorNew = Color.Red Case "radWhite“ : mColorNew = Color.White Case "radYellow“ : mColorNew = Color.Yellow End Select End Sub © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.

Arrays Array is a collection of values referenced in memory by the same name Similar to list of items for list boxes and combo boxes - without the box Use an array to keep a series of variable for later processing such as Reordering Calculating Printing strName (0) (1) (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 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.

Array Terms strName Element Index (or subscript) Boundaries (0) (1) (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 Element Individual item in the array Index (or subscript) Zero based number used to reference the specific elements in the array Must be an integer Boundaries Lower Subscript, 0 by default Upper Subscript © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.

Two Ways to Declare Arrays Using upper bound versus assigned values Examples Dim ArrayName (UpperSubscript ) as Datatype ‘ In this case each element of the array will be assigned a default ‘value: numeric  0, string  “” (no characters) Dim ArrayName ( ) as Datatype = {InitialValueList} ‘No upper Subscript is allowed Dim strName (3) as String ‘an array of 4 elements: Dim decBalance(99) as Decimal ‘an array of 100 elements: Dim strDept ( ) as String = {"ACT", "MKT", "HR", "MIS"} Dim intActCode ( ) as Integer = {10, 20, 30, 40} © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.

Referencing Array Elements Use the Index(s) of the Element strName (0) (1) (2) (3) Jill Creech Paul Fry Rich Wells strName(0) : "Sam Smith" strName(1) : "Jill Creech" strName(2) : "Paul Fry" strName(3) : "Rich Wells" Sam Smith © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.

For Each / Next Loop General Form Example For Each ElementName In ArrayName Statements to execute Next [ElementName] ' Assumes array intTotal previously dimensioned Dim intOneTotal As Integer ‘must be the same type as intTotal For Each intOneTotal In intTotal ‘references EACH element intOneTotal=0 ' reinitialize element of the array Next intOneTotal © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.

Structures Combine multiple fields of data into a single unit Declaration (by default a Structure is Public) Cannot be declared inside a procedure Generally declared in General Declarations Define using Structure, End Structure Once created, declare variable of the Structure as if it were another datatype; make up a meaningful prefix © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.

Structure Declaration General Form Example [Public|Private] Structure NameOfStructure Dim FirstField As Datatype Dim SecondField As Datatype . . . End Structure Dim manyStructuredVar As NameOfStructure Structure CoffeeSale Dim strType As String Dim strQuantity As String Dim decPrice As Decimal End Structure Dim mcslTransaction(20) As CoffeeSale © 2001 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 empOffice.intEmpID empOffice.strFName empOffice.strLName empOffice.datHireDate prdInven(intIndex).strDesc prdInven(intIndex).strID prdInven(intIndex).intQuan prdInven(intIndex).decPrice © 2001 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, however, 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 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.

ReDim Statement Used to redeclare the UpperSubscript/Size of an array Can preserve original data in the array General Form (basic) ReDim [Preserve] ArrayName(UpperSubscript) Examples ReDim Preserve strDept(20) ReDim sdtHousewares.decSale(6) © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.

Table Lookup Using Structures Structure Student strStudentID As String strUserName As String End Structure mstuStudents(99) As Student ....... Dim intIndex As Integer Dim blnFound As Boolean blnFound = False intIndex = 0 Do Until blnFound Or intIndex > 99 If mstuStudents(intIndex).strStudentID = txtID.Text Then blnFound = True lblEmail.Text = mstuStudents(intIndex).strUserName _ & "@clarkson.edu“ End If intIndex = intIndex + 1 Loop If blnFound = False Then MsgBox "There is no such student in the list." © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.

Multidimensional Arrays Arrays can have more than one dimension Like a table of values You must specify the boundaries for each dimension using subscripts Example: Two Dimensional Array Dim intScoreBoard (1, 8) as Integer 0 1 2 3 4 5 6 7 8 1 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.

Referencing Elements in Multidimensional Array intScoreBoard(row,column) 0 1 2 3 4 5 6 7 8 1 0,0 0,1 0,2 0,3 0,4 0,5 0,6 0,7 0,8 1,0 1,1 1,2 1,3 1,4 1,5 1,6 1,7 1,8 intScoreBoard(0,0) intScoreBoard(0,1) intScoreBoard(0,2) intScoreBoard(0,3) intScoreBoard(0,4) intScoreBoard(0,5) intScoreBoard(1,0) intScoreBoard(1,1) intScoreBoard(1,2) intScoreBoard(1,3) intScoreBoard(1,4) intScoreBoard(1,5) © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.

Declaring Two-Dimensional Arrays General Form Example Dim ArrayName(HighestSubscript, Highest Subscript) as Datatype Dim ArrayName( , ) as Datatype = {ListOfValues} Dim strName(2, 3) As String ‘ Results in an array of 12 elements: ‘3 rows: 0, 1, 2 and 4 columns: 0, 1, 2, 3 Dim strName( , ) As String = {{"Jim", "Mary", "Sam", "Sean"}, _ {"Tom", "Sue", "Fred", "Paul"}, _ {"Tim", "Al", "Bob", "Pete"}} ‘ Also results in an array of 12 elements: ‘ 3 rows: 0, 1, 2 and 4 columns: 0, 1, 2, 3 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.

Using Two-Dimensional Arrays Initializing/Reinitializing Use Nested For/Next Loops Use For Each/Next Loop Printing Summing Include a total field for each row and each column Use For/Next Loop to calculate sums © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.

Initializing / Reinitializing Using For/Next loops Using For Each/Next loops Dim intRow As Integer Dim intColumn As Integer For intRow = 0 to 2 For intColumn = 0 to 3 strName(intRow, intColumn) = " " Next intColumn Next intRow Dim strElement As String For Each strElement In strName strElement = " " Next strElement © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.

Printing Example 'Print one name per line For Each strElement In strName 'Set up a line e.Graphics.DrawString(strElement, fntPrintFont, _ Brushes.Black, sngPrintX, sngPrintY) 'Increment the Y position for the next line sngPrintY += sngLineHeight Next strElement © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.

Summing Example decAmount(3,5) decRowTotal(3) 0 1 2 3 4 5 1 1 0 1 2 3 4 5 1 2 3 1 2 3 ROW TOTALS COLUMN TOTALS decColTotal(5) 0 1 2 3 4 5 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.

Summing Code Example 'Crossfoot Total a 2D table Dim decAmount(3,5) As Decimal Dim decRowTotal(3) As Decimal Dim decColTotal(5) As Decimal Dim intRowIndex As Integer Dim intColIndex As Integer For intRowIndex = 0 to 3 For intColIndex = 0 to 5 decRowTotal(intRowIndex) += decAmount(intRowIndex, intColIndex) decColTotal(intColIndex) += decAmount(intRowIndex, intColIndex) Next intColIndex Next intRowIndex © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.

Lookup Two-Dimensional Tables Lookup for an intersection cell (VBMailOrder example) Lookup for a corresponding value (VBAutoCenter example) © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.

Lookup For an Intersection Cell Dim mdecCharge(,) As Decimal = {{1D, 1.5D, 1.65D, 1.85D}, _ 'other elements specified} ....... Dim intCol, intRow As Integer Dim decCharge As Decimal 'Look up the price. intRow = lstWeight.SelectedIndex 'determine the row intCol = lstZone.SelectedIndex 'determine the column decCharge = CDec(mdecCharge(intRow, intCol)) 'find the Charge lblCharge.Text = FormatCurrency(decRate) 'display formatted © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.

Lookup For a Corresponding Value 'Determine the brand row. intRow for VB Brand is 0. Select Case mstrSelectedRad Case "radA" : intCol = 1 Case "radC" : intCol = 2 Case "radX" : intCol = 3 End Select 'Determine the entered number's row. Dim blnFound As Boolean = False For intRow = 0 To 8 If strParts(intRow, intCol) = txtTheirNumber.Text Then lblVBEquivalent.Text = strParts(intRow, 0) blnFound = True Exit For End If Next © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.