Apllied Programming
Apllied Programming
Issues in Object-Oriented
Programming
Classes vs. Objects
Classes are templates that we use to create new
objects.
Classes are the blueprints used to manufacture
objects in your code.
The New() procedure is known as the class
constructor.
Issues in Object-Oriented
Programming
Object vs Object Variables
Object Variables - all variables that refer to objects
The following statement declares a variable of the Customer
type, but doesn't create an object:
Issues in Object-Oriented
Programming
The Cust variable can be set later in the code to
reference an existing instance of the class:
Dim Cust As Customer
Dim Cust2 As New Customer
Cust = Cust2
To set the Company property, you can use either one of the
following statements because they both point to the same object in
memory:
Cust.CompanyName = "New Company Name"
or
Cust2.CompanyName = "New Company Name"
Issues in Object-Oriented
Programming
Its also common to declare object variables without the
New keyword when you know youre going to use them
later in your code, as shown in the following loop, which
creates 20 items and adds them to a ListView control:
Dim LI As ListViewItem
For row = 0 To 20
LI = New ListViewItem
LI.Text = "..."
more statements to set up the LI variable
ListView1.Items.Add(LI)
Next
Issues in Object-Oriented
Programming
Uninitialized and Nullable Variables
- an object variable may exist but not be initialized.
Dim B As SolidBrush
The B variables value is Nothing because it hasnt been
initialized yet. After executing the following statement, the
B variable will have a value and can be used to draw
something:
B = New SolidBrush(Color.Blue)
Issues in Object-Oriented
Programming
To find out whether a variable has been initialized or not,
we use the Is operator to com-pare the variable to
Nothing:
If B Is Nothing Then
MsgBox("Uninitialized Brush variable")
End If
Issues in Object-Oriented
Programming
Lets consider an Integer and a String variable declared as
follows:
Dim Age As Integer
Dim Name As String
The Age and Name variables have not been initialized
explicitly, but they do have a value.
Integers are initialized to zero and strings are initialized to
empty strings.
Issues in Object-Oriented
Programming
A variable that has no value is not necessarily a numeric
zero or an empty string. To differentiate between the default
values and the lack of value, the Framework supports the
Nullable type, which indicates a variable of any of the basic
types that will not be initialized implicitly.
The Nullable keyword is followed by a pair of parentheses
and the Of keyword, followed by the actual type. The
following statement declares an Integer variable that is not
initialized:
Dim Age As Nullable(Of Integer)
Issues in Object-Oriented
Programming
Dim Age As Nullable(Of Integer)
other statements
Dim Qualifies As Boolean
If Age.HasValue Then
If Age.Value < 16 Then
Qualifies = False
Else
Qualifies = True
End If
Issues in Object-Oriented
Programming
Theres also a shorthand notation for declaring Nullable
types; just append a question mark to the variables
name as in the following statement:
Dim Age? As Integer
Issues in Object-Oriented
Programming
Exploring Reference Types
To better understand how reference types work, consider the
following statements that append a new row with two
subitems to a ListView control (the controls item is an object
of the ListViewItem type):
ListView1.Items.Clear
Dim LI As New ListViewItem
LI.Text = "Item 1"
LI.SubItems.Add("Item 1 SubItem 1.a")
LI.SubItems.Add("Item 1 SubItem 1.b")
ListView1.Items.Add(LI)
Issues in Object-Oriented
Programming
To actually remove the controls first item, you
must call the Remove method of the LI variable:
LI.Remove
This statement will remove the ListViewItem
object from the controls Items collection, but
the actual object still lives in the memory. If you
execute the following statement, the item will
be added again to the control:
ListView1.Items.Add(LI)
Issues in Object-Oriented
Programming
Properties versus Fields
The following statement invokes the Property Set segment
of the Email public property of the class:
cust.EMail = "[email protected]"
Issues in Object-Oriented
Programming
Properties versus Fields
Every time you call one of the class properties, the
corresponding public procedure in the class is invoked.
The following statement invokes both the Set and Get
Property procedures of the Customer class Balance
property:
cust.Balance = cust.Balance+429.25
Issues in Object-Oriented
Programming
Shared versus Instance Members
A shared property is common to all instances of the class.
In other words, theres no local variable for this property,
and all instances of the class access the same variable.
Shared methods, on the other hand, are quite common.
The Math class is a typical example. To calculate the
logarithm of a number, you call the Log method of the
Math class:
Math.Log(123)
Issues in Object-Oriented
Programming
Dim cust As New Customer
cust.GetCustomerByID("ALFKI")
Debug.WriteLine cust.CompanyName
Debug.WriteLine cust.ContactName & " " &
cust.ContactTitle
Issues in Object-Oriented
Programming
a class may expose a few shared properties if
all instances of the class should access the
same property value.
It may also expose a few shared methods, which
can be called through the class name if theres
no need to create an instance of the class in
order to call a method.
Issues in Object-Oriented
Programming
In extreme situations, you can create a shared
class:
All properties and methods of this class are
shared by default.
To make the most of objects, however, you
should create classes that are instantiated with
the New keyword and methods that manipulate
the current instance of the class.
Issues in Object-Oriented
Programming
Type Casting
- The data type used most in earlier versions of the
language up to VB 6 was the Variant (which was replaced
in subsequent versions by the Object type).
- A variable declared as Object can store anything, and any
variable that hasnt been declared explicitly is an Object
variable.
- When you retrieve an item from a ListBox control, for
example, you get back an object, not a specific data type.
Issues in Object-Oriented
Programming
Type Casting
- To use this object in our code, we had to convert it to a
more specific type, the Contact type, with the CType() or
DirectCastfunction. The same is true for an ArrayList,
which stores objects, and we usually cast its members to
specific types.
- Variables declared without a specific type are called
untyped variables.
Issues in Object-Oriented
Programming
Early versus Late Binding
- Untyped variables cant be resolved at compile
time; these variables are said to be late-bound .
Inheritance
Inheritance
The key word here is reuse: write once,
use many times.
Inheritance is a powerful concept in objectoriented programming that allows you to
build classes on top of existing ones.
You inherit the functionality of an existing
class and then add more functionality to it or
overwrite some of its base functionality.
Inheritance
Inheritance allows you to build hierarchies of
classes that better represent physical
entities, and it also enables you to reuse
existing code (the holy grail of
programming).
Inheritance is a technique for reusing and
improving code that doesnt cause the
applications that use it to break.
Inheritance
Inheritance allows you to build hierarchies of
classes that better represent physical
entities, and it also enables you to reuse
existing code (the holy grail of
programming).
Inheritance is a technique for reusing and
improving code that doesnt cause the
applications that use it to break.
Inheritance
Its possible to add new functionality to the
inherited code or even override some of
the existing functionality.
You can add new functionality to the code
by adding new members to the inherited
classes