A variable is an object that can hold a value:
Dim myVariable As Integer = 15
In this example, myVariable is an object/variable of type Integer. You can assign values to that object, and then you can extract those values later. You initialize a variable by writing a definition and then assigning a value to that variable. The previous code line defines myVariable and initializes it with the value 15. The syntax for defining a variable is as follows:
Access modifiers are discussed in Chapter 8; for
now you'll use
Dim is short for dimension. This term
dates back to the early days of Basic programming and is essentially
An identifier is
just an arbitrary name for a variable, method, class, or other
element. In this case, the variable's identifier is
myVariable. The keyword
As signals that the term
that follows is the type, in this case Integer.
Rather than using a word to specify a variable's type, you can append a single character that corresponds to the type to the variable's name.
While type characters were preserved in the Visual Basic .NET language for continuity with VB6, many developers feel they should be avoided and that spelling out the type makes for clearer, easier-to-maintain code.
For example, rather than writing
As Integer, you
can use the suffix
%, which has the same meaning.
Thus, the following two lines are equivalent:
Dim myVariable As Integer ...