|
|
|||
| H o m e | |||
|
|
S o m e D a t a T y p e C h a n g e s i n V B . N E T P a r t 1 By Michael McIntyre
Some data types in previous versions of Visual Basic have been updated or eliminated in Visual Basic.NET to make VB.NET compatible with the other .NET programming languages and the .NET runtime.
These changes in data types will affect how you declare, use, or convert some data types.
Def Statements
The Visual Basic 6.0, Deftype statements — DefBool, DefByte, DefCur, DefDate, DefDbl, DefDec, DefInt, DefLng, DefObj, DefSng, DefStr, and DefVar are not supported in VB.NET.
Though there is no direct replacement for the Def statements in VB.NET you may find a new feature of VB.NET helpful. If you need to declare a group of variables to be of the same data type you can declare them in one long code line. Here is an example:
Dim player1Score, player2Score, player3Score, player4Score As Integer
In VB6 only the player4Score variable would be typed as Integer, the other variables would be typed as Variant. In VB.NET all the variables will be typed as Integers.
Currency Data Type
The VB6 Currency data type is not supported in VB.NET. Instead, use the new Decimal data type for calculations involving money and for fixed-point calculations. The Decimal data type can handle more digits on both sides of the decimal point. Here is an example Decimal type variable declaration:
Dim invoiceAmount As Decimal
Date Data Type
In VB6 a Date is stored in a Double format using eight bytes. In Visual Basic.NET, the Date data type uses the .NET common language runtime DateTime data type, which is an eight-byte integer value.
Besides the change from using eight bytes to using an eight-byte integer, there are many new properties and methods available from VB.NET Date type. You can gain some productivity by using these new features. To learn more click here.
NOTE: If you must work with VB6 data of type Date in a VB.NET program be aware of this: Because of these different representations, there is no implicit conversion between the Date and Double data types. To convert between a VB.NET Date type and the Visual Basic 6.0 Date type, use the specialized ToOADate and FromOADate methods of the DateTime structure in the .NET System namespace.
Variant Data Type
The VB6 Variant data type is replaced by the Object data type in VB.NET.
The Object data type is .NET’s universal data type. It can hold data of any other data type. Visual Basic.NET documentation says that all the functionality of the VB6 Variant type is supplied by the VB.NET Object data type.
WARNING: Be aware that you should avoid using the Object data type whenever possible in .NET code. It causes poor performance and makes program code very hard to read. The use of the new VB.NET OPTION STRICT feature will help you avoid over-using the Object data type. Learn more about OPTION STRICT by clicking here.
One note about using Variant in VB6 vs. Object in VB.NET:
In VB6 if two Variant variables containing integers are multiplied, an overflow condition causes the data type of the result to be changed to Double.
In VB.NET if multiplication of two Object variables containing integers results in an overflow, the result is changed to the .NET 64-bit Long data type.
LSet and RSet
.NET enforces type-safe data. The LSet and RSet statements are not supported in VB.NET because they result in a type-unsafe operation, particularly with structures, which would result in unverifiable code. Replace your use of the LSet and RSet statements with the .Net String class’s SubString method. To learn more about the SubString method click here
|
||
|
Copyright © 2001-2003 aZ Software Developers. All rights reserved. |
|||