VBScript Operations 

One of the most common assignments you will hand to your scripts consists of performing operations. VBScript is equipped with various kinds of operators to respond to any type of operation you need.

The Assignment = 
We saw that, when declaring a variable, a memory space is reserved for it. Such a space is empty until you fill it with a value. This is performed with the assignment operation. The assignment operation gives a value to a variable. It is performed using the = operator. Its syntax is: VariableName = Value The VariableName must be a valid variable name. It cannot be a value such as a numeric value or a (doublequoted) string. Here is an example that assigns a numeric value to a variable: 
<Script Language="VBScript"> Salary = 12.55 </Script> 
Once a variable has been declared and assigned a value, you can call the Document.Write() function to display its value. Since the variable is part of your script and not an HTML tag, it doesn't need to be included in doublequotes (the proper sentence is that, "it doesn't need to be passed as a string"; but we have not learned what it means to pass an argument). Here is an example: 
<Script Language="VBScript"> Salary = 12.55 Document.Write(Salary); </Script> 
To improve, safeguard, and make your code more efficient, we have learned that you should declare a variable before using it. This is done using the Dim keyword. The above script could be written as follows: 
<Script Language="VBScript"> Dim Salary Salary = 12.55 Document.Write(Salary); </Script> 
The above code declares a variable before assigning it a value. You will usually perform this assignment when you want to change the value held by a variable. The first time the browser encounters a variable, you can make sure that the variable is holding a value, just as done above. Providing a starting value to a variable is referred to as initializing the variable. 
Practical Learning: Using the Assignment Operator 

The Addition + 
The addition is an operation used to add one value to another. The syntax used it: Value1 + Value2 To perform this operation, the interpreter would add the value of Value1 to that of Value2. You can use such an operation to display the result on an HTML page. Here is an example:
The addition operation as implemented in VBScript can be applied to natural numbers called integers and to floatingpoint numbers called float or doubleprecision numbers. Here is an example that illustrates this:
You can also apply this operation on variables. For example, once a variable holds a value, you can add another value to it: Salary + 0.55 You can also add the values of two variables using their names: SalaryWeek1 + SalaryWeek2 As done in the above script, you can display the addition using the Document.Write function. After the addition operation is performed, it produces a resulting value. Sometimes you will not simply need to display the value on a web page; you may want to use it in another calculation. Therefore, you can store the result in a another variable, then use the result as you see fit. The syntax used is: Result = Value1 + Value2 
Practical Learning: Performing Additions 

String Concatenation: & 
The Document.Write function we have used so far allows you to display a string on a web page. So far, we used this function various times to display different strings. VBScript provides an operator that allows you to add different strings and create a new one. This operation is performed using the string concatenation operator &. The syntax of the & operator is: Value1 & Value2 This operation is performed on strings and you can use it to display various items using the Document.Write function. Here is an example:
The above script would produce RogerLemerre. An alternative would have been to include space in one of the strings. The & operator can also be used like the addition operator as long as you apply it on strings. An example would be: FullName = FirstName & LastName The & operator can also be applied on more than two operand. This could be used to insert characters as you see fit. Here is an example:
After the & operation has been performed on two variables, the operation results in a new string. You can assign this new string to another variable, as in Result = Value1 & Value2 Here is an example:

The Subtraction  
The subtraction is an operation used to subtract one value from another. The syntax used it: Value1  Value2 To perform this operation, the interpreter would subtract the value of Value1 from that of Value2. You can use such an operation to display the result on an HTML page. Here is an example:
The subtraction operation as implemented in VBScript can be applied to natural numbers or decimal numbers. The subtraction can also be applied on variables and/or their values. After the subtraction operation is performed, it produces a result that can be used in other expression or assigned to another variable to store it somewhere. The syntax used would be: Result = Value1  Value2 
The Multiplication * 
The multiplication is used to add a number to itself a certain number of times. This operation is performed using the * operator. Its syntax is: Value1 * Value2 This operation can be performed on numeric values as follows:
The multiplication operation can be applied to values or variables that hold valid values. After the operation is performed, it produces a result that can be displayed on a web page or stored in a variable for later retrieval. The multiplication operation as performed in values produces a result that can be used in another expression or stored in memory through a variable. The syntax used would be: Result = Value1 * Value2 
Practical Learning: Using the Multiplication 

