Visual Basic .NET Data Types


What is data? Simply stated, data is information. From a programmer's perspective, however, the information contained in a piece of data can be stored in a computer many different ways. Given that you have choices about how to store data in a computer, how do you select the "correct" way to store a given piece of data in the computer? It depends.

As a starting point in making a decision about which data type to use for a specific task, you first need to know what the data type choices are. Table 4.1 presents a list of the common data types that are supported by Visual Basic .NET. The table also presents additional information about each data type to help you better understand the use for which each data type is best suited.

Table 4.1. Visual Basic .NET Basic Data Types

Type

Storage (Bytes)

Value Range

Boolean

2

True or False

Byte

1

0 through 255 (unsigned)

Char

2

0 through 65535

Date

8

0:00:00 January 1, 0001, through 11:59:59 December 31, 9999

Decimal

16

0 through ±79,228,162,514,264,337,593,543,950,335 with no decimal point; 0 through ±7.9228162514264337593543950335, with 28 places to the right of the decimal; the smallest nonzero number is ±0.0000000000000000000000000001 ( ±1E-28)

Double

8

“1.79769313486231570E+308 through “4.94065645841246544E-324 for negative values; 4.94065645841246544E-324 through 1.79769313486231570E+308 for positive values

Integer

4

“2,147,483,648 through 2,147,483,647

Long

8

“9,223,372,036,854,775,808 through 9,223,372,036,854,775,807

Object

4

A value of any Object type

Short

2

“32,768 through 32,767

Single

4

“3.4028235E+38 through “1.401298E-45 for negative values; 1.401298E-45 through 3.4028235E+38 for positive values

String

[*]

0 to approximately 2 billion Unicode characters

User Defined

[*]

A range determined by the structure member's data type that is independent of the ranges of the other members

[*] The amount of storage depends on the implementing platform.

A quick look at Table 4.1 shows that there is a considerable range of values among the various data types. Sometimes the data type you need to use is obvious. For example, if you need to store the data value 1000 in a program, the Boolean and Byte data types will not work because they are not capable of storing the value 1000 . However, Char , Decimal , Double , and several other data types are quite capable of storing the value 1000 . How do you decide which of them to use?

Which Data Type to Use?

In all facets of life, decisions often involve tradeoffs. In terms of computer programming, the tradeoff often involves trading memory usage for speed or vice versa. In other cases, you have to trade memory usage for range. For example, if you use a Single data type, you use less memory for each number (4 bytes) than if you use a Double data type (8 bytes), but you are forced to accept a smaller range of values.

As a general rule, programmers are less concerned today about memory limitations than they are about the speed at which data is processed. Because the speed at which data is processed is very important, it seems to make sense that the smaller the number of bytes the program needs to manipulate while processing the data, the faster the program will run.

Well not really.

Data Type Selection and CPU Registers

When chipmakers design central processing unit (CPU) chips, they design them in such a way that certain parts of the chip, called registers, are most comfortable with data of a certain size. For example, most of the CPU chips that run the Windows operating system have registers that handle 4-byte data very efficiently . As a result, Integer data is a good choice in many situations, provided that the data falls within the range of an integer variable. In fact, in some instances, Integer data is processed faster than some of the data that uses fewer bytes simply because the smaller data types don't fit the "natural" size of the CPU's registers.

Let's reconsider our earlier decision about whether to use a Single or Double data type for very large numbers . The Single data type has fewer bytes, and if we don't need the extra range that a Double offers, it would seem that the Single data type should be a good choice. After all, it's only 4 bytes, just like an integer, and should fit naturally within the CPU registers, right?

Well, there's a little glitch in this thinking. The Single and Double data types are designed to represent floating-point numbers. Floating-point numbers are numeric values that may have fractional values, resulting in numbers that have decimal points in them. Because of this,floating-point numbers are not processed in the same way that integer numbers are. (Integer numbers are whole numbers and cannot have fractional values.)

Processing floating-point numbers is inherently slower than processing integer numbers. In fact, the processing is so much slower that chip manufacturers have recently begun to build small floating-point processors (FPPs) into CPU chips. (Prior to being part of the CPU itself, the FPP was a separate chip, like the 8087 by Intel.) The registers inside the FPPs are designed for 8-byte data values. Because of this, even though the Single data type uses fewer bytes than the Double type, it actually runs more slowly in most applications because it doesn't naturally fit the register size of the FFP. (This is because code must be executed to add an extra 4 bytes of empty data to the Single data type, and this operation takes time.) As a result, the Double data type is often the best choice for programs that crunch a lot of floating-point numbers.

Oh my you want to know the time, and I'm telling you how to build a watch. Still, the devil is in the details, and the more details you know, the better programmer you will be. Sometimes you can make better programming choices if you have a complete understanding about the hardware you're working with. As a friend of mine once said: "If you don't like these details, you should consider another line of work." There's a large chunk of truth there, so let's plow on.



Visual Basic .NET. Primer Plus
Visual Basic .NET Primer Plus
ISBN: 0672324857
EAN: 2147483647
Year: 2003
Pages: 238
Authors: Jack Purdum

Similar book on Amazon

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