Showing posts with label visual basic. Show all posts
Showing posts with label visual basic. Show all posts

Monday, April 12, 2010

VB Scripting - Operators

Previous12345678910Next

What is the need for Operators?


Operators in VB Script are to used to perform some operations or do some manipulation on variables or values. Operator is a notation to perform operation and this operation will happen on one or more operands.


Operators in VB script can be classified in to mainly four. They are

  • Arithmetic Operators.
  • Logical Operators.
  • Relational or Comparison Operators.
  • Concatenation Operator.

  • VB Script – Arithmetic Operators


    Arithmetic operators are used for arithmetic operations or mathematical operations such as Addition, Subtraction, Multiplication and other kinds of operations.
    Please find the list of Operators with some examples for easy understanding.

    Operator English MeaningExample Result
    + Add 5+27
    - Subtract 5-23
    * Multiply 5*210
    / Floating Point Division 5/22.5
    \Integer Division5\22
    ^ Exponent 5^225
    Mod Modulus 5 Mod 2 1

    Logical Operators


    Logical operators are used to perform logical operations. They are used to manipulate statements or create logical expressions. The Logical operators are AND, OR and NOT.
    Please find the list of Operators with some examples for easy understanding.

    Operator English MeaningExample Result
    Not Inverts Value Not False True
    Or Either Can Be True True or False True
    And Both Must Be True True And False False

    Relational or Comparison Operators


    Relational operators are also called as comparative operators like if we want to compare numbers etc. Generally they are used in conditional statements like IF Else or While loops.
    Please find the list of Operators with some examples for easy understanding.

    Operator English MeaningExample Result
    = Equal To 20 = 45False
    > Greater Than 20 > 45 False
    < Less Than 20 <>True
    >= Greater Than or Equal To 20 >= 45False
    <= Less Than or Equal To 20 <= 45 True
    <>Not Equal To 20 <> 45 True

    Concatenation Operator


    In order to combine two strings and display as a single string we use Concatenation Operator (&). We use “&” to join two expressions.
    Please find below the description with examples for easy understanding.

    Operator English MeaningExample Result
    &Join or Connect"Hello" & "World"Hello World


    Previous12345678910Next

VB Scripting - Data Types

Previous12345678910Next

What are the Data Types in VB Script?


Data types in VB Script is used in context of Variable. There is only one data type in VB Script called as Variant. The specialty of a Variant is that it can hold different forms of data or information within the variable and there is no need to explicitly define the type of data.


A Variant can hold data which contains either a numeric or a string value. The Variant behaves in the way you want i.e. behaves as a numeric value when used in a numeric context and behaves like a string when used in a string context. The only difference is that any data which is enclosed in quotation marks (" ") behaves like a string even though it contains only numbers.

Variant Subtypes


Variant can hold any type of data like Integer, String, Long, Single or double etc. The different categories of information/data which a Variant can hold are called as subtypes.

Below table gives a clear picture of subtypes of data which a Variant can hold or contain.
SubtypeDescription
IntegerThe size is of 2 bytes, Value can be from 0 to 255, The Integer range is from -32,768 to 32,767.
Long Long is for numbering system, The Integer range is from -2,147,483,648 to 2,147,483,647.
SingleHolds small precision, floating point range is from -3.402823E38 to -1.401298E-45 for negative values and for positive values is from 1.401298E-45 to 3.402823E38.
DoubleHolds big precision, floating point range is from -1.79769313486232E308 to -4.94065645841247E-324 for negative values and for positive values is from 4.94065645841247E-324 to 1.79769313486232E308.
CurrencyRanges from -922,337,203,685,477.5808 to 922,337,203,685,477.5807.
Date (Time)The range is between January 1, 100 to December 31, 9999.
ObjectContains an object.
BooleanContains a boolean value either True or False.
EmptyVariant is uninitialized. Value is 0 for numeric variables or a zero-length string ("") for string variables.
NullVariant intentionally contains no valid data.
ByteContains integer in the range 0 to 255.


You can use the conversion functions to convert the data from one subtype to another subtype which will be discussed in Type Cast lesson.

Previous12345678910Next

VB Script - Variable - Explicit Declaration

Previous12345678910Next

Explicit Declaration


Explicit Declaration is used to avoid the unnecessary confusion created while typing and also to avoid loading the memory with variables because of errors we can use Explicit Declaration. In order to use explicit declaration type we need to declare the variables before the script begins or it should be in the starting of the script.


We will use the same example used for Implicit Declaration but will change the script. In order to create Explicit Declaration in the beginning of the code we need to add one more line of code as ‘Option Explicit”. Take a look at the code for Explicit Declaration

Option Explicit
Dim Int1, Int2, Prod
Int1 = 28
Int2 = 30
Prod = Int1 * Int2
Msgbox Prod

Now lets debug the code. If by mistake instead of “Prod” you type “Prd” in Line 6 as Msgbox Prd then what will happen. Since “Prd” is not declared when you run the script it executes the first five line and when it executes the sixth line it throws error and till it is not rectified the resultant output is not displayed. It will throw error as “Variable is undefined: ‘Prd’.

This is the advantage of using Explicit Declaration.

Previous12345678910Next

VB Scripting - Variables - Implicit Declaration

Previous12345678910Next

Implicit Declaration:


Implicit Declaration is a default declaration type in VB and there is no need for us to declare a variable before assigning a value to the variable. The declaration and definition happens in one step.


Implicit declaration is easy to use. We don’t need to keep track of the variables used in the VB script.

Example

Int1 = 28
Int2 = 30
Prod = Int1 * Int2
Msgbox Prod

Let’s see how this is stored in the memory.








From the above example Int1 and Int2 are operands and the * is the operator and = is the assignment operator. Int1, Int2, Prod are Implicit Variables and as the script is executed in the memory the variables get created in the memory and the respective values are stored in those locations. With this type of declaration it is easy to create variables as and when required.

The one disadvantage of Implicit variables is that if there is a typo error in any line like for example "Prod" is mistyped as "Prd" then when the scrip is executed it creates another variable called "Prd" and the resultant out put is null (Nothing will be displayed). We should be careful in using the Implicit Declarations.

Previous12345678910Next

VB Scripting - Variable Declaration


Previous12345678910Next

What is a Variable?

Variable in VB Script is a place holder in the memory used to store some data or information.


The information assigned in the variable is a reserved section in the computer’s memory for storing data. The Memory used is temporary. The information stored in the memory is not permanent.

Rules for VBScript Variable usage


A variable in the VB script cannot start with a numeric value (Ex – 1stvalue). It can be a combination of Alphabets and Numbers together (Value1) or separated by a underscore (Value_1). There is no way you can use arithmetic operators and also space is not allowed because if space is given then VB script will recognize them as two different variables. Below are the basic rules followed while creating a Variable.
  • It must always begin with a letter
  • Should not contain a period (.) or arithmetic operators (+, -, *, /, ^)
  • The max length of the variable is 256 characters

Types of Variable Declaration


In VB Script Variable Declaration happens in two ways:

1. Implicit Declaration.
2. Explicit Declaration.

Assigning values to a Variable


A value to a variable can be assigned either using Implicit Declaration or Explicit Declaration.

Example

Value1=20
Jobtype=“Permanent”

From the above we can see that = is an assignment operator. The left hand side of the expression is the variable name and the right hand side of the expression is the value assigned to the variable.


Previous12345678910Next

Friday, April 9, 2010

VB Scripting - Building Blocks

Previous12345678910Next

VB Scripting has some building blocks and are important to know them and use them in an appropriate manner. Below are given the building blocks used in VB Scripting.



1. Variable has something called as data types.

2. Operators like =,+,-,* etc..

3. Branching statements to make decisions (If then else conditions)

4. Looping Constructs (For loop, While loop)

