Changes to Data Types

Team-Fly    

 
Visual Basic .NET Unleashed
By Paul Kimmel
Table of Contents
Appendix A.  VB6 Programming Element Changes in VB .NET


Several changes to data types and user -defined types have evolved in Visual Basic .NET. Those changes are described in this section.

Currency Data Type

The Currency type has been replaced by the Decimal type. Changes to value types are described in the section on "Data Types" in Chapter 2, "Out with the Old, In with the New."

Def Type Is Not Supported

Visual Basic 6 allowed you to add a Def Type statement at the module level. For example, DefBool B indicates that all variables that are introduced without a variable type and begin with the letter B default to a Boolean data type.

VB6 supported DefBool, DefByte, DefCur, DefDate, DefDbl, DefDec, DefInt, DefLng, DefObj, DefSng, DefStr, and DefVar, followed by a range of letters; all non-type-specified variables beginning with one of the letters in the range defaulted to the Def Type type. Visual Basic .NET does not support Def Type .

User-Defined Type Construct Is Replaced with Structure

The user-defined Type block has been replaced with a more powerful Structure block. Chapters 3 and 5, "Subroutines, Functions, and Structures," explain using the new Structure construct. Type is not supported in Visual Basic .NET.

Fixed-Length Strings Are Not Supported in Visual Basic .NET

Visual Basic 6 allowed you to define a fixed-length string. By following the String data type with the asterisk and a number, you could indicate a fixed-length string in VB6.

 Dim S As String * 10 

Note

The MSDN help for .NET suggests that appending the string identifier ($) to any identifier forces it to the string data type.

I found no instance in which the $ type identifier was necessary to perform the conversion, and in fact, in all instances tested , appending $ to an identifier resulted in an error. The reason for this is probably that all types inherit from Object, which implements a ToString method. In most cases this method is probably called implicitly in situations where nonstring types are used when a string is expected.

 Dim S As String = 3.1459 

The preceding statement implicitly constructs a Double from 3.1459 and invokes the Double.ToString() method. (Although this cannot be verified because Microsoft hasn't released the source code for the CLR, it is a reasonable inference from the behavior of the code.)

Try the following statement to verify the implicitly ValueType creation for the literal 3.1459.

 MsgBox((3.1459).GetType.Name()) 

Although the statement looks strange , it does display the word Double in the message box.


The fixed-length string is not supported in Visual Basic .NET. All strings in Visual Basic .NET are variable length. The length of the string is updated when you assign a string to the reference. Strings in Visual Basic .NET can be up to approximately two billion Unicode characters in length.

Changes to Integer Types

Integer types have been modified as we converge on 64-bit processors. You have a wider array of integral types to choose from in Visual Basic .NET (refer to Table A.2), but 32-bit systems process 32-bit integers better than 16-bit or 64-bit integers. Table A.2 describes the revisions made to integral data types in .NET.

Table A.2. Revisions to Integer Data Types
Size VB6 Type Visual Basic .NET Type CLR Type
8 bits (none) (none) System.SByte
16 bits Integer Short System.Int16
32 bits Long Integer System.Int32
64 bits (none) Long System.Int64

The first column indicates the number of bits used to store values for each type. The number of bits used as an exponent of 2 indicates the number of possible values. For example, a 32-bit integer is 2 32 or 4,294,967,296, which means that integers can store approximately 4 billion possible values. Integers are signed in Visual Basic .NET, so an integer can store values up to about plus or minus 2 billion.

From Table A.2, you can see that 8-bit types are not supported in VB directly but are supported in the CLR. The optimal integral type to use for 32-bit systems is the Integer type, which should be the type you use for integral values unless you have very specific needs. (Refer to Chapter 2 for specific information on the capacity of specific data types in Visual Basic .NET.)


Team-Fly    
Top
 


Visual BasicR. NET Unleashed
Visual BasicR. NET Unleashed
ISBN: N/A
EAN: N/A
Year: 2001
Pages: 222

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