5.1 TypesEvery object you create or use in a VB.NET program must have a specific type (e.g., you must declare the object to be an integer or a string or a Dog or a Button). The type tells the compiler how big the object is and what it can do. Types come in two flavors: those that are built into the language (intrinsic types) and types you create (classes, structs, and interfaces, discussed in Chapter 8, Chapter 12, and Chapter 13, respectively). VB.NET offers a number of intrinsic types, shown in Table 5-1. Table 5-1. The intrinsic types
Each type has a name (e.g., Integer) and a size (e.g., 4 bytes). The size tells you how many bytes each object of this type occupies in memory. An Integer, for example, is four bytes big. ( User -defined types also have a size, measured as the sum of all their member variables .) Programmers generally don't like to waste memory if they can avoid it, but with the cost of memory these days, you can afford to be mildly profligate if doing so simplifies your program. The description field of Table 5-1 tells you the minimum and maximum values you can hold in objects of each intrinsic type.
When programmers talk about what an object can do, they typically mean the methods of the object. Intrinsic types have implicit methods and they can't do much. You can use them to add two numbers together, and they can display their values as strings. User-defined types can do a lot more; their abilities are determined by the methods you create, as discussed in detail in Chapter 9. Objects of an intrinsic type are called variables. Variables are discussed in detail later in this chapter. 5.1.1 Numeric TypesMost of the intrinsic types are used for working with numeric values: Byte, Decimal, Double, Integer, Long, Short, and Single. The types can be classified either as those used for integer values (whole numbers) and those used for fractional values (rational numbers). The Byte, Integer, Long, and Short types all hold whole number values. [1] (The Byte type is not used very often and won't be discussed in this book.)
Typically you decide which size integer to use (Integer, Long, or Short) based on the magnitude of the value you want to store. For example, a Short can only hold values from -32,768 to 32,767, while an Integer can hold values from -2,147,483,648 through 2,147,483,647. That said, memory is fairly cheap, and programmer time is increasingly expensive; most of the time you'll simply declare your variables to be of type Integer, unless there is a good reason to do otherwise . Among the types that hold fractional values, the Single, Double, and Decimal types offer varying degrees of size and precision. For most uses, Single will suffice. If you need to hold a really big fractional number, you might use a Double. The Decimal value type was added to the language to support accounting applications. Note that the compiler assumes that any number with a decimal point is a Double unless you tell it otherwise. How you tell it otherwise is explained in Section 5.2, later in this chapter. 5.1.2 Non-Numeric Types: Boolean, Char, Date, and StringIn addition to the numeric types, the VB.NET language offers four other types: Boolean, Char, Date, and String. A Boolean value is a value that is either true or false. [2] Boolean values are used frequently in VB.NET programming as you'll see throughout this book. Virtually every comparison (e.g., is myDog bigger than yourDog?) results in a Boolean value.
The Char type is used from time to time when you need to hold a single character. The Char type can represent a simple character (e.g., A) , a Unicode character ( \u0041 ), or an escape character enclosed by single quote marks ( '\n' ). You'll see Chars used in this book, and their use will be explained in context. The Date type is used to hold date and time values. This type is most useful when working with a database from which you might extract date-time values. The String type is used to hold a series of text characters. Chapter 16 discusses the use of Strings in detail. 5.1.3 Types and Compiler ErrorsWhen you create a program using VB.NET, you can specify that the type of all variables must be declared before they are used. It is generally considered to be good programming practice to do so. You require that variables must be typed by including the following line at the top of your source code file: Option Explicit On It is also a good idea to specify that VB.NET behave as a strongly typed language. This means that the compiler will verify that each declared type is the proper type for the object in question. In order to make VB.NET behave as a strongly typed language, you would also add the following line to the top of your source code: Option Strict On
When VB.NET is strictly typed, the compiler will complain if you try to use a type improperly. This compiler check provides important assistance when you're developing code. The compiler complains in one of two ways: it issues a warning or it issues an error.
Programmers talk about design time, compile time, and run time. Design time is when you are designing the program. Compile time is when you compile the program, and run time is (surprise!) when you run the program. The earlier you unearth a bug, the better. It is better (and cheaper) to discover a bug in your logic at design time than later. Likewise, it is better (and cheaper) to find bugs in your program at compile time than at run time. Not only is it better; it is more reliable. A compile-time bug will fail every time you run the compiler, but a runtime bug can hide. Runtime bugs slip under a crack in your logic and lurk there (sometimes for months), biding their time, waiting to come out when it will be most expensive (or most embarrassing) to you. It will be a constant theme of this book that you want the compiler to find bugs. The compiler is your friend. The more bugs the compiler finds, the fewer bugs your users will find. Using Option Strict On to make VB.NET strictly typed helps the compiler find bugs in your code. Here's how: suppose you tell the compiler that Milo is of type Dog. Sometime later you try to use Milo to display text. Oops, Dogs don't display text. Your compiler will stop with an error: Dog does not contain a definition for `showText' Very nice. Now you can figure out if you used the wrong object or you called the wrong method. VS.NET actually finds the error even before the compiler does. When you try to add a method, IntelliSense pops up a list of valid methods to help you, as shown in Figure 5-1. Figure 5-1. IntelliSenseWhen you try to add a method that does not exist, it won't be in the list. That is a pretty good clue that you are not using the object properly. |