VBScript variable types.

joshg

Golden Member
Jul 3, 2001
1,359
0
0
In VBScript (I.E. using WSH or ASP, not VB or VBA...) is there any way that you can "overwrite" the global variable type of Variant into something else, such as Long or Int?

The reason I ask is that, yes though it is designed to be "easy to use" by not having to declare variable type, the performance suffers tremendously when performing loops or doing a lot of mathematical computations.

In VB and VBA you can declare a variable as such:

Dim i as Integer

But in VBScript, you can only declare it like...

Dim i

Once you try to declare type, it throws an error (expected end of statement).


Just am curious...

Thanks! :)
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
Nope, everything is of type VT_VARIANT; however, you can have "subtypes" which include VT_BOOL, VT_BSTR, VT_DATE, etc. You can convert a variant to one of these subtypes using conversion functions like CBool, CStr, CDate, respectively.

You might also want to consider writing that more computationally expensive facets of your project in a COM component with an IDispatch interface implementation for late-binding clients like ASP and VBScript. Visual Basic does this for you automatically (called a "dual interface"). Unfortunately, the overhead of COM would probably negate the very small gains you'd receive from removing the use of variants.

If you're doing a lot of mathematical operations, you probably don't want to be doing them in VB(Script). Consider creating a COM component in C++ to encapsulate these for you.
 

joshg

Golden Member
Jul 3, 2001
1,359
0
0
Yes I've been thinking the same thing...

However they seem to not have any type of C-based compilers available for use here (at least without putting in a purchase order for a few licenses...). Yay for corporate America, right? ... :frown:

Anyone know of a good Win32 compatible freeware C++ compiler? :D - edit: that installs independently into its own directory ;) Since I don't have permission rights to install most software...
 

joshg

Golden Member
Jul 3, 2001
1,359
0
0
Well, I just messed around a bit, and...

Running in WSH with all variables as Variant (but converted to Long with CLng() ), the calculation in question takes around 1:35 to complete...

Conversely, just plugging the same calculation into VB and having it run with variable types set at variable declaration, run time is between 17 and 19 seconds.

... A significant difference. ;)