Section 5.1. Types

   

5.1 Types

Every 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

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 numbers up to 28 digits and the position of the decimal point; typically used in financial calculations; requires the suffix "m" or "M."

Double

8

Double

Double-precision floating-point numbers; holds the values from approximately +/-5.0 * 10 -324 to approximately +/-1.8 * 10 308 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 numbers; holds the values from approximately +/-1.5 * 10 -45 to approximate +/-3.4 * 10 38 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. 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.

Each VB.NET type corresponds to an underlying .NET type. Thus, what VB.NET calls an Integer, .NET calls an INT32. This is interesting only if you care about sharing objects across languages.

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 Types

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

[1] Remember, the Y2K problem was caused by programmers who couldn't imagine needing to reference a year later than 1999.

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 String

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

[2] 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.

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 Errors

When 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 

Thus, by implication , most well-designed VB.NET programs will begin with the following two lines:

 Option Explicit On 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.

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 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. IntelliSense
figs/lvbn_0501.gif

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.

   


Learning Visual Basic. NET
Learning Visual Basic .Net
ISBN: 0596003862
EAN: 2147483647
Year: 2002
Pages: 153
Authors: Jesse Liberty

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