Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Tip - alternative to conditional compilation.

4 views
Skip to first unread message

Steve Jorgensen

unread,
Sep 1, 2003, 3:05:55 AM9/1/03
to
To begin with an example...

Let's say you were wanting to write code usign early binding to the MSXML
library, but then be able to switch between early and late binding at will.
Conditional compilation is one possibility, but it's not practical.

1. You have to put a precompiler condition block around every Dim and
every function declaration that might need to change,
2. If the code spans multiple modules, the flags have to be changed in all
modules to make the switch.
3. Conditional compilation is reported to cause problems if you ever want
or need to use the /DECOMPILE switch.

It turns out you can better encapsulate what needs to be changed in cases
like this by wrapping an object variable in a user-defined data type, then
write all your code to pass around data using the object variable type, and
reference methods and properties via the element in the variable. It works
across module boundaries, the code change is small and only needs to be
made in one place in the code, and the compiler automatically takes care of
treating method/property references as early/late bound depending on the
custom type declaration.

Example:
--------
' Uncomment the following code to use a project reference to the MS XML
library.
'------
Public Type oaltXMLDocument
Doc As MSXML2.DOMDocument
End Type
Public Sub OALInitXMLDocument(oalXMLDocument As oaltXMLDocument)
Set oalXMLDocument.Doc = New MSXML2.DOMDocument
End Sub
'------

' Uncomment the following code to use late-bound references to the MS XML
' library and not require a project reference.
'------
'Public Type oaltXMLDocument
' Doc As Object
'End Type
'Public Sub OALInitXMLDocument(oalXMLDocument As oaltXMLDocument)
' Set oalXMLDocument.Doc = CreateObject("MSXML2.DOMDocument")
'End Sub
'------


- Steve Jorgensen

----
I would have written you a shorter program,
but I didn't have the time.

Steve Jorgensen

unread,
Sep 1, 2003, 3:15:35 AM9/1/03
to
Correction...

On Mon, 01 Sep 2003 07:05:55 GMT, Steve Jorgensen <nos...@nospam.nospam>
wrote:

>To begin with an example...
>
>Let's say you were wanting to write code usign early binding to the MSXML
>library, but then be able to switch between early and late binding at will.
>Conditional compilation is one possibility, but it's not practical.
>
>1. You have to put a precompiler condition block around every Dim and
>every function declaration that might need to change,
>2. If the code spans multiple modules, the flags have to be changed in all
>modules to make the switch.
>3. Conditional compilation is reported to cause problems if you ever want
>or need to use the /DECOMPILE switch.
>
>It turns out you can better encapsulate what needs to be changed in cases
>like this by wrapping an object variable in a user-defined data type, then
>write all your code to pass around data using the object variable type, and

- should read "using the user defined variable type" -

Terry Kreft

unread,
Sep 1, 2003, 6:15:03 AM9/1/03
to
Steve,
Now it makes sense, I was puzzled by your first posting.

Of course you could always wrap them up in your own classes as well.

Terry

"Steve Jorgensen" <nos...@nospam.nospam> wrote in message
news:0ks5lv8kc35rtui68...@4ax.com...

0 new messages