473,545 Members | 2,011 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 2267
rkc

"Steve Jorgensen" <no****@nospam. nospam> wrote in message
news:lf******** *************** *********@4ax.c om...
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.dabb a.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
10400
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 As DAO.Workspace Dim db As DAO.Database Dim qdf As DAO.QueryDef Dim rs As DAO.Recordset
2
2424
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 "constructor" so that I will be able to manipulate my list without the need for constant casting; some sort of runtime type-safety mechanism. For...
2
3399
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. Code below: public void TesterClosed(){} Tester m_tester = new Tester(); m_tester.Close += new Tester.CloseEventHandler(TesterClosed); Now...
11
7749
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 reflection and get the type from there and then cast it to that type so i can use it. When i use DirectCast, it won't let me do something like...
12
1531
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 are more than two classes involved but two will suffice to explain the problem), and each class has a Copy() method as follows: Public Function...
11
3535
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 but x.gettype returns 'string', so it knows it contains a string. If I want to create a variable "y" as the same type that variable x contains...
4
2801
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 binding" at the line BL.a() in method Main How do I overcome this? Public Class test1
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 public view, the interface. The interface defines a contract between the user of your plug in class and any concrete implementation of the class....
0
7664
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7921
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7437
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7771
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4958
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1900
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1023
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.