Unformatted text preview:

Chapter 8 ArraysCase StructureSelect Case General Form (also notice indenting)Select Case - Numeric Value Example 1Select Case - Numeric Value Example 2Select Case - String Value ExampleSharing an Event ProcedureHandles ClauseHandles Clause Examplesender ArgumentCType FunctionSharing Event ExampleArraysArray TermsSimple Array ExampleDefining ArraysDefining Arrays - Alternate FormGeneral Form Dim Statement for ArraysDim Statement for Arrays Examples - Default ValuesDim Statement for Arrays Examples - Assigned ValuesWhat does VB do with the array?Referencing Array ElementsWorking with ArraysFor Each / NextFor Each Loop General FormFor Each / Next ExamplesStructuresStructure/End Structure General FormStructure Example 1Structure Example 2Accessing the Elements in a Structure VariableIncluding An Array In A StructureReDim StatementUsing Array Elements for Accumulators (p 344 -346)Debugging Array ProgramsTable Lookup (p346 - 350)Table Lookup (cont.)Using List Boxes With ArraysList Boxes With Arrays (cont.)Multidimensional ArraysReferencing Elements in Multidimensional ArrayGeneral Form Dim Statement for Two-Dimensional ArraysDim Statement for Two Dimensional-Arrays ExamplesWorking With Two-Dimensional ArraysInitializing For/Next ExampleInitializing For Each/Next ExamplePrinting ExampleSumming ExampleSumming Code ExampleLookup Two-Dimensional TablesLookup Example Using List Box (p 356)Chapter 8ArraysProgramming InVisual Basic.NET© 2001 by The McGraw-Hill Companies, Inc. All rights reserved.8- 2Case 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.8- 3Select Case General Form(also notice indenting)Select Case expressionCase ConstantList [ code to run] [Case ConstantList [ code to run]][Case Else ][code to run]]End SelectIf expression=value in constant listIf expression=value in constant listIf 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.8- 4Select Case - Numeric Value Example 1Select Case intScoreCase Is > =100lblMsg.Text="Excellent Score"Case 80 to 99 lblMsg.Text="Very Good"Case 60 to 79 lblMsg.Text="Excellent Score"Case Else lblMsg.Text="Improvement Needed"End Select© 2001 by The McGraw-Hill Companies, Inc. All rights reserved.8- 5Select Case - Numeric Value Example 2Select Case intListIndexCase 0HandleItemZero( )Case 1, 2, 3HandleItems( )Case Else HandleNoSelection( )End Select© 2001 by The McGraw-Hill Companies, Inc. All rights reserved.8- 6Select Case - String Value ExampleSelect Case txtTeam.Text.ToUpper( )Case "TIGERS"(Code for Tigers)Case "LEOPARDS" (Code for Leopards)Case "COUGARS", "PANTHERS" (Code for Cougars and Panthers)End Select© 2001 by The McGraw-Hill Companies, Inc. All rights reserved.8- 7Sharing an Event Procedure•If the code for multiple controls is very similar, rather than writing separate code for each, the controls can share an event procedure•Use the Handles Clause at the top of the event procedure to enable the code in a single event to be used for multiple controls•Example - radio buttons for selecting color have essentially the same code© 2001 by The McGraw-Hill Companies, Inc. All rights reserved.8- 8Handles Clause•Added to the top of an event procedure to make the procedure respond to events of other controls•Key to using the shared event is the sender argument that is passed to the event procedure•Good technique is to declare a module level variable to store the user's selection (radio button for color in the example)© 2001 by The McGraw-Hill Companies, Inc. All rights reserved.8- 9Private Sub radBlue_CheckChanged(ByVal sender as System.Object, _byVal e as System.Event.Args) _Handles radBlue.CheckChanged, radBlack.CheckChanged, _ radRed.CheckChanged, radWhite.CheckChanged, _radYellow.CheckChangedHandles Clause Example© 2001 by The McGraw-Hill Companies, Inc. All rights reserved.8- 10sender Argument•sender is an object with a Name property•Possible Problem - if you refer to sender.Name with Option Strict turned on, a compile error for Late Binding is generated (i.e. type cannot be determined until run time rather than at compile time)•Solution - Before using, use CType function to convert sender to a specific object type instead of the generic object© 2001 by The McGraw-Hill Companies, Inc. All rights reserved.8- 11CType Function•Converts object from one type to another•General FormCType (ValueToConvert, NewType)•ExampleDim radSelected as RadioButtonradSelected = CType(sender, RadioButton)Select Case radSelected . NameCase "radBlue". . .© 2001 by The McGraw-Hill Companies, Inc. All rights reserved.8- 12' Declare a module level variable for storing colorDim mColorNew as Color. . .Private Sub radBlue_CheckChanged(ByVal sender as System.Object, _byVal e as System.Event.Args) _Handles radBlue.CheckChanged, radBlack.CheckChangedDim radSelected as RadioButtonradSelected = CType (sender, RadioButton)Select Case radSelected . NameCase "radBlue"mColorNew = Color.Blue. . .End SelectEnd SubSharing Event ExamplebtnOK's click event sets form's BackColor to mColorNew© 2001 by The McGraw-Hill Companies, Inc. All rights reserved.8- 13Arrays•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 keep a series of variable for later processing such as –Reordering–Calculating–Printing© 2001 by The McGraw-Hill Companies, Inc. All rights reserved.8- 14Array Terms•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.8- 15Simple Array ExamplestrName Array(0)(1)(2)(3)(4)(5)(6)(7)(8)(9)Janet BakerGeorge LeeSue LiSamuel HoosierSandra WeeksWilliam MacyAndy HarrisonKen FordDenny FranksShawn James© 2001 by The McGraw-Hill Companies, Inc. All rights reserved.8- 16Defining Arrays •Use Dim statement to declare•Specify the number of elements in the array as the UpperSubscript•Each element of the array will be assigned a default


View Full Document

St. Ambrose CSCI 275 - Arrays

Download Arrays
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Arrays and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Arrays 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?