VBScript Introduction:
VBScript Introduction:
Before you continue you should have a basic understanding of the following: Before you continue you should have a basic understanding of the following:
HTML / XHTMLHTML / XHTML..
What is VBScript?
What is VBScript?
•
• VBScript is a scripting languageVBScript is a scripting language •
• A scripting language is a lightweight programming language.A scripting language is a lightweight programming language. A
Alightweightlightweight programming languageprogramming language is one that is designed to have very smallis one that is designed to have very small memory footprint
memory footprint, is easy to implement (important when, is easy to implement (important when porting portinga language), and/or a language), and/or has
has minimalistminimalistsyntax and features.syntax and features. C
Cmay be said to be lightweight, as itsmay be said to be lightweight, as its hardwarehardware assumptions are minimalassumptions are minimal (it is used to program
(it is used to program micro controllersmicro controllers, for example), for example) •
• VBScript is a light version of Microsoft's programming language Visual BasicVBScript is a light version of Microsoft's programming language Visual Basic
How does it Work?
How does it Work?
When a VBScript is inserted into an HTML document,
When a VBScript is inserted into an HTML document, the Internet browser willthe Internet browser will read the HTML and
read the HTML and interpret the VBScript. The VBScript can be executed interpret the VBScript. The VBScript can be executed immediately,immediately, or at a later event.
or at a later event.
We have to write VB Script in notepad
We have to write VB Script in notepad or Word Pad and save withor Word Pad and save with.vbs.vbs extension. Weextension. We can write an independent vb script file or we can embedded in to an HTML file.
can write an independent vb script file or we can embedded in to an HTML file. VBScript Variable:
VBScript Variable:
A variable which holds a pa
A variable which holds a particular value or expression at an instance rticular value or expression at an instance of time.of time. Rules for VBScript variable names:
Rules for VBScript variable names: •
• Must begin with a letter.Must begin with a letter. •
• Cannot contain a period (.)Cannot contain a period (.) •
• Cannot exceed 255 characters.Cannot exceed 255 characters.
Note:
Note:
In VBScript, all variables are of typeIn VBScript, all variables are of type variant variant , , that can stthat can store different ore different types of types ofdata. data.
Declaring VBScript Variables:
Declaring is in the sense creating a variable. We can do this by declaring the variable as dim (Dimension), public and as private also.
For Ex: Dim x
Dim myname
We can also create and assign a value to particular variable at a time. For Ex:
myname=”Sachin”
OPTION EXPLICIT:
If we specify option explicit, we can’t assign a value to variable. I.e. we have to declare the variable first before assigning a value to it. By default VBScript allows us to create a variable and assign a value without declaring it.
For Ex: Dim a a=10 msgbox a
Will display output 10 by showing a pop-up small window. Option explicit
a=10 msgbox a
The MsgBox(VBScript is not CASE SENSITIVE) function displays a message box, waits for the user to click a button, and returns a value that indicates which button the user clicked.
Sample Examples:
1. Dim a, b, c
a = 10
b = “20”
c = a + b
msgbox c
output: 30
Because it internally converts string into integer, depending on the operation it perform operation.
---2.
dim a,b,c
a = "10"
b = "20"
c = a + b
msgbox c
0utput: 1020
Here both variables values given as strings, hence it will append simply.
3.
dim a,b,c
a=int(inputbox("Enter a value"))
b=int(inputbox("Enter b value"))
c = a + b
msgbox c
The InputBox function displays a dialog box, where the user can write some input and/or click on a button. If the user clicks the OK button or presses ENTER on the keyboard, the InputBox function will return the text in the text box. If the user clicks on the Cancel button, the function will return an empty string ("").
We can give comments in VBScript as follows ‘ourcomment
Conditional statements in VBScript:
There are two Conditional Structures in VBScript.
Conditional statements are used to perform different actions for different decisions. In VBScript we have four conditional statements:
• If statement - executes a set of code when a condition is true.
• If...Then...Else statement - select one of two sets of lines to execute. • If...Then...Else If statement- select one of many sets of lines to execute.
• Select Case statement - select one of many sets of lines to execute. (Like Switch case in C, C++ and Java languages).
Examples: 4. dim a,b,c a = 10 b = 20 if a < b then msgbox a else msgbox b end if
Select Case
You can also use the "Select Case" statement if you want to select one of many blocks of code to execute.
Syntax:
Operation “Add” (Here we are giving Combination of Upper and lower case). Select Ucase Case
Case “ADD” Case “SUB” Case “DIV” Case “MUL” End select
Note: The UCase function converts a specified string to uppercase.
(To overcome the mismatch we are writing as Ucase, which converts lowercase letter into uppercase ones).
For Ex:
5. dim a,b,c
a=int(inputbox("enter the value of a")) b=int(inputbox("enter the value of b"))
c=int(inputbox("enter the value for 1.add,2.sub,3.div"))
select case c case 1 msgbox (a+b) case 2 msgbox (a-b) case 3 msgbox (a/b) case 4 msgbox(“Invalid operation’) end select
6. Mid function Functionality
str="Welcome to VBScript" l=len(Str)
msgbox " The length of the String " &l for i=1 to l
c = mid(Str,i,1)
msgbox " "& i &" Character is " &c next
7. Fibonacci Series option explicit Dim n,f1,f2,f3 n = int(inputbox("Enter n value")) f1 = 0 f2 = 1 msgbox f1 msgbox f2 do until n<0 f3 = f1 + f2 f1 = f2 f2 = f3 msgbox f3 n = n-1 loop
VBScript has two kinds’ procedures:
• Sub procedure.• Function procedure.
VBScript Sub Procedures
A Sub procedure:• is a series of statements, enclosed by the Sub and End Sub statements • can perform actions, butdoes not return a value
• can take arguments
syntax:
Sub sample()
some statements
End sub
// without arguments Syntax With arguments Syntax
Sub sample(arg1,arg2) Some statements End sub
We can call a procedure by using Call statement as follows: Call sample(arg)
Or
We can omit call statement and do like this Sample arg
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.
Syntax: Without arguments Function sample ()
Some statements
Sample = return Ur desired value End function
With arguments
Function sample(arg1,arg2) Some statements
Sample = return Ur desire value End function
Byval and byref functionality in VBScript: ByVal
the parameter is passed by value, thus the subroutine works with a copy of the original ByRef