Variables, Constant and Data Types


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             StorageData Type           Storage
Byte1 byteString (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
Byte0 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


EXAMPLE OF DATA TYPE :

Use Integer data Type

Dim number As Interger
number=10

Dim NIC As Long
NIC=870740913

You can use any numeric values for this data type.


Use String Data Type

Dim user_name As String
user_name="Hemantha" 

Use String Data Type

Dim national_date As Date 
national_date=1948/02/04

Use Boolean  Data Type

Dim Do_you_love_me  As Boolean 
Do_you_love_me=True 

-----------------------------------------------------------

Variable 


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

Naming Rules of variables 


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


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


-----------------------------------------------------------

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.

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