Case, Arrays, and Structures - PowerPoint PPT Presentation

About This Presentation
Title:

Case, Arrays, and Structures

Description:

Best alternative for testing a single variable or expression for ... Case 'COUGARS', 'PANTHERS' (Code for Cougars and Panthers) End Select. About Arrays ... – PowerPoint PPT presentation

Number of Views:246
Avg rating:3.0/5.0
Slides: 21
Provided by: spi93
Learn more at: https://www.csus.edu
Category:

less

Transcript and Presenter's Notes

Title: Case, Arrays, and Structures


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 expressionvalue in constant list
If expressionvalue in constant list
If expressionlt gtvalues 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 gt 100
  • lblMsg.Text"Excellent Score"
  • Case 80 to 99
  • lblMsg.Text"Very Good"
  • Case 60 to 79
  • lblMsg.TextAverage 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 arrays Length property

8
Arrays
9
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

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 procedures 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 elements 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
  • 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

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
Write a Comment
User Comments (0)
About PowerShow.com