Classes in VB'Net - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Classes in VB'Net

Description:

Objects tightly 'bundle' data & related functions (methods) as members. This is Encapsulation. Class is the custom data type from which an instance, an object, is ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 13
Provided by: cisUso
Category:
Tags: bundle | classes | freshly | net

less

Transcript and Presenter's Notes

Title: Classes in VB'Net


1
Classes in VB.Net
  • ITE285
  • R. Johnson
  • DRAFT

2
Classes in Java
  • Objects tightly bundle data related
    functions (methods) as members
  • This is Encapsulation
  • Class is the custom data type from which an
    instance, an object, is declared.
  • Inheritance Polymorphism also in VB.NET

3
Parts of An Object in Java
  • Data members (private)
  • Sets and gets (called explicitly)
  • Methods
  • Constructor default and overloaded
  • Destructor

4
Parts of An Object in VB.NET
  • Data members (private)
  • Properties (easier sets and gets)
  • Methods
  • Constructor default and overloaded
  • Destructor (when dynamic memory allocated)

5
Property Procedures
  • A property is a wrapper around a private data
    member.
  • Incorporates sets and gets.
  • Feels like accessing a public data member.
  • Right side of ? automatically calls get
  • Left side of ? automatically calls set

6
Three Ways to Approach Objects
  • Classes strengthen separation of Non-Interface
    code
  • Think of objects as smart data in collection
  • Interface code --- properties for custom dialogs

7
A Simple Class EPAtestCar
  • Model
  • Miles
  • Gallons Gas Used
  • Low Emissions Vehicle?
  • Method CalcMPG

8
Additions to EPAtestCar
  • Property Procedures (streamlined sets gets)
  • Default Overloaded Constructors (New)
  • Override inherited ToString method
  • CompareTo method and interface IComparable (lets
    Array.Sort() work)
  • Throwing exceptions from inside a class to be
    caught elsewhere. From property sets?

9
A Common Class Design Pitfall
  • Normally do NOT create data members which store
    the results of calls to the classs methods.
  • Only store essential, independent data as data
    members.
  • Derived or dependent data should be obtained by
    calling the method(s) which compute it, as
    needed.
  • Example a class Rectangle with properties for
    length and a method to calculate the area

10
Bad Practice!
  • Dim r As Rectangle has an area data member
  • r.length 4 set properties
  • r.width 2
  • r.calcArea() call subroutine method
  • that only stores
    result internally
  • Messagebox.show (r.area) access property
  • storing the
    result of 8

11
Why is this Bad?
  • Dim r As Rectangle has an area data member
  • r.length 4 set properties
  • r.width 2
  • r.calcArea() call subroutine method,
    stores area
  • r.Length 5 change length!!!
  • object now inconsistent, stored area is invalid
    now!
  • Messagebox.show (r.area) has 8, should be 10!

12
Good Practice
  • Dim r As Rectangleno data member for area!
  • r.length 4 set properties
  • r.width 2
  • call function method that returns a freshly
  • computed value as needed
  • Messagebox.show (r.calcArea())
  • Alternative this could instead be a ReadOnly
    property whose Get does the calculation whenever
    referenced
Write a Comment
User Comments (0)
About PowerShow.com