This lesson is designed to get you familiar with data types and their usage. Very important stuff for memory management in larger programs.
Definitions
Variable
Variable is used to store value temporarily. 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
- 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.
A good practice to get in to is naming your variables with a descriptive prefix. When looking through your code, you’ll know at a glance what type of data a particular variable should have in it.
As an example, I use three-letter descriptive prefixes such as these:
Data Type | Prefix | Example |
---|---|---|
Byte | byt | bytTotalBytes = 1024 |
Boolean | bln | blnIsEnabled = vbTrue |
Integer | int | intNumberOfEntries = 1000 |
Long Integer | lng | lngBigNumber = 147483748 |
Single | sng | sngPi = 3.14 |
Double | dbl | dblPi = 3.14159265 |
Date | dat | datToday = “03/25/2018” |
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.
Data type memory storage values
Data Type | Storage | Data Type | Storage |
---|---|---|---|
Byte | 1 byte | String (variable-length) | Length + 10 bytes |
Boolean | 2 bytes | String (Fixed-Length) | Length of string |
Integer | 2 bytes | Currency | 8 bytes |
Long | 4 bytes | Decimal | 12 bytes |
Single | 4 bytes | Object | 4 bytes |
Double | 8 bytes | Variant (numeric) | 16 bytes |
Date | 8 bytes | Variant (text) | length +22 bytes |
Data types and their value range
Data Type | Value Range |
---|---|
Byte | 0 to 255 |
Boolean | True/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 |
Date | January 1, 100 to December 31, 9999 |
String (Variable length) | 0 to approximately 2,097,152 characters |
String (Fixed length) | 1 to 65,535 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) |
Object | Any object |
Variant (numeric) | Any value as large as Double |
Variant (text) | Same as variable length string |
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]
Examples
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
QuickHint:
Declaring multiple variables on a single line is not generally a good practice. Declaration of variables in this manner leads to readability issues.
Implicit declaration vs 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.
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