Using the data types [Part 1]


Visual Basic 6 is rich in data type. So you should know the usage of all the data types to make a powerful application.



Integer values

Among the data types for storing integer values, Integer is the most used data type for its efficiency. But sometimes you may use the Long data type instead of Integer especially when you want to reduce the overflow error as it can hold a very large value, from  -2,147,483,648 to 2,147,483,647 where an Integer can only store a value from -32,768 to 32767.

For small integer values, use the Byte data type, the value range is from 0 to 255 only in this case.




The Boolean Data Type

The Boolean variables stores only 0 and 1 that stand for False and True respectively. To use a Boolean variable in your code, firstly declare the variable as Boolean and then assign a True/False value.

Example:

Private Sub cmdCheck_Click()
    Dim TextOff As Boolean    'Declaring the variable


    'Assigning value
    If Text1.Visible = False Then
        TextOff = True
    End If

    'Using the Boolean variable
    If TextOff = True Then
        MsgBox "TextBox is hidden"
    Else
        MsgBox "TextBox is shown"
    End If

End Sub

Output:
Using the boolean data type


Floating point values

Single and Double are the mostly used data types. For small decimal values, use the Single data type and Double for large values. Decimal is a floating point data type. The Decimal data type is higher in precision and lower in range compared to Double. In case of Decimal data type, you can't explicitly declare it (As Decimal). To use it, convert a value to Decimal data type with the CDec function and assign it to a variant.

Example: The Decimal Data Type
Dim num As Variant
expr = 22.45 * 5.987
num = CDec(expr)