Changes

Jump to: navigation, search

Declaring Visual Basic Variables and Constants

2 bytes added, 19:27, 13 August 2007
no edit summary
The different types of Visual Basic variables and constants was described in detail in the previous chapter ([[Understanding Visual Basic Variable and Constant Types]]). In this chapter we will learn how to declare and initialize variables and constants in Visual Basic.
Before learning how to declare variables and constants it is worth a quick reminder of the difference between variables and constants in Visual Basic. Both constants and variables provide a way to reserve memory locations for the storage of values of various types (such as numbers, characters and strings). These memory locations are assigned names which can be used in Visual Basic code to reference these memory locations. For example, you might want to store the interest rate for a banking application. To do so you would declare a variable with a name of your choice (such as interestRate) and specify the variable as an ''IntergerInteger'' type (since it will be storing a number). Having declared the variable you can assign a value to it and read that value anywhere in your application code.
The difference between variables and constants is that the value stored in a variable can be changed at any time after it has been created. The value assigned to a constant, as the name suggests, cannot be changed once it has been declared an initialized.
</pre>
It is also possible to declare multiple variables on the same line by separating each varaible by variable with a comma. When the variable are of the same type, the type declaration only needs to be made once at the end of the declaration:
<pre>
</pre>
In the case where the variables are of different types the type of variable must be decalred declared at the end of each group of the same type. For example:
<pre>
</pre>
A Visual Basic constant is referenced using the name defined when the constant was declared. For example, the following Visual Basic code sets the ''Text'' roperty property of a Label control to the string value contained in the ''companyName'' constant:
<pre>

Navigation menu