Thursday, 2 January 2014

VB Script Basic, Syntax, Examples and More...



What is VBScript?

VBScript is a Microsoft scripting language.
VBScript is the default scripting language in ASP (Active Server Pages).
A scripting language is a lightweight programming language
VBScript is a light version of Microsoft's programming language Visual Basic

VBScript in HTML
VBScript is inserted into HTML, between standard <script> and </script> tags.
Use the type attribute in the <script> tag to define the scripting language "text/vbscript":

<html>
<body>
<script type="text/vbscript">
...
</script>
</body>
</html>

VBScript Output
When VBScript is used on a web server in an ASP page, the statement response.write() produces output.
When we use Internet Explorer for testing VBScript, we use document.write() to produce output:

Example :
<html>
<body>
<script type="text/vbscript">
document.write("Hello World!")
</script>
</body>
</html>

VBScript Variables : Variables are "containers" for storing information.

Rules for VBScript variable names:
Ø  Must begin with a letter 
Ø  Cannot contain a period (.)
Ø  Cannot exceed 255 characters
In VBScript, all variables are of type variant, that can store different types of data.

Declaring (Creating) VBScript Variables
Creating variables in VBScript is most often referred to as "declaring" variables.
You can declare VBScript variables with the Dim, Public or the Private statement. Like this:

Dim x
Dim carname

Now you have created two variables. The name of the variables are "x" and "carname".
You can also declare variables by using its name in a script. Like this:

carname="Volvo"

Now you have also created a variable. The name of the variable is "carname". However, this method is not a good practice, because you can misspell the variable name later in your script, and that can cause strange results when your script is running.

If you misspell for example the "carname" variable to "carnime", the script will automatically create a new variable called "carnime".  To prevent your script from doing this, you can use the Option Explicit statement. This statement forces you to declare all your variables with the dim, public or private statement.

Put the Option Explicit statement on the top of your script. Like this:

Option Explicit
Dim carname
carname=some value

Assigning Values to Variables : You assign a value to a variable like this:
carname="Volvo"
x=10

The variable name is on the left side of the expression and the value you want to assign to the variable is on the right. Now the variable "carname" has the value of "Volvo", and the variable "x" has the value of "10".

Lifetime of Variables : How long a variable exists is its lifetime.
When you declare a variable within a procedure, the variable can only be accessed within that procedure. When the procedure exits, the variable is destroyed. These variables are called local variables. You can have local variables with the same name in different procedures, because each is recognized only by the procedure in which it is declared.

If you declare a variable outside a procedure, all the procedures on your page can access it. The lifetime of these variables starts when they are declared, and ends when the page is closed.

VBScript Array Variables
An array variable is used to store multiple values in a single variable.
In the following example, an array containing 3 elements is declared:

Dim names(2)

The number shown in the parentheses is 2. We start at zero so this array contains 3 elements. This is a fixed-size array. You assign data to each of the elements of the array like this:
names(0)="Tove"
names(1)="Jani"
names(2)="Stale"
Similarly, the data can be retrieved from any element using the index of the particular array element you want. Like this:

mother=names(0)

You can have up to 60 dimensions in an array. Multiple dimensions are declared by separating the numbers in the parentheses with commas. Here we have a two-dimensional array consisting of 5 rows and 7 columns:

Dim table(4,6)
Asign data to a two-dimensional array:

Example
<html>
<body>

<script type="text/vbscript">
Dim x(2,2)
x(0,0)="Volvo"
x(0,1)="BMW"
x(0,2)="Ford"
x(1,0)="Apple"
x(1,1)="Orange"
x(1,2)="Banana"
x(2,0)="Coke"
x(2,1)="Pepsi"
x(2,2)="Sprite"
for i=0 to 2
    document.write("<p>")
    for j=0 to 2
        document.write(x(i,j) & "<br />")
    next
    document.write("</p>")
next
</script>
</body>
</html>










VBScript Procedures :

VBScript has two kinds procedures:
1)Sub procedure
2)Function procedure


VBScript Sub Procedures A Sub procedure is a series of statements, enclosed by the Sub and End Sub statements can perform actions, but does not return a value can take arguments

Sub mysub()
  some statements
End Sub
Or

Sub mysub(argument1,argument2)
  some statements
End Sub

Example :

Sub mysub()
  document.write("I was written by a sub procedure")
End Sub

VBScript Function Procedures :A Function procedure is a series of statements, enclosed by the Function and End Function statements can perform actions and can return a value can take arguments that are passed to it by a calling procedure without arguments, must include an empty set of parentheses ()
returns a value by assigning a value to its name.

Function myfunction()
  some statements
  myfunction=some value
End Function

Or

Function myfunction(argument1,argument2)
  some statements
  myfunction=some value
End Function

Example (IE Only)
function myfunction()
  myfunction=Date()
end function


Calling a Procedure : This simple function procedures is called to calculate the sum of two arguments:

Example :
Function myfunction(a,b)
       myfunction=a+b
End Function

document.write(myfunction(5,9))

The function "myfunction" will return the sum of argument "a" and argument "b". In this case 14.When you call a procedure you can use the Call statement, like this:
Call MyProc(argument)

Or, you can omit the Call statement, like this:

MyProc argument

No comments:

Post a Comment