Chapter 1: Classes - PowerPoint PPT Presentation

1 / 43
About This Presentation
Title:

Chapter 1: Classes

Description:

A namespace groups one or more classes under a common name ... Uses the clsEmployee, clsPersonalInfo, clsAddress, clsSalaryItem, and clsProject classes ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 44
Provided by: kip50
Category:
Tags: chapter | classes

less

Transcript and Presenter's Notes

Title: Chapter 1: Classes


1
Chapter 1 Classes
2
Overview
  • Introduction
  • Value Types and Reference Types
  • Object-Oriented Design
  • Creating Classes
  • Properties
  • Constructors
  • Multi-Tier Applications
  • Composition Relationships
  • Nested (Inner) Classes
  • Using Text Files

3
1.1 Introduction
  • Object-Oriented Programming (OOP)
  • A class is a pattern or blueprint
  • defines group of related objects with common
    characteristics
  • identifies a specific data type
  • Attributes and behaviors

4
1.2 Value Types and Reference Types
  • Value type
  • variable that contains its own data
  • primitives, such as Integer, Single, Decimal
  • Reference type
  • contains a reference to another memory location
  • objects and arrays, including strings

5
Value Type
  • Dim mCount As Integer 25

Dim temp As Integer mCount
temp 40 'mCount still equals 25
6
Reference Type
  • Dim S1 As Student

S1 New Student S1.Name "Fred Smith"
7
Assigning Reference Types
  • Dim tests() As Integer 80, 95, 88, 76, 54
  • Dim scores() As Integer tests

8
Life and the Garbage Collector
  • Lifetime of an object is determined by...
  • whether or not references to it still exist
  • Object with no references is unreachable
  • Unreachable object can be reclaimed by the
    garbage collector

9
Namespaces
  • A namespace groups one or more classes under a
    common name
  • Avoids collisions between identical class names
  • Good for
  • large applications
  • groups of programmers
  • Every VB project has a root namespace

10
1.3 Object-Oriented Design
  • Object-Oriented Analysis
  • detailed specification of the problem
  • find classes that reflect the application domain
  • Object-Oriented Design
  • create classes that cooperate and communicate

11
Design Steps
  • Find the classes
  • physical entities in the application domain
  • control structures
  • Describe the classes
  • attributes characteristics (properties)
  • operations actions the class may perform
    (methods)

12
Interface and Implementation
  • Interface
  • visible portion of a class
  • Implementation
  • hidden from client programs
  • private members
  • encapsulation principle

13
Inheritance
  • A derived class inherits characteristics of an
    existing class
  • GradStudent and StudentEmployee are derived
    classes

14
Clients and Servers
  • A server produces information consumed by client
  • Client creates a reference to the server and
    sends requests via method calls
  • Server sends responses via method return values

15
1.4 Creating Classes
  • Class declaration syntax
  • Public Class classname
  • member-declarations
  • End Class
  • Example
  • Public Class Student
  • Private id As String
  • Public New( ) ...
  • End Class

16
Hands-On Tutorial
  • Title Student Class
  • Program name Student1
  • Creates a Student class
  • Retrieves and displays values of member variables
    (fields)
  • Output

17
Methods
  • A method is a procedure declared inside a class
  • sub procedure
  • function procedure
  • Example
  • Public Class Student
  • . . .
  • Private Sub Print()
  • MessageBox.Show(mID ", " mName _
  • ", " mAge)
  • End Sub
  • End Class

18
Hands-On Tutorial
  • Adding the GetGradeAverage method
  • Program name Student1
  • Public mCreditsEarned As Single
  • Public mTotalGradePoints As Single
  • Public Function GetGradeAverage() As Single
  • Return mTotalGradePoints / mCreditsEarned
  • End Function

19
Public and Private Member Access
  • Public member
  • accessible to entire program
  • Private member
  • invisible outside the enclosing class
  • Fields should be private
  • Methods can be public or private

20
1.5 Properties
  • Property procedures
  • act like variables to clients
  • Visual Studio .NET creates a template
  • General format
  • Public Property name() As type
  • Get
  • End Get
  • Set(ByVal Value As type)
  • End Set
  • End Property

21
Age Property Example
  • Public Property Age() As Integer
  • Get
  • Return mAge
  • End Get
  • Set(ByVal Value As Integer)
  • If Value gt 1 And Value lt 130 Then
  • mAge Value
  • End If
  • End Set
  • End Property

22
ReadOnly Properties
  • Can be read, but not modified by clients
  • Use ReadOnly qualifier
  • Example
  • Public ReadOnly Property GradePointAverage() As
    Single
  • Get
  • If mCreditsEarned ltgt 0 Then
  • Return mTotalGradePoints / mCreditsEarned
  • Else
  • Return 0.0
  • End If
  • End Get
  • End Property

