- The Fundamentals of VBScript
- VBScript Versus VB
- Creating Variables in VBScript
- Concatenating Strings
- Arrays and Loops
- Resizing Arrays
- Inequality Operators
- Conditional Statements
- Select Case Statements
- Working with Arguments
- Beware of Types
- Event Procedure Naming Syntax
- Server-Side Events
- Local Variables
- Script-Level Variables
Creating Variables in VBScript
There are two ways to create a variable in VBScript. One way is to just use the variable. This is called implicit declaration. The following example shows you how to create a variable implicitly.
<SCRIPT LANGUAGE=VBScript> i = 1 </SCRIPT>
The other way to create a variable in VBScript is to do so explicitly, using the Dim <keyword>. For example, to create a variable, FirstName, you write the following code within a set of <SCRIPT> tags:
<SCRIPT LANGUAGE=VBScript> Dim FirstName </SCRIPT>
Also, later on, after you learn to make classes using VBScript, you can create variables using the Public or Private <keyword>, such as:
<SCRIPT LANGUAGE=VBScript> Private InsideData Public OutsideData </SCRIPT>
Object-Oriented Variable Declaration
Please be advised that the Private and Public <keyword> come into play when you start doing advanced scripting using the <CLASS> tag. The <CLASS> tag denotes a class. A class is an object-oriented data type that you use to make objects.
Variables that are declared as Public can be used by functions outside of the class in which they are declared. Variables that are declared Private have visibility only within the class.
You assign a value to a variable by using the equal sign as shown in the following lines:
Dim s s = 2
Because all variables in VBScript are Variants, you can assign any type of data to a given variable. The variant data type is smart enough to figure out how to accommodate the assignment. For example, although you can create a variable and assign a number to it, you can just as easily assign a string value to it, as follows.
Dim s s = 2 s = "I was a number. Now I am a string"
Listing 3.5 declares a variable, FirstName, that receives the value that a user enters into a Web Page's textbox, Text1. When the user clicks the button, an Alert dialog box displays the name entered in the textbox twice.
Listing 3.5: Declaring a Variable with Dim <keyword> (03asp05.htm)
<SCRIPT LANGUAGE=VBScript> Sub button1_OnClick() Dim FirstName FirstName = text1.value Alert FirstName & " " & FirstName End Sub </SCRIPT>
Figure 3.4 shows the output of the code in Listing 3.5.
Figure 3.4 Variables add versatility to your Web Page.