The Integer Division: \ 
Dividing an item means cutting it in pieces or fractions of a set value. For example, when you cut an apple in the middle, you are dividing it in 2 pieces. If you cut each one of the resulting pieces, you will get 4 pieces or fractions. This is considered that you have divided the apple in in 4 divisions. Therefore, the division is used to get the fraction of one number in terms of another. Microsoft Visual Basic provides two types of results for the division operation. If you want the result of the operation to be a natural number, called an integer, use the backlash operator "\" as the divisor. Here is an example: Value1 \ Value2 This operation can be performed on two types of valid numbers, with or without decimal parts. After the operation, the result would be a natural number. The result of either operation can be assigned to another value. It can also be displayed in a control using the assignment operator: Result = Value1 \ Value2 
The Division / 
The division operation is used to divide a numeric value or the value held by a variable. The division operation is performed using the forward slash. Its syntax is: Value1 / Value2 This operation can be performed on numeric values as follows:
The division operation can be applied to values or variables that hold valid values. The division operation produces a new value through the following syntax: Result = Value1 / Value2 
The Remainder: Mod 
The division operation gives a result of a number with or without decimal values, which is fine in some circumstances. Sometimes you will want to get the value remaining after a division renders a natural result. Imagine you have 26 kids at a football (soccer) stadium and they are about to start. You know that you need 11 kids for each team to start. If the game starts with the right amount of players, how many will seat and wait? The remainder operation is performed with keyword Mod. Here is an example: Value1 Mod Value2 The result of the operation can be used as you see fit or you can display it in a control using the assignment operator as follows: = Value1 Mod Value2 
The Negation  
In mathematics, a number written as 10500 or 44.12 is considered a positive number. In the computer world, we referred to such a number as unsigned (because it doesn't have a sign). An unsigned number has a value greater than 0. If a number is less than 0, such a number qualifies as negative. To indicate that a number is negative, you write the  sign on its left. For example 32 is called a negative number. A value or a variable is made negative by applying the  operator to its left. Here is an example:
The negation operator is referred to as a unary operator because it applies to only one value or variable. As illustrated above, a negative number can be assigned to a variable. A variable can also be made negative by using this operator. 
The Exponentiation: ^ 
Exponentiation is the ability to raise a number to the power of another number. This operation is expressed using the ^ operator (Shift + 6). It uses the following mathematical formula: y^{x} In Microsoft Visual Basic (and Microsoft Access), this formula is written as: y^x and means the same thing. Either or both y a x can be values or expression, but they must carry valid values that can be evaluated. When the operation is performed, the value of y is raised to the power of x. You can display the result of such an operation in a field using the assignment operator as follows: =y^x You can also assign the operation to an expression as follows: Total = y^x 
Line Continuation 
Although VBScript reads code as if it were on one line, there is a maximum number of characters you can display on one line of code. Notepad can hardly display any long or too long text you have in a procedure. Unless you like scrolling left and write on your screen, you can divide your code into different lines and still let VBScript consider it a single line. To cut a line of code and continue on the subsequent line, type an underscore at the end of the line you want to interrupt. Here are two examples in a sub procedure: <Script Language="VBScript"> Dim dblHours Dim dblMonday, dblTuesday, dblWednesday, dblThursday, _ dblFriday, dblSaturday, dblSunday Dim dblSalary Dim dblResult dblHours = dblMonday + dblTuesday + dblWednesday + dblThursday + _ dblFriday + dblSaturday + dblSunday dblSalary = txtSalary dblResult = dblHours * dblSalary </Script> 


Previous  Copyright © 20042010 FunctionX, Inc.  Next 
