Chapter 6, Part 2 - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Chapter 6, Part 2

Description:

We would need to assign the system date to ... intMonth = 1 assign default date. intDay = 1. intYear ... In our assignments we will NOT be including ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 30
Provided by: JuanMa4
Category:
Tags: assign | chapter | part

less

Transcript and Presenter's Notes

Title: Chapter 6, Part 2


1
CIS159
  • Chapter 6, Part 2
  • Constructors
  • Ch06_2.ppt

2
Overview of Topics
  • Class vs Object
  • ReadOnly Properties
  • Instance vs. Shared
  • Constructors and Destructors

3
Class and Objects
  • A class is a definition of a data type.
  • A class definition includes members variables and
    methods.
  • An object is a variable declared using a class
    definition as the data type.
  • A class is the definition, and an object is an
    instance of the class.

4
DateMDY Class
  • Notation
  • - private
  • public
  • protected

5
DateMDY Definition
  • Public Class DateMDY
  • Private intMonth, intDay, intYear As Integer
  • Property Month( ) As Integer
  • Get
  • Month intMonth
  • End Get
  • Set (ByVal Value As Integer) Value required
    keyword
  • intMonth Value
  • End Set
  • End Property
  • need property procedures for Day and Year
  • Public Function getDate( ) As String getDate
    CStr(intMonth) / CStr(intDay)
    / CStr(intYear)End Function
  • End Class

6
Public vs Private Members
  • Public variables can be referenced and altered in
    procedures that declare an object using the class
    definition.
  • Public methods can be called directly in
    procedures that declare an object using the class
    definition.
  • Public is the default if not specified.
  • Private variables can only be referenced and
    altered by methods defined inside of the class.
  • Private methods can only be called by methods
    defined inside of the class.
  • We must provide Property Procedures for private
    variables that allow programmers to set and get
    the values stored in the private variables.

7
ReadOnly Properties
  • This is used for variables that are maintained by
    the class, but may need to provide the value to
    the program at some time.
  • This could be used on running totals, counts, or
    constants used in the class.
  • The getDate function could be converted to a
    ReadOnly Property.
  • Use the keyword ReadOnly on the property
    procedure to only create the Get portion.

8
Defining ReadOnly
  • Public Class DateMDY
  • Need to return the date for all instances
    of DateMDY.Convert getDate to Property Date.
  • ReadOnly Property Date ( ) As String
  • Get
  • Date CStr(intMonth) /
    CStr(intDay) / CStr(intYear)
  • End Get
  • No Set because it is read only.End Property
  • End Class

9
Using ReadOnly Property Procedures
  • Dim bday As New DateMDY
  • bday.Month 10 use property procedure names
  • bday.Day 31 Automatically calls Set
    procedure
  • bday.Year 1980
  • txtDate.Text bday.Date( ) Calls the Get
    procedure for Date

10
Instance vs. Shared Variables
  • Variables defined in a class are normally
    considered Instance variables.
  • Each object created would have separate memory
    allocations for their own variables.
  • A shared variable is a single variable for all
    objects.
  • It can be used to keep a running total, count, or
    it could be a constant.
  • Shared members can be accessed without
    instantiating an object of the class (use
    className.member).
  • Property methods that reference a shared variable
    must also be declared as shared.
  • Methods that are declared as shared, must only
    reference class variables that are shared and/or
    local variables.

11
Defining Shared
  • Public Class DateMDY
  • The current date is needed for all
    instances of DateMDYWe would need to assign the
    system date to these variables
  • Private Shared intCurrMonth, intCurrDay,
    intCurrYear As Integer
  • Shared ReadOnly Property CurrentDate( ) As
    String
  • Get
  • CurrentDate CStr(intCurrMonth)
    / CStr(intCurrDay) /
    CStr(intCurrYear)
  • End Get
  • No Set because it is read only.End Property
  • End Class

12
Using Shared Variables
  • Dim bday As New DateMDY
  • Dim dueDate As New DateMDY
  • lblCurrDate.Text bday.CurrentDate Both
    have the same value
  • lblCurrDate.Text dueDate.CurrentDate
  • Shared methods can also be called using class
    name.
  • lblCurrDate.Text DateMDY.CurrentDate
  • Need to assign values first

13
Passing Objects to Functions
  • Individual variables can be passed to functions
    using Get procedures FormatNumber(bday.Month)
  • Entire objects can be passed to functionsSub
    add30Days(ByRef dueDate As DateMDY)
  • The reference (address) is passed to the
    function, so the local object actually points to
    the location of the original object.
  • Any changes performed in the function will effect
    the original object.
  • In a call-by-value the changes will be local to
    the function.

14
Constructor Characteristics
  • A special method of the class.
  • The name of the Sub is New ( ).
  • Can NOT return a value.
  • Primarily used to initialize member variables.
  • Automatically called when an object of that class
    type is declared.
  • Must be Public.

