Professional Documents
Culture Documents
APII 3 OOPb
APII 3 OOPb
Objectives
2
To :
Compare classes to standard modules
Create an object interface
OOP
OOP
4/22/2016
Standard Modules
Similarities:
How
Differences
Their
OOP
Classes
How Their Data Behaves
All module-level data (static and module- Objects are instantiated from a class.
level variables) is shared by all procedures Each object receives its own set of module data.
within the module.
There are never multiple instances of the
module data.
Module-level variables exist for the lifetime Module variables exist only for the duration of an objects
of the application.
lifetime. Objects can be created and destroyed as needed,
and when an object is destroyed, all its data is destroyed as
well.
How Their Functions and Procedures Behave
When you define a standard module, its Public functions and procedures of classes are NOT
public functions and procedures are immediately available to your program.
instantly available to other modules within An object would have to be instantiated before the
your application.
public variables and routines would be available.
Classes are templates for objects. At runtime, your code
doesnt interact with the code in the class module per se, but
it instantiates objects derived from the class.
Each object acts as its own class module and thus has its
own set of module data.
The client
OOP
4/22/2016
10
OOP
11
OOP
12
Any and all communication between the client and the object
must occur through this interface:
Properties
Methods
Events
OOP
4/22/2016
13
14
Properties
You might want client code to be able to retrieve an
employees information e.g. name, sex, age, and the date of
hire.
For client code to get these values from the object, the
object must expose an interface member for each of these
items.
Remember, the values exposed by an object are called
properties.
Therefore, each piece of data would have to be exposed as
a property of the Employee object.
OOP
15
Methods
In addition to properties, you can expose simple or complex
functions, such as Delete or AddNew.
E.g. the Employee objects Delete function might be complex.
OOP
16
Events
For
However,
OOP
4/22/2016
End Set
End Property
End Class
OOP
OOP
4/22/2016
21
Remember this?
Auto-Implemented Property
22
Remember this?
Auto-Implemented Property
When you declare an auto-implemented
property, Visual Studio automatically creates a
hidden private field called a backing field
that contains the property value.
The backing fields name is an underscore and
the property name.
Example:
E.g.,
23
Remember this?
Auto-Implemented Property
OOP
Because
OOP
4/22/2016
26
Return m_intHeight
End Get
Set(ByVal value As Integer)
If m_intHeight < 10 Then Exit Property
m_intHeight = value
End Set
End Property
End Class
27
OOP
Read-only properties
There will be times that you will want to create
properties that can be read but not changed.
28
4/22/2016
29
Public
Function
intNumber1 As Integer, _
AddTwoNumbers(ByVal
OOP
30
32
31
OOP
OOP
Next, double-click the button to access its Click event, and enter
the code in the following slide
OOP
4/22/2016
33
34
35
NB:
You can also use one statement:
Dim objMyObject As Object = New clsMyClass()
Later we discuss why this is not always advisable.
36
OOP
4/22/2016
37
reference an object.
The object reference (the address of the object) will be bound to the
object variable (objMyObject) when the program is executed instead
of when it is compiled (built).
OOP
Note:
thus a lot of
performance.
overhead
MessageBox.Show(objMyObject.AddtoNumbers(1, 2))
poor application
OOP
10
4/22/2016
OOP
OOP
44
The compiler can check for syntax and reference errors in your code so
that many problems are found at compile time, rather than at runtime.
OOP
OOP
11
4/22/2016
45
46
OOP
47
OOP
48
OOP
12
4/22/2016
49
OOP
OOP
objMyObject = Nothing
OOP
OOP
13
4/22/2016
53
54
Objects Lifetime
meaning memory
OOP
Objects Lifetime
Objects Lifetime
55
56
objThisObject = objThatObject
OOP
OOP
14
4/22/2016
Objects Lifetime
57
58
Tasks
Do the following tasks on your own
OOP
OOP
Tasks
Tasks
59
60
After clicking
OOP
15
4/22/2016
Tasks
61
OOP
16