Before you use a variable you must declare it. To declare a variable you can use
the Dim statement.
Dim intNumber
As Integer
Dim decInterestRate As
Decimal
In addition
| intNumber | The prefix int would signify an integer variable |
| decHours | The prefix dec would signify an Decimal variable, use sng for Single and use dbl for Double |
| strName | The prefix str would signify a string variable |
| blnYearEnd | The prefix bln would signify a Boolean variable |
By default it is REQUIRED to declare variables before you use them. If you do
not declare variables an error --- Name 'xxx' is not declared
will be reported. However it is not syntactically required that you indicate a
data type. In this class you must declare the data type when you declare the
variable.
It is not a violation of the rules of the language to declare more than one
variable on the same line
Dim intNumber As Integer, decAmount As Decimal, strName As String
But it is not advised. We will declare only one variable per line
Dim intnumber
As Integer
Dim decAmount As Decimal
Dim strName As String
Variables may be declared in the declarations section of the Form or within the procedure in which they will be used. A variable declared at Form/Class level is “known” to every procedure in the Form. That means all procedures may use variables declared at “Form level”. A variable declared at the “procedure level” is “known” only to that procedure and can only be referenced by statements within that procedure.
In Visual Basic all variables are assigned an initial value automatically.
This is NOT the case in all computer languages. C, C++, and Java do NOT
automatically initialize variables.
Since the need for variables arise throughout the programming process you will
typically work back and forth between the variables declarations and the
instructions during the creation of a program, declaring new variables as the
need arises.