15
Default Constructor
  • Default constructor has no parameters.
  • Always define a default constructor, even if it
    will not be doing any initialization.
  • If one is not defined,the compiler will generate
    one.
  • Usually used to assign a default value. Public
    Sub New ( ) intMonth 1 assign default
    date intDay 1 intYear 1900 End Sub

16
Overloaded Constructors
  • Constructors can be overloaded.
  • Same method name but a different number or type
    of parameters.
  • Initial values can be passed at declaration if
    there is a parameterized constructor
    defined.Sub New (ByVal m As Integer, _ ByVal
    d As Integer, _ ByVal y As
    Integer) intMonth m intDay d intYear
    yEnd Sub

17
Property Procedures and Constructors
  • Use property procedures by using the property
    name, because any validation would be performed
    in the Set procedure.
  • Sub New (ByVal m As Integer, _ ByVal d As
    Integer, _ ByVal y As Integer) Month
    m calls set procedure Day d Year yEnd
    Sub

18
Keyword Me
  • Sometimes we declare variables in class
    procedures using the same names as of the class
    variables or property names.
  • Local variables override class variables and
    properties.
  • Use Me to reference the class variables or
    properties.Sub New (ByVal Month As Integer,
    _ ByVal Day As Integer, _ ByVal Year As
    Integer) Me.Month Month calls set
    procedure Me.Day Day Me.Year YearEnd Sub

19
Date Property
  • In the next two slides, you are presented with
    some questions, and you will need to recall what
    the Date property does, so it is presented here
    as a reminder.
  • ReadOnly Property Date ( ) As String
  • Get
  • Date CStr(intMonth) /
    CStr(intDay) / CStr(intYear)
  • End Get
  • No Set because it is read only.End Property

20
Using Default Constructors
  • Public Sub New ( ) intMonth 1 assign default
    date intDay 1 intYear 1900End Sub
  • In the declaration below determine why the
    default constructor is called and what is
    returned by Date( ).
  • Dim bday As New DateMDY
  • txtDate.Text bday.Date( )

21
Using Overloaded Constructors
  • Sub New (ByVal m As Integer, _ ByVal d As
    Integer, _ ByVal y As Integer) Month
    m calls set procedure Day d Year yEnd
    Sub
  • In the declaration below determine why overloaded
    constructor is called and what is returned by
    Date( ).
  • Dim payDate As New DateMDY(2, 26, 2002)
  • txtDate.Text payDate.Date( )

22
Constructor Example
  • Dim strName As String calls default
  • Dim strName As New String (Marquez) calls
    overloaded
  • Note must use New to access overloaded String
    constructor.

23
Destructor
  • A method that is automatically called when object
    goes out of scope.
  • Must be public.
  • The name of Sub is Finalize( ).
  • Can NOT return a value.
  • Can be used to cleanup dynamic variables.
  • It is implemented as an Override method.
  • It is recommended that we do not declare one
    unless it is required, because the .Net Framework
    performs it own garbage collection (releases
    memory).

24
Class Outline - Expanded
  • Class Name
  • Properties (variables)
  • Operations (methods)
  • Constructors
  • Property Procedure (set and get)
  • Operations (getDate, Date)
  • Destructors

25
Data Validation in Property Procedures
  • In property procedures data validation should
    usually be included.
  • When invalid data is sent in it can be rejected
    by Throwing an exception.
  • That means that the declaration of the object and
    usage of its property methods should be inside
    an Try/Catch block.
  • In our assignments we will NOT be including data
    validation ?
  • The textbook examples include if statements to
    validate, but dont do anything if data is
    invalid ?.

26
Data Validation Definition
  • Property Description As String
  • Get
  • Description mstrDescription
  • End Get
  • Set (ByVal Value As String) If Value Not
    Then
  • mstrDescription Value Else
    Throw New System.Exception(Description is
    missing.)End Set
  • End Property

27
Data Validation Usage
  • Private Sub btnProcess()
  • Try objBookSale New BookSale
  • objBookSale.Description txtDescription.Test
    additional processing here
  • Catch ex As Exception
  • MessageBox.Show("Error " ControlChars.NewLine
    _ ex.Message) End Try
  • End Sub

28
Summary
  • Class vs Object
  • ReadOnly Properties
  • Instance vs. Shared
  • Constructors and Destructors

29
Next
  • Review BookSale class in textbook (p283-284).
  • Shared, ReadOnly, Constructors
  • Review VB8 Classes
  • Complete clsCustomer and clsOrder
  • Next Class
  • Ch 6 Part 3 Inheritance
  • Assign VB9 Inheritance
Write a Comment
User Comments (0)
About PowerShow.com