Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming In Visual Basic.NET

Similar presentations


Presentation on theme: "Programming In Visual Basic.NET"— Presentation transcript:

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

2 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.

3 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.

4 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.

5 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.

6 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.

7 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.

8 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.

9 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.

10 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.

11 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.

12 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.

13 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.

14 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.

15 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.

16 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.

17 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.

18 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.

19 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.

20 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.

21 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 > If mstuStudents(intIndex).strStudentID = txtID.Text Then blnFound = True lbl .Text = mstuStudents(intIndex).strUserName _ & 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.

22 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 1 © 2001 by The McGraw-Hill Companies, Inc. All rights reserved.

23 Referencing Elements in Multidimensional Array
intScoreBoard(row,column) 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.

24 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.

25 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.

26 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.

27 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.

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

29 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.

30 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.

31 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.

32 Lookup For a Corresponding Value
'Determine the brand row. intRow for VB Brand is 0. Select Case mstrSelectedRad Case "radA" : intCol = Case "radC" : intCol = Case "radX" : intCol = 3 End Select 'Determine the entered number's row. Dim blnFound As Boolean = False For intRow = 0 To 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.


Download ppt "Programming In Visual Basic.NET"

Similar presentations


Ads by Google