The VBScript Network and Systems Administrator's Cafe

Apr 6 2008   12:17AM GMT

Variable types in VBscript and their upper and lower limits



Posted by: Jerry Lees
Tags:
Development
Variable Types
VBScript

I just realized that the direction I’m headed with this blog (atleast a few posts from now) requires that I give you some more background information beyond what I’ve already done.

 Previously, I’ve talked about variables and given you a few types in passing– but never detailed the different types that are available to you as a script writer.

To that I say, “VBscripts default variable type is a Variant, which can hold any type of value you need to put in it without any work at all on your part.”

“WAIT?!?!!”, you say, “Here you are harping on us to be lazy and now you’re about to go through a big posting on variable types when the one you just mentioned should work fine for me!??”

Yes, a variant will work just fine for you for the most part. But it does do some funny things with variables because under the hood it does the conversion for you. Some even say using variants slows things down. Sometimes in VBscript you need to interface with an external COM dll or some other program and the other variable types will be needed because the call to the DLL will break if your variables aren’t properly cast into a type. Also, variants do funny things to numbers… take this simple example.

x = 2.0000
WScript.Echo x

 When you run this it simply prints:

2

No, No, No! I set x to 2.000 not 2. You might be thinking that 2.000 = 2 from your math days and you’d be right except some of your science folks out there will agree that sometimes you need to communicate precision as well. In science, 2.000 communicates that you’ve MEASURED something to the thousandth place to be exactly 2.000. Frankly, that can sometimes take work and if you’re lazy and you do all that work you want credit for it, right? (See how I brought that ramble back full circle? Be lazy!)

Also, It does some funky things with variables you’ve never set to a value as well. Since Variants are just that– you can use them for strings, like for a URL “http://www.360voice.com/” and numbers like 100. No problem, right? Again… Possibly it’s a problem… Consider this scenario in your script (with nothing else in the script): 

WScript.Echo y
WScript.Echo y + 1

This prints the following (between the _ characters below on two different lines to show you what happens):

_

1
_

See it? The first echo of y appears blank– yet when I add it to the number 1, I get 1!  That’s because if you don’t put a value in a variant variable it treats it is 0 if it is used in the context of a number and as an empty string (or NULL (Not “”, or the empty string) for you C/C++ programmers out there) if you use it in the context of a string. Big difference huh? Same Y, but two different values– empty and 0! (On a side note, think about empty with respect to a file’s contents– Empty takes no space at all and 0 (zero) takes a whole byte in a file.)

Now that you see some slight differences, consider the following types of variables in the table below: 

Subtype Description
Empty Variant is uninitialized. Value is 0 for numeric variables or a zero-length string (“”) for string variables.
Null Variant intentionally contains no valid data.
Boolean Contains either True or False.
Byte Contains integer in the range 0 to 255.
Integer Contains integer in the range -32,768 to 32,767.
Currency -922,337,203,685,477.5808 to 922,337,203,685,477.5807.
Long Contains integer in the range -2,147,483,648 to 2,147,483,647.
Single Contains a single-precision, floating-point number in the range -3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values.
Double Contains a double-precision, floating-point number in the range -1.79769313486232E308 to -4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values.
Date (Time) Contains a number that represents a date between January 1, 100 to December 31, 9999.
String Contains a variable-length string that can be up to approximately 2 billion characters in length.
Object Contains an object.
Error Contains an error number.

All these number types… I’m betting your head is swimming if you haven’t encountered this before. Have no fear… we’ll go over them more a little later. Atleast, for now you see what I’m talking about. Next time, I’ll go over conversion functions to convert number types.

Extra Credit: Think about if I had a payroll script I was working on– and you were actually going to get paid by it. You worked 40.5 hours (or if your salary your check should be $1201.62) Would you mind terribly if I fed this to the payroll department after converting these numbers to an Integer first?

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: