
Problem: Default Properties
Many VB 6.0 objects define a property to be the default property. Although convenient, it does have some
drawbacks. It can make the code harder to read if the default property is not well known. It forces the use of the
Set keyword when assigning object references to distinguish reference assignment from setting the default
property.
'VB 6.0 Code
Dim T1 As New TextBox, T2 As New TextBox
T1 = "Homer" 'TextBox's default property is Text.
T2 = T1 'Replace T2's Text with T1's
Set T1 = T2 'Set keyword needed to replace the T1 object reference
'with a reference to T2.
Assume you have authored the following VB 6.0 code.
'VB 6.0 Code
Dim obj1 As CMyObject, obj2 As CMyObject
obj1 = New CMyObject 'Illegal, Set required
obj2 = obj1 'Illegal, Set required.
The previous code will result in the dreaded runtime error 91. How many times have you seen this VB 6.0 error
dialog?
VB restricts the use of default properties. Only properties that take a parameter can be set as the default. For
example, the Collection.Item property takes an Index value as a parameter. This requirement removes the
ambiguity between setting a default property and assigning an object reference.
Therefore, the Set keyword is no longer needed and has been removed. Also notice in the following code that VB
allows you do define multiple variables of the same type on a single line of code. This is not the case in VB 6.0,
where obj1 would be implicitly set to a Variant.
'VB code
Dim obj1, obj2 As SomeClass
obj1 = New SomeClass 'Ok in VB !
obj2 = obj1 'Ok in VB !
To set a property as the default, simply mark the property with the Default keyword within the class
definition.
' VB code
Public Class CustomCollection
Public Default Property Item(index As Integer) As SomeClass
'Implement property
End Property
End Class
Problem: No Implementation Inheritance
VB 6.0 does not support implementation inheritance. Inheritance is one of the three "pillars of object oriented
programming", along with encapsulation and polymorphism. Under VB 6.0, you can simulate inheritance by
employing the containment and delegation technique. However, this is just a workaround, not a solution. True
inheritance allows a new class to be based upon and extend an existing class.
VB provides inheritance using the new Inherits keyword and several other new supporting keywords. With
inheritance the derived class can:
• Inherit the functionality of the base class as is.
• Modify the base class functionality by overriding base class methods.
• Add functionality by adding new methods.
We will examine all the details of OOP in the next chapter, however for the time being, consider the following
code. Employee is the base class in our system.
'VB code
Public Class Employee
Private mName as String
Public Property Name() as String
'Implement Name prop...
End Property
End Class
WageEmployee derives (or extends) the Employee class. The Inherits keyword is used to establish a
base/derived class relationship.
Public Class WageEmployee
Inherits Employee
Private mWage as Double
Private mHours as Double
Public Function ComputePay() as Double
Return mWage * mHours
End Function
End Class
Because WageEmployee extends Employee, WageEmployee objects support all of the public members in the
parent. Therefore, the following code is allowed. Recall, the Name property was defined in the parent class. The
next chapter will walk you through all of the details of OOP.
' The Name property was defined in the parent class.
Dim wage As New WageEmployee()
wage.Name = "Homer Simpson"
Problem: Limited Threading Support
VB 6.0 leverages the COM runtime to provide support for multithreaded environments. However, VB 6.0 does
not provide a mechanism to spawn and control new threads. Various hacks and kludges have been documented
that work around this. All are difficult and dangerous to implement. Still, multithreading can be used to increase
an application’s responsiveness.
VB can use the System.Threading namespace to create and manage threads of execution. This opens a whole
new universe of programming to VB developers. It is your responsibility to resolve concurrency and
synchronization issues. These are tough problems, so use multithreading only where the extra cost of
development is warranted.
Here is a simple example of creating and starting a new thread of execution. The StartHere() method will execute
on a second thread. We are printing out the ID of the current thread in both Main() and StartHere(). Because
two threads are used in the background, we should see two ID values.
'VB code
Imports System.Threading
Public Class MyThreadTest
Public Sub StartHere()
Console.Write("MyThreadTest.StartHere() is running in thread: ")
Console.WriteLine(Thread.CurrentThread.ManagedThreadId)
End Sub
End Class
Module SimpleThreading
Sub Main()
Dim test As New MyThreadTest()
Console.WriteLine("*** Beginning Thread Test ***")
Console.WriteLine("Main Thread: " & _
Thread.CurrentThread.ManagedThreadId)
Dim oThread As New Thread(AddressOf test.StartHere)
oThread.Start()
Console.ReadLine()
End Sub
End Module
When this code runs, you should see something like the following. Your thread IDs will most likely differ.
This class will not examine the details of multithreaded programming using VB. However, the .NET SDK
documentation provides through coverage of this topic. Consult the documentation if you are interested.
Copyright (c) 2008. Intertech, Inc. All Rights Reserved. This information is to be used exclusively as an
online learning aid. Any attempts to copy, reproduce, or use for training is strictly prohibited.
VB Default Properties, Inheritance, Threading
Table of Contents
Courseware
Training Resources
Tutorials