Html Display Vb Script Variable Assignment

Variables form the basis of programming. Variables are used to hold a value or an expression.

Whenever you have a piece of data to work with, you will have to declare a variable.

For example, if you have to store names of students or salaries of employees, you will be using variables named students or salaries.

Variables can also be used for holding expressions.

Suppose you have stored the marks of a student in English and Mathematics using the variables markE and markM.

You want to find the total marks. Then, you can use a variable named markT and set its value to markE + markM. In other words, markT = markE + markM. Here, markT is a variable which holds an expression.

In this tutorial, you will learn-

Declaring Variables

Declaring variables is same as creating variables because you are instructing the computer to reserve memory space.

You can name the variable the way you want.

It can be short names like x, y or z or more self-describing names like student, Name, salary etc.

Providing clear and meaningful names to variables is considered as a good programming practice.

There are certain rules for VBScript variable names.

  1. Variable name must begin with a letter. Examples: salary, mark etc. Variables beginning with numbers or special characters are not allowed. Examples: 1stSchool, 3rdCar, _name etc.
  2. Variable name cannot exceed 255 characters.
  3. Variable name should not contain a period (.).

For declaring variables, you need to use the keyword Dim. Suppose you plan to use a variable named “salary” in your VBScript program, syntax

Dim salary;

Just declaring the variables will not help you, use it. You will have to assign a value to it at some point or another and this process is known as initializing the variable. If you are planning to declare a variable named salary, then you can code like this:

Dim salary salary = 10000

The important thing you need to make sure is that you should not assign a value to the variable as and when you declare it. Suppose you write a statement like this:

Dim salary = 10000

If you try to output salary using document.write, it will not return any output.

Code Sample #2

Step 1) Open your text editor and add the following lines of code.

<html> <head> <title>Variables</title> </head> <body> <script type="text/vbscript"> Dim variable1 variable1="John" document.write(variable1) ‘Dim variable2 = "Smith" ‘document.write(variable2) </script> </body> </html>

Step 2) Save this file as variable.html in your preferred location and then open this in IE (following the steps specified in previous chapter). Now, you will see the value John on the browser.

Step 3) Next, uncomment line # 11 & 12

Again save the file and refresh the IE browser if it is already opened or open the file in the IE browser. You might be wondered to see nothing; neither John nor Smith. The problem here is that you tried to assign the value to the variable while declaring it which is not allowed.

Loose Binding

VBScript provides you the freedom to use variables without declaring it (called loose binding).

For example, without having the statement Dim student, you can assign a value to the variable student like –

student = "John"

But, it is not at all a good programming practice.

If you use a variable without declaring it and misspell the same variable when you use it again, VBScript will not prompt you of the error.

So to make the code easier to read and to identify the errors, you should use the Option Explicit statement at the beginning of your code so that you will be forced to declare all your variables even if you forget to do so.

To avoid variable type related issues, it is always good to specify the statement Option Explicit at the beginning of your VBScript code.

Code Sample #3:

Step 1) Open your text editor and add the following lines of code.

<html> <body> <script type="text/vbscript"> Option Explicit ‘Dim markE, markM, markT markE=90 markM=86 markT=markE+markM document.write("Your marks in English is " & markE & "." & "<br />") document.write("Your marks in Mathematics is " & markM & "." & "<br />") document.write("Your total marks is " & markT & ".") </script> </body> </html>

Step 2) Save the file as variables.html in your preferred location. Now open the file in Internet Explorer and your screen is blank. Why ? because you have used option explicit but not declared variables before using them

Step 3) Now to understand the importance of Option Explicit statement, uncomment Line 5 in above code

Step 4) Save the variables.html file and refresh your browser. Now, your output will be like this:

Note - To concatenate two strings , you need to use “&”. In above example its used inside document.write command

It is obvious that the calculation of total marks is wrong.

Now just add the first statement Option Explicit at the beginning of VBScript code (without the Dim statement).

Save the file and see the output. You will get nothing as output which indicates that your code has some error.

Here the error is you have not declared variables before using it even after specifying Option Explicit statement.

You can also declare variables using public and private keywords like public student or private student.

But, you have to be more careful while using these two keywords for declaring variables because it will change the scope of your variables.

You can also store multiple values in a single variable and such variables are known as Vbscript array variables.

Suppose, you want to store details like name, marks, address etc of 30 students.

It will be really difficult to create and manage sets of 30 variables for names, marks, addresses and so on.

Instead, you can declare a single variable named students and store the names of all 30 students in this variable.

In such case, you will declare the variable as Dim students(29) (array index starts from zero) and you will assign values as

students(0) = "John" students(1) = "Hannah" students(2) = "Kevin" ....... ....... students(28) = "Rose" students(29) = "Emma"

Similarly, you can create variables like marks, address etc to store the respective values of all 30 students. You can also create multi dimensional arrays having up to 60 dimensions.

Code Sample #4:

