Tuesday 15 September 2015

Variables in VBscripting

Variable is nothing but a place holder. Variable is a named memory location used to hold a value that can be changed during the script execution. Vb scripting has only one fundamental. Variables in vb scripting are declared in three ways. They are as follows
  • Dim
  • Private
  • Public

Dim: It is a keyword. Vb scripting generally uses Dim to declare variables of any data type. . Multiple variables can be declared using this dim statement simply we have to separate them with a comma.
Ex: Dim top, bottom, left, right
Public: These statements varaibles are available to al procedure in all scripts
Private: These statements varaibles are available only to the scripts in which they are declared.
There are some restrictions for declaring these variables these are called Naming Conventions. Naming conventions are nothing but rules. Which we have to follow.
  • It can contain both alphabets and numerals also
  • It must begin with an alphabetic character
  • Cannot contain an embedded period
  • Must not exceed 255 characterstics
  • Cannot contain special characters, spaces
  • Must be unique in the scope in which it is declared.
  • Should not use keywords.


If you give some valid keywords it will not display any errors and it won't give any value . So don't give keywords in assigning values like name etc. Firstly we will discuss about the Data types. Data types is used to store data.It is nothing but which type of data is stored in our variable like integer, real numbers, string is like date, time etc.,Generally Data types are as follows
  • Empty:  Variant is uninitialized. Value is 0 for numeric variables or a zero-length string ("") for string variables.
  • Null: Variant intentionally contains no valid data.
  • Boolean: Contains either True or False.
  • Byte Contains integer in the range 0 to 255.
  • Integer Contains integer in the range -32,768 to 32,767.
  • Currency -922,337,203,685,477.5808 to 922,337,203,685,477.5807.
  • Long: Contains integer in the range -2,147,483,648 to 2,147,483,647.
  • Single: Contains 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.
  • Double: Contains 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.
  • String: Contains a variable-length string that can be up to approximately 2 billion characters in length.
  • Object: Contains an object.
  • Error: Contains an error number.

These are not that necessary we won’t use much, this subtype information is just to know about this knowledge.
Assigning Values:
Generally we assign values. Varaibles are placed on the right side and values are placed on the right side.
Example: B = 200
Variants: They hold different kinds of information default data type returned by all functions in vb scripting
Types of Variables:
There are two types of variables
Scalar Variables: Scalar variables containing a single variable. It contains a single value
Ex: Dim intempid= 20
Dim str First name = " Ajay"
Dim str Last name = " kumar"
Dim intAge = 43
here when we declare a string we have to keep them in ".." and when we declare a number we can directly declare it no need of keeping any comments there 

Array Variables: Array variables can contain one or more variables . Multi dimensional array variables can be declared using this array variables. Mostly we come across array variables.
Ex:  Dim arr(2,2)
where we can understand very easily that we have 2 rows and 2 columns. and it holds around 4 variables. 
Even in arrays we have two types one is Static arrays and the other is Dynamic array.
Static array: It stores specific number of elements and it's cannot be altered during the run time
Dynamic array : It can be handled by changing size of the arrays . we use Redim and redim preserve keywords to change the size of the Dynamic arrays 
Redim Keyword can increase the size and add on variables in a particular array and also can change the values in variables which are declared 
Redim Preserve option is used when we don't want to change the variables in present array and we want to assign it with additional values by deleting unnecessary variables or by commenting the unnecessary variables .

No comments:

Post a Comment