Variables and Data Types


This VB6 lesson clarifies the concepts of variables and data types with examples. Some simple definitions and explanations have given here. Hope they will help you understand quickly and easily.


Variable 

Variable is used to store value. The value of the variable may vary during the program execution.



Constant

Constant is a fixed value that does not change during the program execution. You can define your own constant to use it in your program.



Naming Rules of variables 

1. A variable name must begin with an alphabet.
2. It cannot be more than 255 characters.
3. The variable name must not contain any special character like %,&,!,#,@ or $.
4. And finally, it has to be unique within the same scope.

Data Types 

Visual Basic is rich in its data types. Data types are used to declare the variables. At the time of declaration, memory is allocated for the variables. Different data types are used to store different types of values.

Table: Memory storage for data types

Data Type              Storage Data Type            Storage
Byte1 byte String (variable-length)Length + 10 bytes
Boolean2 bytesString (Fixed-Length)Length of string
Integer2 bytesCurrency8 bytes
Long4 bytesDecimal12 bytes
Single4 bytesObject4 bytes
Double8 bytesVariant (numeric)16 bytes
Date8 bytesVariant (text)length +22 bytes




Table: Data types & their value range
Data TypeValue Range
Byte 0 to 255
BooleanTrue/False
Integer-32,768 to 32,767
Long-2,147,483,648 to 2,147,483,647
Single-3.402823*10^3 to -1.401298*10^45 for negative values
1.401298*10^-45 to 3.402823*10^38 for positive values
Double -1.79*10^308 to -4.94*10^-324 for negative values
4.94*10^-324 to 1.79*10^308 for positive values
DateJanuary 1, 100 to December 31, 9999
String (Variable length)0 to approximately 2 billion characters
String (Fixed length)1 to 65,400 characters
Currency-922,337,203,685,477.5808 to 922,337,203,685,477.5807
Decimal+,-79,228,162,514,264,337,593,543,950,335 if no decimal is used
     +,-7.9228162514264337593543950335 (28 decimal places)
ObjectAny object
Variant (numeric)Any value as 16px as Double
Variant (text)Same as variable length string




Helpful links about data types:



Variable Declaration 

Depending on where the variables are declared and how they are declared, there are many ways to declare a variable in visual basic. When you declare a variable, memory space for the variable is reserved. This is called memory allocation. Different amount of memory space is reserved for different data types.

You can declare a variable with the 'dim' keyword.

Syntax:
            Dim variable As [Type]
       
Example:
Private Sub cmdSum_Click()
    Dim m As Integer
    Dim n As Integer
    Dim sum As Integer

    m = 10     'm is a variable, 10 is a constant
    n = 30
    sum = m + n

    Print "The sum is " & sum

End Sub

Output: The sum is 40



You can declare many variables in one line as follows and  assign multiple variables in one line using ':' operator.

Private Sub cmdSum_Click()
    Dim m As Integer, n as Integer, sum as Integer
    m = 10 : n = 30 
    sum = m + n

    Print "The sum is " & sum

End Sub

Output: The sum is 40




Implicit declaration : The Variant Data Type

If you use a variable without declaring its type, it is called a variant variable.

Example:
Dim num
Or,
Dim num As Variant

Or, if you choose not to declare a variable then also it is of the Variant data type. So you can use a variable in Visual Basic without declaring it. The variant data type can store numeric, date/time or string values. This is called implicit declaration. That means, you are declaring the variable implicitly. But it is not recommended to use implicit declaration and a good programmer will declare the variables explicitly. Because, it can lead to errors that may not be detected at run time.




Using the Option Explicit statement 

The statement  'Option Explicit' is written in general declaration section that reports the user of any undeclared variable showing an error message. That means, if you forget to declare any variable, an error message will be shown reporting it.


Go here for the above program: Easy VB6 samples.

Creating your own constants

You can create your own constant to use it in your program. The value of the constant remains unchanged throughout the program.

Example:
Private Sub cmdCalculate_Click()
    Const pi = 3.1415926   'or Const pi As Double = 3.1415926
    Dim area As Double, r As Double
    r = 2
    area = pi * r * r
    Print area
End Sub

Output: 12.5663704