Open your text editor and add the following lines of code.

<html> <body> <script type="text/vbscript"> Option Explicit Dim students(19), marks(19) students(0) = "John" marks(0) = 95 students(1) = "Emma" marks(1) = "83" students(2) = "Kevin" marks(2) = 87 document.write(students(0) & " has scored " & marks(0) & ".<br />") document.write(students(1) & " has scored " & marks(1) & ".<br />") document.write(students(2) & " has scored " & marks(2) & ".<br />") </script> </body> </html>

Here, we have stored details of only three students. You can add details of up to 20 students as we have set the size of the array as 20 (as index starts from 0).

Variant Subtypes

In the previous section, you might have noticed that we assigned different types of data to the variables.

We have stored numbers (mark and salary), strings (name) etc in different variables.

These numbers, strings etc are known as data types.

In fact, VBScript has only one data type called Variant.

Variant is a special kind of data type which can hold different kinds of information.

If you use Variant in a numeric context, it behaves as a number and when you use it in a string context, it behaves as a string.

In other words, when you specify salary=10000, VBScript assumes that salary is a numeric data type.

A Variant makes specific distinctions about the nature of the data.

For example, you can use variant type to store Boolean values, currency, date and so on.

These different categories of information that can be contained in a Variant are called sub types.

Though most of the time, Variant behaves in such a way that is most appropriate for the data it contains, you should be aware of different subtypes.

Following is the list of variant subtypes.

  • Empty: A special subtype to represent a variable that has not been assigned with any value yet.
  • Null: A special subtype to represent a variable assigned with a null value.
  • Integer: Using 2 bytes to express signed integer in the range -32,768 to 32,767.
  • Long: Using 4 bytes to express signed integers ranging from -2,147,483,648 to 2,147,483,647.
  • Single: Using 4 bytes to express real numbers in floating-point format ranging from -3.402823e38 to -1.401298e-45 for negative values, and from 1.401298e-45 to 3.402823e38 for positive value.
  • Double: Using 8 bytes to express real numbers in floating-point format ranging from -1.79769313486232e308 to -4.94065645841247e-324 for negative values, and from 4.94065645841247e-324 to 1.79769313486232e308 for positive values.
  • Currency: Using 8 bytes to express real numbers in decimal format ranging from -922,337,293,685,477.5808 to 922,337,293,685,477.5807.
  • Date: Using 8 bytes to express dates ranging from January 1, 100 to December 31, 9999.
  • String: Using 1 byte per character to express a sequence of characters that can be up to approximately 2 billion characters.
  • Object: A special subtype to represent a reference to an object.
  • Error: A special subtype to represent an error number.
  • Boolean: Using 2 bytes to contain either True or False.
  • Byte: Using 1 byte to express integer in the range 0 to 255.

There are two built-in VBScript functions that help you know the subtype of a variable: “varType()” and “typeName()”.

The vartype returns the numeric representation and typeName() returns the text representation of the subtype of the variable.

Each subtype has a predefined numeric representation.

Code Sample #5

Open your text editor and add the following lines of code.

<html> <head> <script type="text/vbscript"> Option Explicit Dim a a = Empty document.write("a = " & a & "<br />") document.write("The numeric representation of a is " & VarType(a) & "<br />") document.write("The variable a is of " & TypeName(a) & " data type." & "<br /><br />") Dim b b = Null document.write("b = " & b & "<br />") document.write("The numeric representation of b is " & VarType(b) & "<br />") document.write("The variable b is of " & TypeName(b) & " data type." & "<br /><br />") Dim c c = 4 document.write("c = " & c & "<br />") document.write("The numeric representation of c is " & VarType(c) & "<br />") document.write("The variable c is of " & TypeName(c) & " data type." & "<br /><br />") Dim d d = -2100483648 document.write("d = " & d & "<br />") document.write("The numeric representation of d is " & VarType(d) & "<br />") document.write("The variable d is of " & TypeName(d) & " data type." & "<br /><br />") Dim e e = -3.402823E38 document.write("e = " & e & "<br />") document.write("The numeric representation of e is " & VarType(e) & "<br />") document.write("The variable e is of " & TypeName(e) & " data type." & "<br /><br />") Dim f f = "John" document.write("f = " & f & "<br />") document.write("The numeric representation of f is " & VarType(f) & "<br />") document.write("The variable f is of " & TypeName(f) & " data type." & "<br /><br />") Dim g g = True document.write("g = " & g & "<br />") document.write("The numeric representation of g is " & VarType(g) & "<br />") document.write("The variable g is of " & TypeName(g) & " data type." & "<br /><br />") </script> </head> <body> </body> </html>

Save the file as subtype.html and open it in IE. Your output will look like this:

NOTE: You can also declare variables using public and private keywords like public student or private student. But, you have to be more careful while using these two keywords for declaring variables because it will change the scope of your variables.

