220 likes | 258 Views
Case, Arrays, and Structures. 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
E N D
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
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
Select Case General Form(also notice indenting) Select Caseexpression CaseConstantList [ code to run] [CaseConstantList [ 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
Select CaseintScore Case Is> =100 lblMsg.Text="Excellent Score" Case80 to 99 lblMsg.Text="Very Good" Case60 to 79 lblMsg.Text=“Average Score" Case Else lblMsg.Text="Improvement Needed" End Select Select Case - Numeric Value Example 1
Select Case - String Value Example Select CasetxtTeam.Text.ToUpper( ) Case"TIGERS" (Code for Tigers) Case"LEOPARDS" (Code for Leopards) Case"COUGARS", "PANTHERS" (Code for Cougars and Panthers) End Select
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
Declaring and Allocating Arrays Generally speaking, there are two ways to declare arrays: • Intitializer lists (if you know the values of the array during coding) • 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
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
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
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
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
Structures and Classes • Structures and classes are similar in the following respects: • Both have members, including constructors, methods, properties, fields, constants, enumerations, and events. • Both can implement interfaces. • Both can have shared constructors, with or without parameters. • Structures and classes differ in the following particulars: • Structures are value types; classes are reference types. • All structure members are Public by default; class variables and constants are Private by default • Structure variable declarations cannot specify initializers, the New keyword, or initial sizes for arrays; class variable declarations can. • 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. • Structures are not inheritable; classes are. • Structures are never terminated, so the common language runtime (CLR) never calls the Finalize method on any structure
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.
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