Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Are vbscript classes fast?

Status
Not open for further replies.

atwork8

Programmer
May 3, 2008
3
GB
Hi there,

Just found these forums and my first post, so hi all :eek:)

I'm doing some work with vbscript just now and had a quick question I hope somebody could help with...

Say I have a few functions for accessing a database e.g. openDBConn, closeDBConn, openRs, closeRs, getRsAsArray, executeSql, getDbConnString etc

I could put these functions in to an include file "incDbHelper.asp" and include the file in any page that needs database functionality... or I could create a class "clsDbhelper.asp" which again I would include in any page that needs it. Now once the files have been included, to access the functions I could do:

executeSQL("select * form table")

or

(if my db class is called Database)

set db = new Database
db.executeSQL("select * form table")

So my question is what is the best method? Using a class seems like extra overhead for nothing? I've had a search around google but couldn't find anything on performance issues when using classes. Does anybody have an experience they could help me out with?

Thanks in advance for any help :eek:)
 
They aren't any faster but it helps to section off the code. Variables and methods belong somewhere. All the variables used by the class are local to the class. What's nice is that if, at a later date, you need two or more instances, it is a lot easier.

Also it is reusable. I keep a collection of classes which I pull in whenever I write programs. You could keep a bunch of routines and global variables but you might get a name clash. With classes, it is not so likely.

It is a personal thing. I prefer classes because I've been using classes since I started using C++ in 1990. I can live without them but I just prefix all the routine names so I know which group they belong to.
 
Normally you write a class in order to encapsulate something like a data structure with common methods for operating on it. It isn't meant as a bag of subroutines.

If you find yourself:
[ul][li]Modifying the class in every script that uses it,[/li]
[li]Passing tons of parameters in and out because the data doesn't "live inside" the class, or[/li]
[li]Creating only a single instance in scripts where you use it...[/li][/ul]
... well, it probably wasn't a good candidate for a class after all.

There is nothing stopping you from doing it anyway though.

Most people I see using classes in script really just want a reusable bag o' subs and fucntions. This is often achieved more cleanly by using an external script block containing those routines, that you include into other scripts that use those routines.

Of course for WSH that means stepping up from the naked .VBS file to using .WSF files instead. This gives you a clean way for a script to include individual script block files, even mixing VBScript and JScript in one script. The WSH documentation covers all of that. Reference (Windows Script Host)
 
Classes are great. Create a PERSON class, and many apps can use it. you can modify the PERSON class to add to it, and even more people can use it.

When a method changes, you can change the PERSON class, and it will be fixed automatically everywhere else.

-David
2006, 2007 & 2008 Microsoft Most Valuable Professional (VB)
2006 Dell Certified System Professional (CSP)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top