Download presentation
Presentation is loading. Please wait.
1
Case, Arrays, and Structures
2
Summary Slide Case Structure –Select Case - Numeric Value Example 1 –Select Case - String Value Example Arrays –Declaring and Allocating Arrays –Array Declaration Example –Passing Arrays to Procedures –Example Passing Arrays –Multidimensional Rectangular and Jagged Arrays –For Each/Next Repetition Structure –Example Multidimensional Array with Each/Next Repetition Structures –Example of a Structure –Structures and Classes –Structure Variables Structures and Arrays
3
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
4
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 values in any of the preceding constant lists ** Case Else is an optional clause
5
Select Case - Numeric Value Example 1 Select Case intScore Case Is > =100 lblMsg.Text="Excellent Score" Case 80 to 99 lblMsg.Text="Very Good" Case 60 to 79 lblMsg.Text=“Average Score" Case Else lblMsg.Text="Improvement Needed" End Select
6
Select Case - String Value Example Select Case txtTeam.Text.ToUpper( ) Case "TIGERS" (Code for Tigers) Case "LEOPARDS" (Code for Leopards) Case "COUGARS", "PANTHERS" (Code for Cougars and Panthers) End Select
7
About Arrays Arrays are data structures consisting of data items of the same type Position number –Values that indicate specific locations within arrays –The first element in every array is the zero element Length property –Every array in Visual Basic “knows” its own length through the Length property GetUpperBound method –Returns the index of the last element in the array –The value returned by this GetUpperBound is one less than the value of the array’s Length property
8
Arrays
9
Declaring and Allocating Arrays Generally speaking, there are two ways to declare arrays: 1. Intitializer lists (if you know the values of the array during coding) 2. Declare and populate method (if you do not know the values of the array elements during coding because they will be dynamically loaded to the array) The amount of memory required by an array depends on the length of the array and the size of the data type of the elements in the array
10
Array Declaration Example
11
Passing Arrays to Procedures Passing the Array –Specify the name of the array without using parentheses –Every array object “knows” its own upper bound Do not need to pass the upper bound of the array as a separate argument –In Visual Basic, arrays always are passed by reference Receiving the array –The procedure’s parameter list must specify that an array will be received
12
Example Passing Arrays
13
Multidimensional Rectangular and Jagged Arrays Multidimensional arrays (multiple-subscripted) –Require two or more indices to identify particular elements Rectangular arrays –Two indices, first identifies the element’s row, the second the elements column –A rectangular two-dimensional array with m rows and n columns is called an m-by-n array Jagged arrays –Jagged arrays are maintained as arrays of arrays –Rows in jagged arrays can be of different lengths
14
For Each/Next Repetition Structure For Each/Next –Provided to iterate through the values in a data structure, such as an array –Instead of a counter, For Each/Next uses a variable to represent the value of each element –Useful when the indices of the elements are not important –Particularly useful for looping through arrays of objects
15
Example Multidimensional Array with Each/Next Repetition
16
Structures A structure is a combination of multiple fields of related data. The Structure declaration cannot go inside a procedure Structure statements are typically placed at the top with the module level declarations or in a separate class Once you have created a structure, you can declare variables of the structure, just as if it were another data type
17
Example of a Structure
18
Structures and Classes Structures and classes are similar in the following respects: 1.Both have members, including constructors, methods, properties, fields, constants, enumerations, and events. 2.Both can implement interfaces. 3.Both can have shared constructors, with or without parameters. Structures and classes differ in the following particulars: 1.Structures are value types; classes are reference types. 2.All structure members are Public by default; class variables and constants are Private by default 3.Structure variable declarations cannot specify initializers, the New keyword, or initial sizes for arrays; class variable declarations can. 4.Structures implicitly inherit from the ValueType class and cannot inherit from any other type; classes can inherit from any class or classes other than ValueType. 5.Structures are not inheritable; classes are. 6.Structures are never terminated, so the common language runtime (CLR) never calls the Finalize method on any structure
19
Structure Variables Once you have created a structure, you can declare procedure-level and module-level variables as that type. For example, you can create a structure that records information about a computer system, as in the following code: You can now declare variables of that type, as follows: To assign and retrieve values from the elements of a structure variable, you use the same syntax as you use to set and get properties on an object.
20
Structures and Arrays A structure can contain an array as one of its elements You access the values of an array within a structure the same way you access a property on an object You can also declare an array of structures
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.