Lecture 11 - Enumerations and the Form1 Object

Embed Size (px)

Citation preview

  • 8/14/2019 Lecture 11 - Enumerations and the Form1 Object

    1/13

    Lecture 11 Enumerations

  • 8/14/2019 Lecture 11 - Enumerations and the Form1 Object

    2/13

    Outline

    Last lecture, we discussed Arrays By which we build an indexed data structure:

    Composed of multiple elements for data storage In which each element is the same data type.

    In this Lecture, we continue our discussion ofData Structures:

    With Enumerations Which allow us to place limits on our primitive data types.

    Along the way, we will also look at Objects

    Which combine Properties and Behaviors to form a complex data type. By looking more closely at Form1.

  • 8/14/2019 Lecture 11 - Enumerations and the Form1 Object

    3/13

    Introduction

    So far, most of the variables weve used have had few limitations

    Other than technical limits (i.e., based on max size).

    Example: A variable defined as Integercan take any value As long as that value is an Integer of reasonably small size.

    In contrast, aBooleanvariable is a bit different: It can assume only two values: True orFalse.

    It is common to want to limit variable values: i.e., restrict the range of values which the variable can store.

    Example: A variable to store the number of doors a car has

    Why do we need the ability to store an unrealistic value: 175, 923? Instead, a smaller limited value (e.g., 2-10) makes more sense.

    Such limited data types are calledEnumerations.

  • 8/14/2019 Lecture 11 - Enumerations and the Form1 Object

    4/13

    Declaring Enumerations Enumerations allow the programmer to build a new data type:

    By using one of the (integer-based) primitive data types: Integer(default), Long, Short, or Byte.

    And listing valid values one by one...

    Each valid value is given a value name. And is assigned an Integer value (either manually, or automatically).

    To declare an Enumeration, use the following syntax:

    Public Enum varNameAsInteger

    valueName_1 = 0

    valueName_2 = 1

    valueName_n = n

    End Enum

    Note on the Manual Assignment of integer values: You may assign Integers to some, all, or none of the allowed values

    Non-assigned values will be assigned to integers, automatically;

    Integers can be assigned in any order Several values may also take the same integer (but the 1st will be primary).

  • 8/14/2019 Lecture 11 - Enumerations and the Form1 Object

    5/13

    Error-Prevention Generally, enumerations play an important role in error-prevention:

    By preventing storage of invalid variable values This also places limits on calculation results. very useful for embedded systems .

    Example: A program running in aCars Ignition System Goal: Control fuel input based on various factors, such as

    Digital input from a temperature sensor.

    Question: What happens when the sensor goes bad? Returns the value: Temp = 10,000 degrees C?

    If a Double is used to store input temperature values: The unrealistic value will be passed along for calculation

    could be DISASTROUS.

    If the program uses an Enumeration: Example: 7,000 discrete values between -50 C and 650 C.

    The unrealistic value could be caught as an invalid input! Supports SMOOTH error-handling (e.g., adoption of the realistic max value).

    Problem: In VB .NET, invalid values are NOT automatically errors.

    This requires some additional programmingusing Properties.

  • 8/14/2019 Lecture 11 - Enumerations and the Form1 Object

    6/13

    A Brief Introduction to Objects

    Enumerations are not declared in subroutines

    But instead, within an Object that will use it, more permanently. Thus, it is declared within the Objects Class Definition.

    We havent covered Objectsbut, weve already been using them! Every time we create a new Windows Application:

    We always get an Object of type Form1. Form1 is defined between the Keywords:

    Class Form1() and End Class.

    Objects are defined to have: Characteristics: Members;

    So far, we have not used these directly

    All our defined variables have been in subroutines and functions Rather temporary, and limited in Scope.

    Members may be accessed eitherdirectly orindirectly, via Properties.

    Behaviors: Methods Weve already seen these:

    Event-Handlers (private subroutines to handle a button click)

    User-defined Methods (public subroutines and functions)

  • 8/14/2019 Lecture 11 - Enumerations and the Form1 Object

    7/13

    Example: Day-Action

    So now, Lets consider an Enumeration example: A Program called Day-Action that gets/accepts the time of day

    And predicts a current Day-Action for our Sim-Person, Albert

    based on that time.

    Alberts allowed Day-Actions will include 8 states: Asleep

    Getting Ready for Work

    Traveling to Work

    At Work

    At Lunch

    Traveling from Work

    Relaxing with Friends

    Getting ready to Sleep

    We will define and use anEnumerated Data type

    to keep track of (predict) Alberts Actions versus time of day.

  • 8/14/2019 Lecture 11 - Enumerations and the Form1 Object

    8/13

    Enumerations Example: DayAction

    Lets write a VB .NET program, to implement ourEnum:

    trkHour

  • 8/14/2019 Lecture 11 - Enumerations and the Form1 Object

    9/13

    Example: DayAction

    Next, lets define Enum typeand declare 1 instance:

  • 8/14/2019 Lecture 11 - Enumerations and the Form1 Object

    10/13

    Example: DayAction (cont.)

    Next, lets add a Load event-handler.

  • 8/14/2019 Lecture 11 - Enumerations and the Form1 Object

    11/13

    Example: DayAction (cont.)

    So, lets define the HourProperty (Set and Get)

    And make it so that the Hour Property is Set upon loading. Via the Form1_Load Private Subroutine.

  • 8/14/2019 Lecture 11 - Enumerations and the Form1 Object

    12/13

    Example: DayAction (cont.)

    Now, lets define the trkHourevent-handlerfor scrolling So the txtStatus.Text displays output, when trkHouris scrolled.

  • 8/14/2019 Lecture 11 - Enumerations and the Form1 Object

    13/13