5. Functions and Procedures

6. String, Conversion, Date and Time

7. File System Object

Previous12345678910Next

VB Scripting Basics - Input Box Function

Previous12345678910Next

Input box function in VB Scripting is used to get information from the user and then that value is stored in a variable.


Type in the statement below in your note pad and save as sample2.


UserInput = Inputbox(“Please Enter your Name”)


When you execute the file by double clicking it is displayed like this.


“UserInput” in the above statement is a variable and whatever the user enters in the text box is stored in the variable called “UserInput”. If we want to view the data entered on the screen then we need to call this variable in the Msgbox function and the variable value is displayed for the user. In order to do this modify your code like this

UserInput = Inputbox(“Please Enter your Name”)
Msgbox  UserInput


When you execute the file again with this changes what happens. First you are prompted to enter a value and clicking OK button will display the message box with the value entered.

To put in as a definition Input box is an inbuilt VB Scrip function used for getting an input from the user during runtime. Input box provides a text box and allows the user to type in some data. When the user clicks the OK button this data is assigned to a variable. The default input type for an input box is a string.

Previous12345678910Next

VB Scripting Basics - Msgbox Function

Previous12345678910Next

Msgbox Function in VB Scripting is a very basic function and Let’s start with a simple VB Script to display a message for the user using the Msgbox Function and try to understand how it works.


Msgbox

Step 1: Open your note pad and type in this statement.


Msgbox “This is First VB Script”


Step 2: Click File > Save As and in the Save window


1. Enter the fine name as sample.vbs
2. Choose “All Files” option for the Save as Type
3. Click Save button

Step 3: On the desktop or the location you have given you will see a file with this icon

Step 4: Double clicking on this file your message box will be displayed with the predefined message you gave “This is First VB Script”

Let’s understand what Msgbox function does

Msgbox is used for displaying the output. It is an inbuilt VB function used to output (display) a value to the user. This value can be a constant or a variable. Msgbox contains a OK button. Until the user clicks the OK button the execution will not move on to the next line.

Previous12345678910Next

Defining VB Script and its Usage

Previous12345678910Next

What is VB Script?

1. VB Script is a scripting language.
2. A scripting language is a lightweight programming language.
3. VB Script is a subset of Microsoft's programming language Visual Basic.
4. VB Script is not case sensitive but as a part of best coding standards we use appropriate casing.



How to create and run a VB script?


It is very simple to create a VB script. Just write the code in a Microsoft Note pad and save the file by giving the file name with an .vbs extension. For example in the File name text box while saving you can enter this file name “example1.vbs”. In the Save as File choose the option “All Files” other wise it will save as a text file. As soon as you save your file as a VB script file you can run the script by double clicking on it.

Previous12345678910Next

VB Scripting - Important Terminology

12345678910Next

Before actually looking into VB scripting it is good to understand some terminology and also what they mean. We should know some thing about programming, scripting, compiler, scripting to have a better understanding.


Let’s look at what Programming and Scripting means


Programming: Programming is used for application development and involves writing of code and should have in depth knowledge of a programming language.

Scripting: Scripting on the other hand does not involve in writing the entire code for the application but involves in writing some lines of code or instructions so as to validate the application developed. To put in simple language Scripting is used for validation.

What is Compiler and Interpreter


Compiler: Programming uses compiler to compiles the entire code in one go and creates an executable file. This executable file will be used for execution. The advantage of compiler is that it has a faster execution rate compared to the interpreter. The big disadvantage of compiler is that there is no way you can change your code in the executable file and you have to go back to the code and make changes and again create an executable file.

Interpreter: Scripting uses Interpreter to read the code one line at a time and executes one line at a time. The advantage is that if the interpreter encounters an error it stops executing the remaining lines and you have the chance to correct it and re run it. At any point of time you can interrupt wile running and change the code and continue running it. The biggest disadvantage is that each time the code is executed it has to read each line and is time consuming and is also slow.

12345678910Next