4.1.2- Data Types in VBScript, or One Type For All
by NT Community Manager.
|
| << 4.1.1- Creating a Variable | Chapter4 | 4.1.3- Naming Variables >> |
Data Types in VBScript, or One Type For All
In most programming languages, it is common to specify a data type for a variable, so that your system knows what type of variable it's dealing with. However, in VBScript this is not the case. In VBScript, all of the variables with seemingly different data types are actually only one type. This is because VBScript determines the data type for you. The variable type that VBScript uses to store the information in is the variant. A variant is a special type of variable, which can store a value of any type. This means that you don't have to specify a data type for a variant when you declare it as you do in most other programming languages. (In fact, you can't specify a data type in VBScript – you'll get an error.) At the moment that the variable is read, VBScript will self/itself assign a particular type, based on the data and on the context.
This does have a couple of downsides, there is a performance hit, and sometimes VBScript serves up a different type for the data than you want. However, you can normally go far by just using the variant and letting it automatically assign a type – it usually works. Hence, you could declare a variable like this:
Dim LengthOfAPieceOfString
Then you could assign an integer value to it:
LengthOfAPieceOfString = 5
Later on, you might decide that you want to replace this value with a string value:
LengthOfAPieceOfString = "Not very long"
This means that we need a mechanism for keeping track of the type of data that is being stored in each variant. VBScript assigns a subtype to the variant that reminds the variant of the type of data being stored. A subtype can be any of the following.
Numeric Subtypes
You can assign almost any number to a variable. We can assign whole numbers, fractions, and even negative floating pointing numbers:
IntegerNumber1 = 76
DecimalNumber2 = 2.5356
FloatingPointNumber3 = -1.4E06
Basically, we use floating point numbers to represent very small or very large decimals such as 0.00000123 or 1.14E-6 or 1.87x10 -6.
In VBScript, there are five different numeric subtypes, which are outlined below.
Integer
Integers are simply whole numbers. Examples of integers are 3, 12, and -5127. The integer data type can handle whole numbers within the range -32,768 to 32,767. For numbers that are outside this range then the long type is used, which we'll see shortly.
Byte
Bytes are integers within the range 0 to 255. They're used for very basic arithmetic. It's a useful type, because the method in which data is made available means that a variable can be easily stored by the computer within a single byte, which is the computer's basic storage unit. As VBScript uses variants, VBScript requires an extra byte on top of this, but the principle of bytes taking up very little memory space remains the same.
Long
The long type is very similar to the integer type, but supports a much larger range. A long variable can contain a value in the range -2,147,483,648 to 2,147,483,647.
Single
The single type can hold single precision floating point numbers, within the range
-3.402823E38 to -1.401298E-45 (for negative values), and 1.401298E-45 to 3.402823E38 (for positive values).
Double
The double type holds double precision floating point numbers. This means that it will support a much larger range than the single type. In reality, this range is -1.79769313486232E308 to
-4.94065645841247E-324 (for negative values), and 4.94065645841247E-324 to 1.79769313486232E308 (for positive values).
Currency
The currency subtype accepts numbers with up to four decimal places. It can support a range of -922,337,203,685,477.5808 to 922,337,203,685,477.5807.
String Subtype
Variants with the string subtype hold textual information or words. The string subtype will identify everything you store in it as text, even if you supply it as a mixture of text and numerical data, numerical data alone or even date information. For example, the following code creates a variant called CarType, with the value "Buick", a variant called CarEngineSize, with the value "2.0", and a variant called DatePurchased, with the value "July 4, 1999":
CarType = "Buick"
CarEngineSize = "2.0"
DatePurchased = " July 4, 1999"
String values are usually put in between double quotation marks, so that they can be differentiated from numerical values. Note that you can't perform mathematical functions on strings, even if the content of the strings involved are purely numerical. Hence, if you try to add the two strings "12" and "14" together, as shown in the following example, you won't get the result "26" that you might have anticipated:
Number1 = "12"
Number2 = "14"
Number3 = Number2 + Number1 'Will produce "1412"
This is because while the string subtype itself seemingly accepts different types of data, it is only holding textual representations of these types, such as Integer or Date. Therefore, while it might appear to you that Number1 contains a number, the presence of quotation marks indicates that this is to be treated as text.
We'll cover the process of joining, or concatenating, strings later in this chapter. VBScript also provides a number of special functions with which you can manipulate strings. These functions allow you to measure the length of a string, truncate a string at the beginning or end, return certain characters from a given string, or even convert a string into its numerical equivalent. We'll look at string manipulation functions later in . A basic rule of thumb is that strings are normally used for storing words or textual information – numeric information is normally stored in the appropriate numeric subtype. The exceptions to this rule are numbers such as telephone numbers and social security numbers, which are usually better stored as strings.
Date Subtype
VBScript also supports a subtype, date, that can be used to hold dates and times. Variants of this subtype use a predefined format. The same subtype is used to record both date and time. For example, we can use the variant DateTime to store a given date:
DateTime = #12/10/2001#
A date must be surrounded by the # symbol (and not the " symbol): if it isn't surrounded by any symbols, it will be evaluated as a numeric expression. (If it were surrounded by "" then it would be interpreted as a string.) Later in the same program, we could use the same variant to store a given time:
DateTime = #11.03#
The predefined format of the date type prevents 'regular' numerical arithmetic from being carried out on variants of subtype date. Once the computer is aware of their special format, it's possible to carry out simple numerical arithmetic on them. For example, if you subtracted July 20, 2001 from July 24, 2001, you'd get the answer 4.
Boolean Subtype
Boolean variants can be set to one of two values, namely TRUE or FALSE. In VBScript, if you convert these values to the integer type then they convert to the values –1 and 0 respectively. They can be used to record the state of the variable, in that if a variable isn't TRUE then it must be FALSE. They can be set when a user performs a certain action, i.e. runs a certain form, and they can then be used to determine a certain course of action:
blnVariant = FALSE
If blnVariant = FALSE Then do this
...
Else do that
...
Note that TRUE or FALSE should not be in quotes.
Special Subtypes
We should briefly outline some other subtypes here.
Empty
Empty variants are variables that have yet to be assigned a value (known as uninitialized variables). Note that you shouldn't confuse these variables with variables that have been assigned the value 0 (zero)! This is because 0 is a valid value (which also implies that the variable is likely to be a numeric type or a Boolean FALSE), while an empty variable has no value at all (because no value has yet been assigned to it).
NULL
NULL is an unusual subtype that is used in conjunction with databases, when we talk about fields that contain no data. NULL does not mean 0, or empty, it means that there is no data, nothing. The concept of NULL is a big stumbling block for the experienced programmer and novices alike.
There is also a set of things that NULLs are most commonly confused with. These are things that most definitely are not NULL:
- Zero
- A blank string
- A blank space
- A string of length zero
None of these values are "nothing" – they are all "something". Since NULL has no data type or value, it is only a NULL if it is nothing, contains no data, and no data type.
Object
The Object subtype refers to objects, which are block of codes, which can carry out jobs such as accessing a database or keeping track of advertisements. Since objects are more complex than simple data they are treated in a special way. We won't worry about this just yet, as we'll be taking a closer look at them in Chapter 6.
Error
The Error subtype is very rarely used and for that reason we're not going to cover it. Occasionally you'll come across a component or function that uses it, but we don't recommend using it in any circumstances, as there are no conversion functions that allow you to create or convert a variant with the Error subtype.
Determining Subtypes with TypeName()
So, if VBScript treats all these different subtypes as basically the same type, how can we find out which variant is of which subtype? Sometimes it is useful for you, as the programmer, to know the subtype of a variant, and we can find this out by using the VBScript function TypeName(). If you feed a variant into the TypeName() function, as follows:
LengthOfAPieceOfString = 5
WhatTypeOfVar = TypeName(LengthOfAPieceOfString)
Then depending on what you placed in LengthOfAPieceOfString then one of the following text descriptions of variant types is returned:
|
Empty Null Integer Long Integer Single Double Currency Date String Object Error Boolean Variant Byte Array |
If some of these variant sub types are unfamiliar to you, don't worry: we'll be looking at arrays later in this chapter, objects in Chapter 6 and Data Access objects in Chapters 12 to 14.
We placed the number five in this example, so integer is returned. We'll now take a quick look at how you can use TypeName() to return the subtype that VBScript is storing for a particular variant.
Try It Out – Using TypeName to return a Variant Subtype
1. Start up your favorite ASP editor and type in the following:
<HTML>
<HEAD>
<TITLE>Using TypeName</TITLE>
</HEAD>
<BODY>
<%
Dim dblPi, varWhatIsPi, datToday, whatIsDate, strText, whatIsText
dblPi = 3.142
varWhatIsPi = TypeName(dblPi)
datToday = Date
whatIsDate = TypeName(datToday)
strText = "Hello World"
whatIsText = TypeName(strText)
Dim emp
emptyVar = TypeName(emp)
%>
<P><B> dblPi returns <%= varWhatIsPi %></P>
<P>datToday returns <%= whatIsDate %></P>
<P>strText returns <%= whatIsText %></P>
<P>emp returns <%= emptyVar %></B></P>
</BODY>
</HTML>
2. Save it as TypeName.asp
Run this in your preferred browser.
|
|
How It Works
This example is very simple. We dimension four variables and assign them values with different subtypes, and four corresponding variables to record the subtypes of the variables. We start by declaring all of the variables we're going to use and then we allocate the value of pi, 3.142, to the variable dblPi. Then we create a variable varWhatIsPi, which contains the TypeName value (of type string) of the dblPi variable:
<%
Dim dblPi, varWhatIsPi, datToday, whatIsDate, strText, whatIsText
dblPi = 3.142
varWhatIsPi = TypeName(dblPi)
We then assign a variable datToday with today's date (provided by the VBScript Date function), and then assign TypeName(datToday) to the variable whatIsDate:
datToday = Date
whatIsDate = TypeName(datToday)
We assign a string value to the variable strText, and then assign TypeName(strText) to the variable whatIsText:
strText = "Hello World"
whatIsText = TypeName(strText)
We then declare a variable emp, but we don't actually assign it a value:
Dim emp
emptyVar = TypeName(emp) %>
Finally we return the four TypeName values in the ASP:
<P><B>dblPi returns <%= varWhatIsPi %></P>
<P>datToday returns <%= whatIsDate %></P>
<P>strText returns <%= whatIsText %></P>
<P>emp returns <%= emptyVar %></B></P>
When passed to the function TypeName, dblPi returns double, datToday returns date, strText returns string and emp returns empty. This confirms the fact that when you allocate data to a variable, VBScript also assigns a subtype to the variant. The subtype of the variant can be determined by using the VBScript TypeName function.
| << 4.1.1- Creating a Variable | Chapter4 | 4.1.3- Naming Variables >> |

RSS