23
Hands-On Tutorial
  • Title Adding Properties to the Student Class
  • Program name Student2
  • Adds the following properties
  • ID
  • Name
  • Age
  • CreditsEarned
  • GradeAverage

24
Shared Variables
  • Contains data that is shared among all class
    instances
  • Also called a shared field
  • Program example SharedMembers
  • Usually private
  • Example
  • Private Shared mInstanceCount As Integer 0

25
Shared Properties
  • Sets/returns value of shared variable
  • Usually public
  • Example
  • Public Shared ReadOnly Property InstanceCount() _
  • As Integer
  • Get
  • Return mInstanceCount
  • End Get
  • End Property

26
1.6 Constructors
  • Methods that run automatically when an instance
    of a class is created
  • Always named New
  • Can be overloaded
  • each must have different parameter list
  • Default constructor example
  • Public Sub New()
  • mID "000000000"
  • mName "(unknown)"
  • End Sub

27
Overloading the Constructor
  • Public Sub New(ByVal id As String, ByVal name As
    String, _
  • ByVal age As Integer)
  • mID id
  • mName name
  • mAge age
  • End Sub
  • 'Sample call
  • Dim s2 As New Student("222222222", "Julio
    Gonzalez", 23)

28
Optional Constructor Parameters
  • Optional keyword lets you avoid overloading the
    constructor
  • You must supply default arguments
  • Public Sub New( _
  • Optional ByVal id As String "000000000", _
  • Optional ByVal name As String "(unknown)", _
  • Optional ByVal age As Integer 0)
  • mID id
  • mName name
  • mAge age
  • End Sub

29
Hands-On Tutorial
  • Title Adding a Parameterized Constructor to the
    Student Class
  • Student3 program

30
1.7 Multi-Tier Applications
  • Multi-tier application model
  • Design Patterns (1995) by Erich Gamma, Richard
    Helm, and others
  • Three layers, or tiers
  • presentation
  • business
  • data

31
Three Tiers (Layers)
  • Presentation
  • Interacts with the user, provides all
    input-output
  • Business
  • Contains processing rules and logic, including
    behaviors and attributes belonging to application
    entities
  • Data
  • Information storage and retrieval system, such as
    Database connections, recordsets, text files, and
    XML data

32
Hands-On Tutorial
  • Title Bank Teller Application
  • Program BankTeller
  • Presentation tier
  • frmTeller class
  • Business/Data tier
  • Account class

33
Hands-On Tutorial
  • Title Bank Teller Transaction Log
  • Program BankTeller2
  • Presentation tier
  • frmTeller class
  • Business tier
  • Account class
  • Data tier
  • TransactionLog class
  • logs all transactions to a file

34
1.8 Composition Relationships Between Classes
  • Created when one class contains fields that are
    instances of other classes
  • Also known as aggregation, or containing
    relationship

35
Employee Information Example
  • clsEmployee class contains objects of the
    following types
  • clsPersonalInfo
  • clsAddress
  • clsSalaryItem
  • clsProject

36
Using UML to Describe Classes
UML stands for Universal Modeling Language
37
Overriding ToString
  • Standard way of displaying the state of an object
  • Example
  • Public Overrides Function ToString() As String
  • Return mStreet ", " _
  • mCity ", " _
  • mState ", " _
  • mZip ", " mPhone
  • End Function

38
Hands-On Tutorial
  • Title Employee Information
  • Program EmployeeInfo
  • Uses the clsEmployee, clsPersonalInfo,
    clsAddress, clsSalaryItem, and clsProject classes

39
1.9 Optional Topic Nested (Inner) Classes
  • A nested class is declared inside an existing
    class
  • Good way of hiding a class
  • Prevents name clashes with other classes having
    the same name
  • Class NetworkConnection
  • Class Address
  • Private mURL(3) As Integer
  • End Class
  • End Class

40
1.10 Optional Topic Using Text Files
  • Advantages
  • save and retrieve information without the
    overhead of a database
  • platform independent
  • edit with any text editor
  • Required Imports
  • System.IO
  • System.IO.File

41
Text File Examples - 1
  • Find out if a text file exists
  • If Exists("input.txt") Then ...
  • Open a text file for reading
  • Dim inFile As StreamReader OpenText("input.txt")
  • Create a new text file
  • Dim outFile As StreamWriter CreateText("output.t
    xt")
  • Append to an existing text file
  • Dim outFile As StreamWriter AppendText("output.t
    xt")

42
Text File Examples - 2
  • Read a line from a text file
  • Dim temp As String
  • temp inFile.ReadLine()
  • Check for end of file
  • While inFile.Peek ltgt -1
  • temp inFile.ReadLine()
  • End While
  • Write a line to a text file
  • Dim temp As String "Some data"
  • outFile.WriteLine(temp)
  • Close a StreamReader or StreamWriter
  • inFile.Close()

43
The End
Write a Comment
User Comments (0)
About PowerShow.com