16.1. Types


Every object in Visual Basic 2005 has a type. Types come in two flavors:

  • Those that are built into the language (called fundamental or intrinsic types )

  • Types you create (classes, structs, and interfaces)

Visual Basic 2005 offers a number of intrinsic types , as shown in Table 16-1.

Table 16-1. The intrinsic types

Type

Size (in bytes)

.NET type

Description

Boolean

1

Boolean

true or false.

Byte

1

Byte

Unsigned (values 0-255).

Char

2

Char

Unicode characters.

Date

8

DateTime

Midnight 1/1/0001 through 11:59:59 12/31/9999.

Decimal

12

Decimal

Fixed-precision up to 28 digits and the position of the decimal point. This is typically used in financial calculations. Requires the suffix "m" or "M."

Double

8

Double

Double-precision floating-point; holds the values from approximately +/-5.0 * 10-324 to approximate +/-1.8 * 10308 with 15 16 significant figures.

Integer

4

Int32

Integer values between -2,147,483,648 and 2,147,483,647.

Long

8

Int64

Integers ranging from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

Short

2

Int16

Integer values -32,768 to 32,767

Single

4

Single

Floating-point number. Holds the values from approximately+/-1.5 * 10-45 to approximate +/-3.4 * 1038 with 7 significant figures.

String

 

String

A sequence of Unicode characters.


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. 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.

Each Visual Basic 2005 type corresponds to an underlying .NET type. Thus, what Visual Basic 2005 calls an Integer, .NET calls an Int32. This is interesting only if you care about sharing objects across languages, an important topic but one that is beyond the scope of this book. The description field of Table 16-1 tells you the minimum and maximum values you can hold in objects of each type.

16.1.1. Numeric Types

Most of the intrinsic types are used for working with numeric values (Byte, Short, Integer, Single, Double, Decimal, Long).

Another way to divide the types is into those used for integer (whole number) values, and those used for fractional values. The Short, Integer, and Long types all hold whole number values. Most of the time, you'll just use Integer for these values, unless there is a good reason to do otherwise.

The Single and Double types hold fractional values (rational numbers). For most uses, Single will suffice, unless you need to hold a really big fractional number, in which case you might use a Double. The Decimal value type was added to the language to support accounting applications, and is described in Table 16-1.

16.1.1.1. Nonnumeric types

In addition to the numeric types, the Visual Basic 2005 language offers four other primitive types: Char, String, Date, and Boolean .

The Char type is used from time to time when you need to hold a single character. For sequences of characters you'll use the String type. Date types are used to hold date and time values, and are most useful when working with databases from which you might extract a date-time value.

The one remaining type of importance is Boolean. A Boolean value is a value that is either TRue or False.

The Boolean type was named after George Boole (1815-1864), an English mathematician who published "An investigation into the Laws of Thought, on Which Are Founded the Mathematical Theories of Logic and Probabilities," and thus created the science of Boolean algebra.


16.1.1.2. Understanding types

When you create a program under Visual Basic 2005, you may choose to set Option Explicit and Option Strict to On, by writing these lines at the top of your source code:

 Option Strict On Option Explicit On 

If you use Option Explicit or Option Strict, you must make them the first statements in your source code file.


Option Explicit requires that every variable be declared before it is used (variables are explained in the next section). Option Strict restricts the way you cast from one type to another. Both enable the compiler to help you find bugs in your program and together they make Visual Basic 2005 a strongly typed language; and that is a very good thing. This book assumes you will always set both Option Explicit and Option Strict on.

In a strongly typed language, every object you create or use must have a specific type (e.g., you must declare the object to be an Integer, a String, or a Dog). The type tells the compiler how big the object is and what it can do.

Confusingly, a perfectly legitimate type is type Object, though this type is rarely used and tends to undermine type safety.


The size of an object is measured in bytes. An Integer, for example, is four bytes big. User-defined types have a size as well, measured as the sum of all their member variables.

When programmers talk about what an object can do, they typically mean the methods of the object. User-defined types have many methods. Intrinsic types have implicit methods. For example, the numeric types have the ability to be added together, multiplied, and so forth.

The compiler will help you by complaining if you try to use a type improperly. The compiler complains in one of two ways: it issues a warning or it issues an error.

You are well advised to treat warnings as errors. That is, you ought to stop what you are doing and figure out why there is a warning and fix the problem. Never ignore a compiler warning.


Programmers talk about "design time," "compile time," and runtime." Design time is when you are designing the program. Compile time is when you compile the program, and runtime is (surprise!) when you run the program.

The earlier you find a bug, the better. It is best (and cheapest) to discover a bug in your logic at design time. It is better (and cheaper) to find bugs in your program at compile time rather than at runtime. 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 code, and lurk there (sometimes for months) biding their time, waiting to come out when it will be embarrassing to you.

It is 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. Setting Option Strict On helps the compiler find bugs in your code. Here's how: suppose you tell the compiler that Milo is of type Dog. Some time later you try to use Milo to display text. Oops, Dog objects don't display text. Your compiler will stop with an error.

 Dog does not contain a definition for 'showText' 

Very nice. Now you can go figure out if you used the wrong object or you called the wrong method.

Visual Studio 2005 actually finds the error even before the compiler does. When you try to add a method that does exist, IntelliSense pops up to help you, as shown in Figure 16-1.

When 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.

Figure 16-1. IntelliSense




Programming Visual Basic 2005
Programming Visual Basic 2005
ISBN: 0596009496
EAN: 2147483647
Year: 2006
Pages: 162
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net