This chapter and the chapters that follow are all about the glue that holds the various parts of a Visual Basic program together: the Visual Basic language itself. In this chapter, we'll take a look at the elements of the Visual Basic language: how to declare variables and arrays, and how to use those elements. We'll see how to use strings, conditionals, operators, loops, and math techniques. We'll also see how to handle special Visual Basic formats like dates and financial data. And we'll see some items that programmers like but don't often see in programming books, like how to use Switch and Choose.
We'll cover tasks that involve some complexity and whose syntax is hard to remember in this chapter. In this way, this chapter also acts as a reference for easy lookup of those hard-to-remember items—and can save you from reinventing the wheel.
We'll see a lot of code in this chapter and throughout the book. To keep things simple, I'll use console applications, because they're the simplest to code and will keep extraneous details from getting in the way. Here's the console application we developed in the previous chapter that displayed the words "Hello from Visual Basic" in a DOS window:
Module Module1 Sub Main() System.Console.WriteLine("Hello from Visual Basic") End Sub End Module
Here, we're creating a Visual Basic module, and modules are designed to hold code. Sub Main indicates the entry point of our program—the part that will be executed first. And we're using the WriteLine method of the System.Console class to write to the console (that is, DOS window). Methods are procedures that are built into classes—note the syntax here; to call a method, you specify the class or object the method is part of, followed by a dot (.) and the method name. This tells Visual Basic where to look to find the method you want to use. We'll become familiar with syntax like this in this chapter and Chapter 3. As you can see, there are all kinds of terms here that have special meaning in Visual Basic, such as Sub, Module, End, and so on; those are Visual Basic keywords.