Declaring variables is an integral part of most programming languages. To put in simple words, a declaration announces the existence of the element to the compiler. Variables are used to store values for later use. To hold these values, you must declare locations in memory where each of these values can be stored. This memory location is called a variable.
A variable contains any unit of information like a number, a date, or a sequence of alphanumeric characters (called a string). The type of information a variable contains is always the same, but its contents can be changed easily and quickly. In general, while declaring variables you must specify two things: the name of the variable, and the type of data the variable will contain.
How to Declare Variables?Syntax of declaring a variable:
Dim variablename [As datatype]The variable name must begin with an alphabetic character and can be 255 characters long and it should not be a reserved name that is used by Visual Basic. The datatype means the characteristic of a variable that determines what kind of data it holds.
Examples of variable declaration:
Dim EmpName as String
Dim JoinDate as Date, SerialNo as Integer
Commonly Used Data TypesThere are several data types used in VB. The most common ones that beginners should know are Boolean, Integer, Long, Double, Date, String and Variant (default).
BooleanThis data type stores just True (-1) of False (0).
IntegerThis data type holds integer values in the range of -32,768 to 32,767.
LongThis is an enhancement of the Integer data type. It stores integer values in the range of -2,147,483,648 to 2,147,483,647.
DoubleThe Double data type is a 64-bit floating point (decimal) number used when high accuracy is needed.
DateAs the name suggests, the Date data type is used to store date values.
StringThe String data type is used to store values as text. A string can include letters, numbers, spaces, and punctuation.
VariantA special data type that can take any form like numeric, string, or date data. By default, if you do not declare a data type for a variable, it becomes a variant. Unless you have a good reason, the usage of this data type should be minimal as it may lead to pitfalls in your code.
Assigning Values to VariablesNow that you have learnt how to declare variables, it is time to know how to assign values to them. The assignment of values to variables is done by the "equal to" (=) sign. Let's see a few examples:
EmpName = "Bruce Willis"As you can see above, you can assign values to variables and change them at a later stage in the code. You can also use variables to calculate values for other variables.
JoinDate = "05/25/2009"
SalesAmount = 25000
EmpName = "John"
Commission = SalesAmount * 0.01
Why is it a good practice?Declaring variables reserves an area in the memory for its use. Therefore, it's best to declare variables when you first use them to ensure that they are always initialized to some valid value and that their intended use is always apparent. By declaring the variable when it is first used, you give it context. You give it a reason to exist, which goes a long way towards understanding what the variable is for.
Not declaring variables may cause errors in your code and sometimes may even cause untraceable errors and give undesired output. So as a best practice always remember to declare variables before you use them.
Back to Visual Basic for Beginners