Now that you have an idea of the types of data that Visual Basic .NET makes available to you, let's explore how you can actually use these data types in a program to get something productive done.
When you think about it, a computer program is usually designed with one thing in mind: manipulating data. When you write an email message to a friend, the program accepts the keystrokes you make, formats the text into an email message, and then passes along the message to your friend. If you run a program to figure out how much a bank loan will cost you per month, you type in the amount of the loan, the interest rate, and the number of months, and then you press the Enter key; the monthly payment is then calculated. In each of these cases, the program accepts data from you via the keyboard, processes that data, and presents new data based on what you typed in to the program.
Regardless of the exact purpose of the program, the basic sequence is the same: Get some data from the user, process that data, and then display the result to the user . The sequence can be simplified to three basic steps (as described in Chapter 3, "Thinking About Programs"):
Clearly, the program needs some place to hold the data after you type it in to the program. The place where the program holds the data is called a variable.
You define a variable to hold a data item. You create a variable's name yourself, and there are certain rules that you must follow when you create a valid variable name:
Consider the variable names presented in Table 4.2.
Table 4.2. Valid and Invalid Variable Names
Visual Basic .NET programmers follow some naming conventions for variables, and you'll learn when you start writing your own programs in later chapters. For now, you should know that you need to follow the three naming rules for variables.
Rule 3 for variable naming states that you cannot use a Visual Basic .NET keyword as a variable name. A keyword is a word that has special meaning to Visual Basic .NET. A keyword causes Visual Basic .NET to perform a certain action or process associated with the keyword. You will learn what each of the Visual Basic .NET keywords mean as you progress through this book. The following are the keywords for Visual Basic .NET:
Remember that you cannot use keywords as variable names. If you try to create a variable name by using a keyword, you get an error message from Visual Basic .NET.
The Dim Statement: Defining Variables
Before you can use a variable in a program, you need to define it. For example, suppose you want to create a variable named Age for use in a program. Further assume that you are only interested in using a person's age as an integer value. That is, you only care if a person is 37, not 37.5. To define the Age variable, you would type the following line into your program:
Dim Age As Integer
When you type this line and press the Enter key, Visual Basic .NET examines it. In other words, Visual Basic .NET scans , or parses, the line you typed to see if the line conforms to the rules of valid Visual Basic .NET statements. A statement is nothing more than a sequence of expressions, or words, that obey the rules of the Visual Basic .NET language. These language rules are called the syntax rules of the language. If you type something into a program that Visual Basic .NET determines does not obey the syntax rules, Visual Basic .NET issues a syntax error. Visual Basic .NET is smart enough to write a squiggly line under the word (or words) that cause a syntax error. (The squiggly line is the result of Visual Basic .NET's IntelliSense feature.) Because you do not get a syntax error when you type in the program code line
Dim Age As Integer
you know you have entered a statement that conforms to the Visual Basic .NET syntax rules.
The Dim keyword at the start of this statement tells Visual Basic .NET that you want to create a variable for use in the program. The word immediately following the Dim keyword is the name you want to use for the variable ( Age in this example). The last two keywords, As Integer , tell Visual Basic .NET the data type, also called the type specifier , to use for the variable named Age . Because you have obeyed all the syntax rules of Visual Basic .NET in this statement, you now have a variable named Age that you can use in your program to store integer-type data.