Three kinds of VB.NET statements
A Visual Basic program consists of a series of statements and there are really only three kinds:
- Declaration Statements
We'll take a more detailed look at these next.
- Assignment Statements
This is just making one thing equal to another:
myVariable = myOtherVariable + aFileVariable
Visual Basic also supports assignment operators such as *= and &=. This simply combines an assignment with a operation. The operator &= is the same as a = a & b.
- Executable Statements
This can be something like starting a programming loop:
For i = 1 To 10
more statements
Next
Or executing a method, programming that is part of a class somewhere else:
aMethod(myVariable, myOtherVariable)
Methods will be covered extensively when we discuss Object Oriented Programming.
Variables, Constants, and Arrays
All programs need data to work with. Data is stored in variables and constants while a program is working on it. A variable is just a name attached to part of computer memory that you can change. A constant is a name attached to computer memory, but it's protected from being changed. You use the Dim statement, or one of several others to determine where the variable name will have meaning in the program (called the Scope of the variable - Public, Private, Protected, Friend, or Protected Friend). Here's a variable declaration.
Dim myVariable As String
One of the big differences between languages is the types of data that are supported. For example, whole numbers such as 1, 2, 3, -8759 and 14589729 are called integers and nearly all languages will support some form of integer. But not all languages will store them in computer memory the same way or support all possible integers. An integer in Visual Basic 6 will occupy 16 bits (the smallest unit of data, a 1 or a 0) of memory - equivalent to 2 bytes of memory - and can only store numbers in the range from -32,768 through 32,767. In all Visual Basic .NET languages, including VB.NET 2010, a integer will occupy 32 bits of memory - 4 bytes - and can store numbers from -2,147,483,648 through 2,147,483,647. In addition, there are at least three kinds of integers in .NET (Short, Integer, and Long) and more if you start counting things like the Byte data type that can technically store an integer as well.
The whole point of this is to help you understand the concept of "type" - a key part of .NET. A "type" is just a name for a specific way of storing information. "Long" is a "type".
VB.NET gives you a broad range of types that you can use in your program to store not only numbers, but characters (ordinary text), dates, true or false (Boolean) or floating point numbers. (A floating point number uses a base, like 1.23 where the number of digits is the same, and then multiplies this base by a power of ten - for example 10, 100, 1000, or .001 - to get larger or smaller values. The net effect is that the decimal point can "float" forward or backward.) There is also an Object data type that you can store any information in and a User-Defined data type - the Structure.
There is only one datatype worth mentioning that VB.NET does not support and that's one that was quite well known in VB6 and a few earlier versions: the Variant type. Earlier versions of Visual Basic made it easy to just start using a variable without bothering to decide what type of variable it was. This was one of the "features" of VB6 that made it unpopular with a lot of professional programmers. It turns out that this carefree way of coding can result in some nasty bugs and will also make the code run slower. In VB.NET, you can use the Object data type in a similar way but it's strongly discouraged (Object is provided for an entirely different reason) and performance is even worse than VB6 if you do use it.
You can also create a name for a whole Array of data with a single name using a declaration like this:
Dim myArray(10) As Integer
This declaration lets you store 11 (not 10, the array starts with 0) integers. Jumping ahead to a little more complicated code, you can prove it to yourself with a quick test:
Dim myArray(10) As Integer
Dim i As Integer = 1
For Each myElement In myArray
myElement = i
Console.WriteLine(myElement & " is in element " & i)
i += 1
Next
Running this code gives you this result in the Output Window of VB.NET. (You could put it in a Button event subroutine as in Lesson 1.)
1 is in element 1
2 is in element 2
3 is in element 3
4 is in element 4
5 is in element 5
6 is in element 6
7 is in element 7
8 is in element 8
9 is in element 9
10 is in element 10
11 is in element 11
There are lots of other more advanced ways to deal with data, such as enumerations, collections, and structures. Check MSDN for the specific details about declaring variables and constants. The key thing to remember right now is that you should declare a meaningful name for every piece of information that you will use in your program.
The main thing that a statement will contain is an operation that "does something" with information (that is, data). So we discuss operations on the next page.

