473,503 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Type safety vs late binding and class polymorphism

I frequently find myself wanting to use class abstraction in VB/VBA code, and
frankly, with the tacked-on class support in VB/VBA, there are some problems
with trying to do that and have any type-safety as well. I thought I would
share some of what I've come to think about this after dealing with it several
times of late.

First, an example. Let's say I have several classes, each with a string
property called Name, and I have several cases in which I want to make VBA
collections of one or the other of these classes using each item's Name
property value its key in the collection.

For type safety, I could:

A) Make one function for each of these cases that takes an explicit object
type to add, but that's a ludicrous code duplication.

B) Create a common interface for these classes called, say INamed, but the way
VBA uses interfaces, for this to be reliable, I need 2 implementations of the
Name property, one for the class when referenced as its own type (Name), and
one for when referenced as an INamed type (INamed_Name), with one
implementaiton wrapping the other. That's a real mess, and it's a bigger mess
if more than one interface is involved.

Forgetting type safety:

C) Make one function to add any "named" class instance to a collection, but
have it use Object as the Item parameter type. This is late bound, so the
Name property must be looked up in the class definition for each call, and the
compiler can't tell you if you're trying to pass a non-named type.

In spite of the lack of type safety and the potential (but rarely significant)
performance issue, I've found that "C" is the most reasonable answer,
particularly if more than about 2 cases are involved.

Note that the use of automated testing can be a good substitute for type
safety in this case (much as double-entry accounting can be a good substitute
for or supplement to having Excel), but I realize that not many people will
ever do automated testing in VB/VBA. See http://timestream.net/learningxp/ to
follow the progress of some of us mavericks who do.
Nov 13 '05 #1
2 2266
rkc

"Steve Jorgensen" <no****@nospam.nospam> wrote in message
news:lf********************************@4ax.com...
I frequently find myself wanting to use class abstraction in VB/VBA code, and frankly, with the tacked-on class support in VB/VBA, there are some problems with trying to do that and have any type-safety as well. I thought I would share some of what I've come to think about this after dealing with it several times of late.

First, an example. Let's say I have several classes, each with a string
property called Name, and I have several cases in which I want to make VBA
collections of one or the other of these classes using each item's Name
property value its key in the collection.

For type safety, I could:

A) Make one function for each of these cases that takes an explicit object
type to add, but that's a ludicrous code duplication.

B) Create a common interface for these classes called, say INamed, but the way VBA uses interfaces, for this to be reliable, I need 2 implementations of the Name property, one for the class when referenced as its own type (Name), and one for when referenced as an INamed type (INamed_Name), with one
implementaiton wrapping the other. That's a real mess, and it's a bigger mess if more than one interface is involved.

Forgetting type safety:

C) Make one function to add any "named" class instance to a collection, but have it use Object as the Item parameter type. This is late bound, so the
Name property must be looked up in the class definition for each call, and the compiler can't tell you if you're trying to pass a non-named type.

In spite of the lack of type safety and the potential (but rarely significant) performance issue, I've found that "C" is the most reasonable answer,
particularly if more than about 2 cases are involved.


Don't you still have to write 'messy' code when you want to retrieve the
objects from the collection and determine what they are? Isn't the point
of a class to incorporate the 'mess' so it doesn't have to be delt with by
client code?



Nov 13 '05 #2
On Tue, 10 Aug 2004 11:13:01 GMT, "rkc" <rk*@yabba.dabba.do.rochester.rr.bomb>
wrote:

"Steve Jorgensen" <no****@nospam.nospam> wrote in message
news:lf********************************@4ax.com.. .
I frequently find myself wanting to use class abstraction in VB/VBA code,

and
frankly, with the tacked-on class support in VB/VBA, there are some

problems
with trying to do that and have any type-safety as well. I thought I

would
share some of what I've come to think about this after dealing with it

several
times of late.

First, an example. Let's say I have several classes, each with a string
property called Name, and I have several cases in which I want to make VBA
collections of one or the other of these classes using each item's Name
property value its key in the collection.

For type safety, I could:

A) Make one function for each of these cases that takes an explicit object
type to add, but that's a ludicrous code duplication.

B) Create a common interface for these classes called, say INamed, but the

way
VBA uses interfaces, for this to be reliable, I need 2 implementations of

the
Name property, one for the class when referenced as its own type (Name),

and
one for when referenced as an INamed type (INamed_Name), with one
implementaiton wrapping the other. That's a real mess, and it's a bigger

mess
if more than one interface is involved.

Forgetting type safety:

C) Make one function to add any "named" class instance to a collection,

but
have it use Object as the Item parameter type. This is late bound, so the
Name property must be looked up in the class definition for each call, and

the
compiler can't tell you if you're trying to pass a non-named type.

In spite of the lack of type safety and the potential (but rarely

significant)
performance issue, I've found that "C" is the most reasonable answer,
particularly if more than about 2 cases are involved.


Don't you still have to write 'messy' code when you want to retrieve the
objects from the collection and determine what they are? Isn't the point
of a class to incorporate the 'mess' so it doesn't have to be delt with by
client code?


That depends. In this case, each instance of the collection is only supposed
to contain one type of object instance. I could write a type safe collection
class for each one, and if I'm writing code that will be heavily reused, such
as a shared library, I'll do that. Otherwise, it's usually adequate to assume
the calling code is going to be looking at the rignt collection and know what
types of items are in it.

Alternatively, there may be a similar situation in reading as there is in
writing, that you have a function that can handle collections of any number of
different data types, so long as they have the expected, named members. The
dilemma is the same as above, and I would still usually choose option C.
Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
465
by: Siggy | last post by:
How can I trap this error -User-defined type not defined - in order to make custom error messages, when there is a missing reference, or missing dll/ocx file. Siggy
9
10391
by: Zlatko Matić | last post by:
I was reading about late binding, but I'm not completely sure what is to be done in order to adjust code to late binding... For example, I'm not sure if this is correct: early binding: Dim ws...
2
2419
by: Dave | last post by:
Hello all, I am creating a linked list implementation which will be used in a number of contexts. As a result, I am defining its value node as type (void *). I hope to pass something in to its...
2
3396
by: Max | last post by:
When you add an event handler to some object you need to specify the function to call when the event occurs. Suppose there is a class TesterClosed() which is called whenever the Tester is closed....
11
7746
by: Tubs | last post by:
i am attempting to write something which can morph itself to whatever comes in and get the value property from it but i don't know what type it is until runtime. I am therefore trying to use...
12
1528
by: Daniel Klein | last post by:
I think I've done my homework and checked around on Google Groups but this seems to be a situation not yet covered. Here is the scenario... There are two classes, Foo and Bar (actually there...
11
3527
by: JohnR | last post by:
I'm trying to find a way to create a variable of a given type at runtime where I won't know the type until it actually executes. For example, dim x as object = "hi" x is declared as an object...
4
2796
by: Rippo | last post by:
Hi I have the following console application and am attempting to late bind a class with option strict on! However of course I cant and I get the following error "Option Strict On disallows late...
0
160
by: Jeff Louie | last post by:
Robert.. You can use interfaces or an abstract base class to abstract out the functionality of the plug ins. You then program to the abstraction. In the case of an interface, you program to the...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7348
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5592
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4685
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3166
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.