Summary

  • Variables are used to hold a value or an expression while programming. Variables are to be declared and initialized separately.
  • Though you can use variables without declaring, declaring variables before using them is considered as a good programming practice.
  • Variant is the only data type of VBScript and variant has different subtypes including String, Boolean, Integer, Currency etc.

Troubleshooting

In case you see a blank page after you run the code, do the following

  • Press F12 to open developer tools
  • In left toolbar scroll down until you see "Emulation" settings page
  • Change Document Mode from default ("Edge") to 10
  • Add the following code to the head<meta http-equiv="x-ua-compatible" content="IE=10">

 

VBScript - Variables


Previous Page

Next Page  


VBScript Variables

Variable is a named memory location used to hold a value that can be changed during the script execution. VBScript has only ONE fundamental data type, Variant.

Rules for Declaring Variables:

  • Variable Name must begin with an alphabet.

  • Variable names cannot exceed 255 characters.

  • Variables Should NOT contain a period(.)

  • Variable Names should be unique in the declared context.

Declaring Variables

Variables are declared using “dim” keyword. Since there is only ONE fundamental data type, all the declared variables are variant by default. Hence, a user NEED NOT mention the type of data during declaration.

Example 1: In this Example, IntValue can be used as a String, Integer or even arrays.

Dim Var

Example 2: Two or more declarations are separated by comma(,)

Dim Variable1,Variable2

Assigning Values to the Variables

Values are assigned similar to an algebraic expression. The variable name on the left hand side followed by an equal to (=) symbol and then its value on the right hand side.

Rules:

  • The numeric values should be declared without double quotes.

  • The String values should be enclosed within doublequotes(")

  • Date and Time variables should be enclosed within hash symbol(#)

Examples:

' Below Example, The value 25 is assigned to the variable. Value1 = 25 ' A String Value ‘VBScript’ is assigned to the variable StrValue. StrValue = “VBScript” ' The date 01/01/2020 is assigned to the variable DToday. Date1 = #01/01/2020# ' A Specific Time Stamp is assigned to a variable in the below example. Time1 = #12:30:44 PM#

Scope of the Variables

Variables can be declared using the following statements that determines the scope of the variable. The scope of the variable plays a crucial role when used within a procedure or classes.

Dim

Variables declared using “Dim” keyword at a Procedure level are available only within the same procedure. Variables declared using “Dim” Keyword at script level are available to all the procedures within the same script.

Example : In the below example, the value of Var1 and Var2 are declared at script level while Var3 is declared at procedure level.

Note : The scope of this chapter is to understand Variables. Functions would be dealt in detail in the upcoming chapters.

<!DOCTYPE html> <html> <body> <script language="vbscript" type="text/vbscript"> Dim Var1 Dim Var2 Call add() Function add() Var1 = 10 Var2 = 15 Dim Var3 Var3 = Var1+Var2 Msgbox Var3 'Displays 25, the sum of two values. End Function Msgbox Var1 ' Displays 10 as Var1 is declared at Script level Msgbox Var2 ' Displays 15 as Var2 is declared at Script level Msgbox Var3 ' Var3 has No Scope outside the procedure. Prints Empty </script> </body> </html>

Public

Variables declared using "Public" Keyword are available to all the procedures across all the associated scripts. When declaring a variable of type "public", Dim keyword is replaced by "Public".

Example : In the below example, Var1 and Var2 are available at script level while Var3 is available across the associated scripts and procedures as it is declared as Public.

<!DOCTYPE html> <html> <body> <script language="vbscript" type="text/vbscript"> Dim Var1 Dim Var2 Public Var3 Call add() Function add() Var1 = 10 Var2 = 15 Var3 = Var1+Var2 Msgbox Var3 'Displays 25, the sum of two values. End Function Msgbox Var1 ' Displays 10 as Var1 is declared at Script level Msgbox Var2 ' Displays 15 as Var2 is declared at Script level Msgbox Var3 ' Displays 25 as Var3 is declared as Public </script> </body> </html>

Private

Variables that are declared as "Private" have scope only within that script in which they are declared. When declaring a variable of type "Private", Dim keyword is replaced by "Private".

Example : In the below example, Var1 and Var2 are available at Script Level. Var3 is declared as Private and it is available only for this particular script. Use of "Private" Variables is more pronounced within the Class.

<!DOCTYPE html> <html> <body> <script language="vbscript" type="text/vbscript"> Dim Var1 Dim Var2 Private Var3 Call add() Function add() Var1 = 10 Var2 = 15 Var3 = Var1+Var2 Msgbox Var3 'Displays the sum of two values. End Function Msgbox Var1 ' Displays 10 as Var1 is declared at Script level Msgbox Var2 ' Displays 15 as Var2 is declared at Script level Msgbox Var3 ' Displays 25 but Var3 is available only for this script. </script> </body> </html>

Previous Page

Print

PDF

Next Page  


0 comments

Leave a Reply

Your email address will not be published. Required fields are marked *