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

VBScript classes, singletons and Class_Terminate

100 views
Skip to first unread message

A Ratcliffe

unread,
Aug 7, 2003, 8:49:02 AM8/7/03
to
Reposted here from microsoft.public.inetserver.asp.general (Bob Barrows
suggested I'd be more likely to get a response here)

As you might have gathered from the Subject, I've been experimenting with
using VBScript classes in my ASP pages to tidy the script up. Generally
things are going well.

Since I come from a C++ background, I have long made use of singleton
objects in various ways, and I can see an obvious use for one here but not
sure if I can. I'll explain the situation:-

I have a cDataLink class, configured via a couple of Const defines for the
Provider type, database name etc (So I can easily set it according to
Access/SQL etc). In addition, all my other classes use 'lazy loading', only
going to a SQL SELECT to get information if they are asked for that data. To
create the cDataLink object, I can see two possibilities.

a) I can create it at the beginning of every page. Setting the object to
Nothing at the end will (I assume) call its Class_Terminate() and allow it
to clean up.

Although this would work, it means that each page will open the data
connection regardless of whether the other objects actually need it. It goes
against the grain of only opening a connection if you have to, and then at
the latest possible moment, especially if it then isn't required for that
particular parsing of the page.

b) Using the abilities provided by a well designed Singleton, the first
object to require the connection would create the cDataLink object by the
very nature of asking for it. Any data connection requests after that point
would return the existing connection object.

The obvious benefit of this is that if no class instance requests the
connection, it will never be created. One problem I foresee is in the
termination of the connection. Unless I actively Set the connection to
Nothing at the end of the page, I'm not sure if I can garantee that the
connection object will be terminated and released. This goes against the
design of a Singleton, which lingers uniquely just as long as it is required
and then goes.

c) Possibly I could do something based round Reference counts, like a COM
object, but that may be code for coding's sake.

On to my questions:-

a) Am I right to worry that a VBS class is not garanteed to call its
Terminate unless it is set to Nothing somewhere, or is it safe to assume it
will vanish when the page has finished parsing, running its Terminate() in
the process.

b) I have yet to find any sort of 'static' declaration for VBS classes
(although I may have been looking in the wrong place). This is almost
obligatory for creating a true Singleton type of object, since its one of
the tricks for ensuring that you will always the same object back.

Basically I'm asking if it is actually possible to create a Singleton class
in VBS (via 'proper' techniques, or by tricks/cheats), and if it is possible
am I stepping into a world of hell trying to accomplish it?

Yours,

A Ratcliffe
aratc...@archimagic.net

P.S. Sorry about the verbiage. Hazard of the trade at times when you are
trying to describe something.


Walter Zackery

unread,
Aug 7, 2003, 12:15:45 PM8/7/03
to
"A Ratcliffe" <aratc...@archimagic.net> wrote in message
news:egZvAJOX...@TK2MSFTNGP12.phx.gbl...
<SNIP>

> a) Am I right to worry that a VBS class is not garanteed to call its
> Terminate unless it is set to Nothing somewhere, or is it safe to assume
it
> will vanish when the page has finished parsing, running its Terminate() in
> the process.

A VBS class will call its Class_Terminate method once it goes out of scope
either implicitly or explicitly. One exception to this is if the parent
calling script abruptly terminates because of an unhandled error.

> b) I have yet to find any sort of 'static' declaration for VBS classes
> (although I may have been looking in the wrong place). This is almost
> obligatory for creating a true Singleton type of object, since its one of
> the tricks for ensuring that you will always the same object back.
>
> Basically I'm asking if it is actually possible to create a Singleton
class
> in VBS (via 'proper' techniques, or by tricks/cheats), and if it is
possible
> am I stepping into a world of hell trying to accomplish it?
>

Unfortunately, you can't create static classes or static methods in
VBScript. However, see below for a workaround....

Here's an example of a simple class that creates a FileSystemObject. The
class ensures that all instances of the FileSystemObject created from the
same class instance or from any new class instances will reference the same
object.

Option Explicit
Dim x
Dim y
Dim z
Dim a
Dim b
Set x = new Class_FSO
set y = x.FSO
set b = x.FSO
set z = new Class_FSO
set a = z.FSO
msgbox CStr(y Is a) & " " & CStr(y Is b) & " " & CStr(a Is b)
Class Class_FSO
Private FSO_

Public Property Get FSO
On Error Resume Next
If Typename(FSO_) = "FileSystemObject" Then
Set FSO = FileSysObj
Else
ExecuteGlobal "Const Class_FSO_IsRunning = 1"
If Err = 0 Then
ExecuteGlobal "Set Class_FSO_RunningInstance = Me"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set FileSysObj = FSO
Else
Set FSO = Class_FSO_RunningInstance.FSO
Set FileSysObj = FSO
End If
End If
End Property

Private Property Set FileSysObj(obj)
Set FSO_ = obj
End Property

Private Property Get FileSysObj
Set FileSysObj = FSO_
End Property
End Class


Joe Earnest

unread,
Aug 7, 2003, 12:59:22 PM8/7/03
to
Hi,

