Details About the Visual Basic .NET Data Types


The following sections briefly describe the data types presented in Table 4.1.

The Boolean Data Type

A Boolean data item can hold only one of two values in Visual Basic .NET: True or False . No other values are allowed for Boolean data items. True and False are keywords in Visual Basic .NET and equate to the values “1 for True and for False . (A complete list of Visual Basic .NET keywords is presented later in this chapter, in the section "Keywords.") This means that a Boolean data item can accept the values True , False , -1 , or , and that's it; nothing else is allowed.

So, should you use True or “1 if you want a Boolean data item to have a True value? Just what you need more decisions to make. Well, actually, this one's pretty easy. You should use True instead of “1 . This is the correct decision for two reasons. First, suppose you want to use a Boolean variable named Sick to describe how you feel. Which line is easier for you to understand?

 Sick = True 

or

 Sick = -1 

Clearly, using the Visual Basic .NET keyword True imparts more information about how the variable Sick is being used than does “1 . Using the keyword True gives the person reading the code at least some idea of how the variable Sick is being used. In other words, the use of the keyword True or False provides better documentation of how the variable is being used in the program. Don't forget that there may be other people who will have to understand your code, too.

A second reason for using True and False instead of their numeric equivalents concerns changes that might occur to Visual Basic .NET in the future. Suppose Microsoft decides that using the value of “1 for a true state of a Boolean variable was a bad idea, so it changes the True value to 1 . If all the Boolean variables in a program were assigned a “1 value instead of True , you would have to go through all those programs and change every occurrence of the “1 value to 1 . This could involve a lot of search-and-replace effort as you searched through all your programs' source code, looking at each -1 ; this process would be prone to errors.

However, if Microsoft changes the numeric value of the True state and you have used the keyword True throughout your code, everything still works correctly, and you don't have to make any changes to your programs' source code. All you then have to do is recompile your programs. This is the case because the new Visual Basic .NET compiler would know that everywhere it finds the keyword True , it must use the value 1 instead of “1 , as it did before. Why should you make all the conversions when the Visual Basic .NET compiler can do it all for you?

You will see other examples of where it makes sense to use words to represent numeric values in later chapters.

The Byte Data Type

A Byte data item uses a single byte of memory. Remember from Chapter 2, "The Basics of Object-Oriented Programming," that each byte in memory has 8 bits. Each of these 8 bits can assume only one of two possible values: 1 or . If you take the number 2 (that is, the number of possible values for a bit) and raise it to the 8th power (that is, the number of bits available in a byte), you find the following:

2 8 = 256

This means that a byte of memory is capable of representing 256 unique values. Because 0 is a valid value for a byte, the range of values for a Byte data type is 0 through 255.

Byte data items are not used very often in Visual Basic .NET programs. However, they are used to store values that represent the American Standard Code for Information Interchange (ASCII) character set. Each time you press a key on the keyboard, a byte of information is sent from the keyboard to the computer as an ASCII character. For example, if you press the lowercase letter a on your keyboard, the ASCII value sent to the computer is the numeric value 97. If you hold the Shift key down and press a again, a capital A, with an ASCII value of 65, is sent to the computer. A complete list of the ASCII codes is presented in Appendix A, "The ASCII Character Set."

If your program needs to manipulate ASCII data as it is received from the keyboard, the Byte data type might be a good choice. In situations in which you need to manipulate character data, the Char data type might be more appropriate, as discussed in the following section.

The Char Data Type

The Char data type is used to store character data. That might seem strange because I just told you that the Byte data type is often used to store character data. Well, the World Wide Web is changing everything. If you write a program for the Web, it might be run in countries other than the United States. We're pretty fortunate in the United States because we can present each of the characters in our character set in 1 byte. If you count the number of keys on your keyboard, you'll see that there just aren't that many to worry about. This is not so for many Asian countries . A Chinese typewriter might have several thousand character keys on it. Clearly, a Byte data type doesn't have the range to store such a large character set.

The information age is drawing the world into a closer community, and Visual Basic .NET embodies the concept of a locale. Locale simply reflects the environment in which Visual Basic .NET is being used. For example, in the United States, people would use the notation 5/24/02 to represent the date May 24, 2002, whereas Europeans would use the notation 24/5/2002. If you set the Visual Basic .NET locale to the proper location, information is displayed in a format that is common to the locale selected. If you are writing programs in the United States, your locale uses the ASCII character set. However, if you are programming in China, your locale might be using the Katakana character set.

To accommodate the more extensive character sets found in some locales, programmers around the world have accepted the Unicode character set. Unlike the ASCII character set, the Unicode character set uses 2 bytes (16 bits) to represent each character in the character set of the locale being used. Note that Visual Basic .NET uses the Unicode character set in the United States, even though the ASCII character set would be sufficient. Keep in mind that the "double-wide" Unicode character set is the default mechanism for working with characters in Visual Basic .NET.

Given that Unicode uses 2 bytes for each character, how many characters could it represent? Let's do the math. There are 16 bits, with two possible states for each bit:

2 16 = 65,535

With more than 65,000 possible characters, Unicode can handle just about any character set that a locale might ever need.

The Decimal Data Type

The Decimal data type is a fixed-point numeric data type that uses 16 bytes (128 bits) for storage. Of the 128 bits, 96 are used to represent the number itself, and 1 bit is used for the sign bit. (A sign bit is used to determine whether the number is positive or negative. If the sign bit is , the number is positive. If the sign bit is 1 , the value is negative.) The remaining bits in the number are used as a base-10 scaling factor. If you do the math, you get the following large number:

2 96 = 79,228,162,514,264,337,593,543,950,335

