Wednesday, 16 September 2015

Class 8 - Declaring a variable

Declaring a variable is used to reserve space in memory.  There are 2 types of variable declaration:
  • Implicit declaration
  • Explicit declaration
Implicit Declaration :

Whenever VB comes across a variable, it is assigned a default data type. 

Explicit Declaration :

When the user wants to specify the data type of the variable, then it is called declaring explicitly.

Dim statement is used to declare a variable.

Syntax :

dim variable name as data type

Example :

dim a,b,c as Integer
 dim m as single, i,j as Integer

String variable declaration :

String declaration is little different from other data type declaration.
There are two types of string declaration.

  • Variable Length
  • Fixed Length
If  we want to enter a data of any length then it is variable length.

dim name as string

If we want to enter a data with a particular length, then it is called fixed length.

dim name as string * 3

In the above example we can enter a string with 3 characters only.


Assigning a value :

Once the variable is assigned a data type, the value should be given to it.
To declare and assign  one variable :
dim a as integer  
a=100  

or
dim a as integer = 100

To declare more than one variable :

dim a,b,c as integer
a=10
b=20
c=30


No comments:

Post a Comment