A couple of related suggestions that you may want to look at until someone
else comes along with more experience in what you're doing with an ASP host.
Bob's posts are very knowledgable, so if he suggested a repost, you may want
to wait for someone who's tried this.

[snipped and interlaced]


"A Ratcliffe" <aratc...@archimagic.net> wrote in message
news:egZvAJOX...@TK2MSFTNGP12.phx.gbl...

> I have a cDataLink class, configured via a couple of Const defines for the


> Provider type, database name etc (So I can easily set it according to
> Access/SQL etc). In addition, all my other classes use 'lazy loading',
only
> going to a SQL SELECT to get information if they are asked for that data.
To
> create the cDataLink object, I can see two possibilities.

> The obvious benefit of this is that if no class instance requests the


> connection, it will never be created. One problem I foresee is in the
> termination of the connection. Unless I actively Set the connection to
> Nothing at the end of the page, I'm not sure if I can garantee that the
> connection object will be terminated and released. This goes against the
> design of a Singleton, which lingers uniquely just as long as it is
required
> and then goes.
>
> c) Possibly I could do something based round Reference counts, like a COM
> object, but that may be code for coding's sake.
>
> On to my questions:-
>
> a) Am I right to worry that a VBS class is not garanteed to call its
> Terminate unless it is set to Nothing somewhere, or is it safe to assume
it
> will vanish when the page has finished parsing, running its Terminate() in
> the process.

You might want to take a look at this thread, (discussion by several
knowlgable posters and implementation code by Michael Harris) dealing with
self-termination of a WSC through internally defined classes, and see if it
contains anything useful for you. (URL will wrap and need to be
reconstructed.)

http://www.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=emn%23Zku5%
24GA.134%40cppssbbsa05&rnum=3&prev=/groups%3Fas_q%3Dwsc%2520terminate%2520cl
ass%26safe%3Dimages%26ie%3DUTF-8%26oe%3DUTF-8%26as_ugroup%3Dmicrosoft.public
.scripting.*%26as_uauthors%3Dharris%26lr%3D%26hl%3Den

The essence (from another Michael Harris post) is that

---
Script based WSCs and VBScripts classes don't support true
constructors/destructors. ... Unlike VBScript classes, WSCs have no
explicit initialize or terminate events for which you write handlers. But
unlike the VB/VBScript counterparts, WSCs allow you to write global
executable code outside of a procedure. That let's you do the equivalent of
the initialize processing, but not the terminate.

A rather clever workaround that someone posted a few months ago (I don't
recall the author or I
would give him full credit) is to encapsulate WSC class-level resources in a
private VBScript class
defined and used only inside the WSC. When the WSC is created, create and
initialize an instance of
this private "resource wrapper" which fires it's Class_Initialize() handler.
When the WSC goes out
of scope and is destroyed, so does the "resource wrapper" instance which
fires it's
Class_Terminate() handler.
---

> b) I have yet to find any sort of 'static' declaration for VBS classes
> (although I may have been looking in the wrong place). This is almost
> obligatory for creating a true Singleton type of object, since its one of
> the tricks for ensuring that you will always the same object back.
>
> Basically I'm asking if it is actually possible to create a Singleton
class
> in VBS (via 'proper' techniques, or by tricks/cheats), and if it is
possible
> am I stepping into a world of hell trying to accomplish it?

AFAIK, there is no Static declaration implemented in any aspect of VBS. (It
*is* a reserved keyword, but several of those are VB carryovers and are not
implemented.) Michael Harris has posted the use of an invisible IE window
as a singleton data and script repository (he calls it an "IE pipe"), which
can be located through the windows collection. (URL will wrap and need to
be reconstructed.) If I understand your question correctly, however, I
doubt that this will get you where you need to go.

http://www.google.com/groups?q=+%22iepipe%22+group:microsoft.public.scriptin
g.*+author:harris&hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=uQBI5xyCCHA.2656%40tkmsft
ngp05&rnum=2

Actually, now that you've opened the subject, a singleton WSC could be very
useful, ...

Joe Earnest

Bob Barrows

unread,
Aug 7, 2003, 1:09:13 PM8/7/03
to
Joe Earnest wrote:
> Hi,

>
> Bob's posts are very knowledgable,

Thanks for that Joe. The reason for that, of course, is that I avoid
answering posts about which my knowledge is limited (ahh, the secret is
out!).

In this case, I suggested the repost because:
1) my knowledge of this subject is limited,
2) it wasn't getting much response on the asp.general group, and
3) I knew I could count on you and Michael to chime in (Michael has, albeit
by proxy ...)

Very interesting reading from all three of you.

Bob Barrows


A Ratcliffe

unread,
Aug 7, 2003, 2:31:36 PM8/7/03
to
Thanks to everyone for such quick responses. Following on from all your
suggestions, I have something I can experiment with.

I look forward to checking back here regularly,

Yours,

Ann-Marie Ratcliffe
aratc...@archimagic.net

"Bob Barrows" <reb_...@yahoo.com> wrote in message
news:exPDFcQX...@TK2MSFTNGP10.phx.gbl...

0 new messages