Monday 10 October 2011

Script 3: Write a script to calculate the Simple Interest.

Dim Principal, Time, Rate, SimpleInterest

Principal = CDbl (InputBox ("Please enter the Principal"))
Time = CDbl (InputBox ("Please enter the Time"))
Rate = CDbl (InputBox ("Please enter the Rate"))

SimpleInterest = (Principal * Time * Rate)/100

MsgBox SimpleInterest

Script 2: Write a script to find the area of a triangle.

Dim Base, Height, Area

Base = CDbl (InputBox ("Please enter the Base"))
Height = CDbl (InputBox ("Please enter the Height"))

Area = 0.5 * Base * Height

MsgBox Area

Script 1: Accept two numbers from the user and print the sum of two numbers.

Dim Sum

Number1 = CInt (InputBox ("Please enter the number 1"))
Number2 = CInt (InputBox ("Please enter the number 2"))

Sum = Number1 + Number2

Msgbox Sum

Type Conversion:

CInt: Converts any value into an Integer.

CDbl: Converts any value into a Double.

CLng: Converts any value into a Long.

CBool: Converts any value into a Boolean.

CStr: Converts any value into a String.

Comment in VBScript:

  • Single quote (‘) is used to comment a particular line or statement.
  • No multi line comment is VB

Example:
‘This is an example of comment
Msgbox "Hello World"

TypeName:

TypeName is a keyword which tells the kind of value held by a Variable.

Example:
Dim a
a="VBScript" : b=123
Msgbox TypeName (a)
Msgbox TypeName (b)

OutPut:
String
Integer

Data Types:

  • VBScript has only one data type called a Variant.
  • A Variant is a special kind of data type that can contain different kinds of information, depending on how it is used. Because Variant is the only data type in VBScript, it is also the data type returned by all functions in VBScript.
  • At its simplest, a Variant can contain either numeric or string information. A Variant behaves as a number when you use it in a numeric context and as a string when you use it in a string context. That is, if you are working with data that looks like numbers, VBScript assumes that it is numbers and does what is most appropriate for numbers. Similarly, if you're working with data that can only be string data, VBScript treats it as string data. You can always make numbers behave as strings by enclosing them in quotation marks (" ").

Subtype

Description
EmptyVariant is uninitialized. Value is 0 for numeric variables or a zero-length string ("") for string variables.
NullVariant intentionally contains no valid data.
BooleanContains either True or False
ByteContains integer in the range 0 to 255.
IntegerContains integer in the range -32,768 to 32,767.
Currency-922,337,203,685,477.5808 to 922,337,203,685,477.5807.
LongContains integer in the range -2,147,483,648 to 2,147,483,647.
SingleContains a single-precision, floating-point number in the range -3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values.
DoubleContains a double-precision, floating-point number in the range -1.79769313486232E308 to -4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values.
Date (Time)Contains a number that represents a date between January 1, 100 to December 31, 9999.
StringContains a variable-length string that can be up to approximately 2 billion characters in length.
ObjectContains an object.
ErrorContains an error number.


Reference: http://msdn.microsoft.com


DIM (Dimension)

It is a keyword which is used to declare a variables.

Saturday 8 October 2011

Delimitation:


  • Each line in VBScript is known as statement. As soon as you start code in the next line the previous line gets delimited.
  • Colon (:) can be used as a delimiter


                Example:
                                Dim a: a=10

Option Explicit:


  • "Option Explicit" is a keyword used to declare variables explicitly.When you use the Option Explicit statement, you must explicitly declare all variables using the Dim, Private, Public, or ReDim statements. If you attempt to use an undeclared variable name, an error occurs.
  • Use Option Explicit to avoid incorrectly typing the name of an existing variable or to avoid confusion in code where the scope of the variable is not clear.
           Example:
                       Option Explicit
                       Dim input, input1
                       Input=10
                       input1=20

                      Error: Variable is undefined: input1

Naming Convention for Variables:


  • Naming Convention for Variables:
  • Variables should start with an alphabet
  • No special characters is used in variables, except “_”
  • It can be combination of alphabets and numbers
  • Maximum length of a variable is 255 characters
  • Spaces are not allowed with in variables
  • Variables are case insensitive
  • Cannot use Keywords as variables names

Variables


  • Variable is a container which is used to hold a value either in temporary or permanently.
  • VBScript has one fundamental datatype called “VARIENT” which can hold different datatypes.

E.g. VInteger=123
       VString=”vbscript”

Wednesday 5 October 2011

My First VBScript

1. Open a NotePad.
2. Type: Msgbox "My First VBScript". Double quotes are mandatory.
3. Save the file as MyScript.vbs .
4. Double click on the file MyScript.vbs to execute.

Extension of the VBScript is .vbs
Msgbox is a keyword which is used to display any information on to the screen.

What is VbScript?

  • VBScript stands for "Visual Basic Scripting Edition", it’s a scripting language.
  • Developed by Microsoft.
  • Latest Version of VBScript is 5.8
  • VBScript does not require any compiler.
  • VBScript has been installed by default in Microsoft Windows.
  • Wscript.exe is a built-in file in windows machine which supports VBScript execution.