As you can see in Table 4.1, the smallest nonzero number that can be represented with the Decimal data type is approximately ±1E “28 . (You can get additional details about the mechanics of the Decimal data type by using the Visual Basic .NET help system; search for the phrase "decimal data type.")

Programmer's Tip

graphics/tip_icon.gif

The smallest value for the Decimal data type is ±0.0000000000000000000000000001. This value is known as the Epsilon value and can be important in certain situations. The reason it becomes important is because if you test a value that is less than the Epsilon value against the value 0, the value is so small that Visual Basic .NET cannot detect the difference between that value and 0. That is, a value smaller than ±0.0000000000000000000000000001 is so small that Visual Basic .NET interprets it as 0.0. Although this is usually not a problem, it does point out a potential danger of comparing floating-point numbers to 0.


Because the Decimal data value is a fixed-point number, Decimal is often used for financial calculations that require large numbers of significant integral and fractional digits with no round-off errors.

The Double and Single Data Types

This section discusses the Double and Single data types together because they are similar in nature. As you can see in Table 4.1, the Double data type uses 8 bytes for storage, and the Single data type uses only 4 bytes for storage. Because of their different storage requirements, the range of values for a Single data type is less than that for a Double data type. It is also true that the Single data type has fewer digits of precision. That is, if you use a Single data type, you can rely on the number having only 7 significant digits of precision. The Double data type has up to 15 significant digits of precision.

Given that both data types are used to store floating-point numbers (that is, values that can have decimal points in them), which type should you use? As mentioned earlier in this chapter, the CPU chips used for the Windows operating system have FPPs that are most efficient when processing 8-byte data values. For that reason, the Double data type is often the best choice because the values are processed faster than Single values. Keep in mind, however, that if memory restrictions are critical, you might want to use the Single data type because it requires less memory for storage.

The Integer , Long , and Short Data Types

This section discusses the three data types Integer , Long , and Short together because they are all integral data types (that is, no decimal point is allowed), differing only in their storage requirements and, hence, their range of values.

The Integer data type takes 4 bytes (32 bits), of which 1 bit is used as the sign bit, leaving 31 bits for the number itself. Therefore, the possible range of values for an Integer data type is

“2 31 through 2 31 = “2,147,483,648 through 2,147,483,647.

An Integer data type has a positive value when the sign bit is positive and a negative value when the sign bit is negative.

The Long data type is similar to the Integer data type, but it takes 8 bytes (64 bits) for storage, again using 1 bit for the sign bit. Therefore, the Long data type has a range of

“2 63 though 2 63 = “9,223,372,036,854,775,808 through “9,223,372,036,854,775,807

A Long data type has a positive value when the sign bit is positive and a negative value when the sign bit is negative.

Finally, the Short data type uses only 2 bytes (16 bits) for storage, including the sign bit. As a result, its range of value is

“2 15 through 2 15 = “32,768 through 32,767

A Short data type has a positive value when the sign bit is positive and a negative value when the sign bit is negative.

Note that these three data types support only integral data values. That is, no decimal point is allowed with these three data types. Obviously, you should not use these values if you are working with data such as money or interest rates that may have fractional values. As you will learn, integral data types are perfect for counting things, and you will use them extensively in later chapters.

Of the three integral data types, the Integer data type is usually the most efficient to use, provided that it has the range of values you need in your program.

The Object Data Type

As mentioned in Chapter 2, Visual Basic .NET is an object-oriented language. Therefore, the programs you write with it often manipulate objects in various ways as the programs execute. Although we are not ready to dig in to this topic fully at this time, I can tell you that the 4 bytes mentioned in Table 4.1 as the storage value for the Object data type is actually the number of bytes required to store the memory address of an object, not the object itself. This might seem a bit strange at first, but as you will see later in this chapter, it makes a lot of sense when it comes to processing your objects in a program.

For the time being, you can simply think of the 4 bytes associated with an Object data type as a means by which Visual Basic .NET knows where to look in your computer's memory for an object. You'll learn more details later in this chapter.

The String Data Type

The String data type is one of the few data types that does not have a fixed storage requirement. The reason for this has to do with the nature of String data. The String data type is used to store character data. For example, suppose you have a String variable named MyName . You might have a program statement like this:

 MyName = "Joyce" 

In this case, MyName would use 10 bytes of memory (plus a few more overhead bytes). Why doesn't it use just 5 bytes plus a few overhead bytes? Remember that Visual Basic .NET is a global language that uses the Unicode character set, so each character requires 2 bytes of memory.

If you changed the program statement to this:

 MyName = "Clive Cussler" 

the storage requirement would increases to 26 bytes plus the overhead bytes. Note that the blank space between the first and last name counts as a character, so there are 13 letters in the name, requiring 26 bytes of memory.

Programmer's Tip

graphics/tip_icon.gif

The overhead bytes I keep mentioning with respect to the String data type are used by Visual Basic .NET to process certain String operations efficiently . The number of overhead bytes is fixed and does not change with respect to the number of characters present in the String variable. As a general rule, you do not need to concern yourself with these overhead bytes, and they are not mentioned again in any subsequent discussions about the String data type.


It should be obvious that most of the times that you want to represent information that is not numeric in nature, you use the String data type. You can safely assume that a String data type can store up to about 2 billion Unicode characters, which is usually enough characters for the problem at hand.

The User Defined Data Type

The User Defined data type requires a variable amount of memory for storage. There are circumstances in which a program might be more efficient with a blend of data types than with just one of the types presented in Table 4.1. In such cases, Visual Basic .NET allows you to define your own data type. Because you have the freedom to define User Defined data types as you see fit, there is no way to know how many bytes you need for one until after you have defined it.

I like to think of User Defined data as "data for adults." User Defined data types are an advanced topic that is covered in Chapter 12, "Program Loops."



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

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