VB.NET Data Typesby Budi Kurniawan 07/30/2001
The new version of Visual Basic, VB7 (or VB.NET), is a big jump from VB6. With VB7 you can use the type library in the .NET Framework, and your applications run on the Microsoft .NET Framework Common Language Runtime (CLR).
IPT>
IPT>
There are also a number of changes from the previous versions. VB7 now supports inheritance and has a new error-handling mechanism. As part of the .NET Framework, VB7 needs to update the data types for interoperability with other programming languages such as C# and C++, and with the .NET Framework and Runtime. Data types in VB7 now represent the .NET data types, which are structures in the System namespace of the .NET Framework. However, you can still use the old programming style when working with data types, because in VB7 the data types are wrappers of those .NET data types. This article shows you how you could adapt yourself to these data types.
Bad news for VB6 experts: their expertise is not really relevant in VB.NET. Expertise in VB6 was often measured by skill in programming Windows API from inside the language. This is no longer true with VB7; VB7 programmers are now required to know the numerous types in the .NET Framework. To become an expert in VB.NET, you have to start all over again.
First, of course, you need to master the many classes, interfaces and structures that are part of the .NET Framework, not to mention the many changes in the new version of the language. But you need to start somewhere, right? Understanding the new set of data types is a good place to start.
VB7 Data Types and Equivalent .NET Framework Type Structures
VB7 value data types are wrappers for the corresponding .NET Framework type structure. These structures derive from the class System.Object . In fact, System.Object is the root of all types in the .NET Framework. The following table lists the data types in VB7 and the corresponding .NET data types. Note that there are new data types that were not available in VB6, and that some of the data types in VB6 are no longer supported. The changes from VB6''''s integers, currencies, and variants will be explained below.
Visual Basic type
.NET Runtime type
structure Storage size
Value range
Boolean
System.Boolean
4 bytes
True or False
Byte
System.Byte
1 byte
0 to 255 (unsigned)
Char
System.Char
2 bytes
0 to 65535 (unsigned)
Date
System.DateTime
8 bytes
January 1, 1 CE to December 31, 9999
Decimal
System.Decimal
12 bytes
+/-79,228,162,514,264,337,593,543,950,335 with no decimal point; +/-7.9228162514264337593543950335 with 28 places to the right of the decimal; smallest non-zero number is +/-0.0000000000000000000000000001
Double
System.Double
8 bytes
-1.79769313486231E308 to -4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values
Integer
System.Int32
4 bytes
-2,147,483,648 to 2,147,483,647
Long
System.Int64
8 bytes
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Object
System.Object (class)
4 bytes
Any type can be stored in a variable of type Object
Short
System.Int16
2 bytes
-32,768 to 32,767
Single
System.Single
4 bytes
-3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values
String
System.String (class)
10 bytes + (2 * string length)
0 to approximately two billion Unicode characters
User-Defined Type (structure)
(inherits from System.ValueType )
Sum of the sizes of its members
Each member of the structure has a range determined by its data type and independent of the ranges of the other members
.NET Framework types not available in VB.NET: SByte , UInt16 , UInt32 , UInt64 .
Integers
The following table shows correspondences between previous integer types and Visual Basic.NET 7.0 types.
Integer Size
Previous Visual Basic Type and Type Character
Visual Basic.NET 7.0 Type and Type Character
.NET Framework and Runtime Type
16 bits, signed
Integer (% )
Short (none)
System.Int16
32 bits, signed
Long (& )
Integer (% )
System.Int32
64 bits, signed
(none)
Long (& )
System.Int64
Since a data type in VB7 is a wrapper of the corresponding type in the .NET Framework, you can continue writing the following code.
Dim x As Integer
which will translate into the following. Dim x As Int32
Note that on 32-bit systems, 32-bit integer operations are faster than either 16-bit or 64-bit integer operations. This means that in VB7, Integer is the most efficient and fundamental numeric type. You can improve performance in your applications by changing your Long declarations to Integer when you migrate to VB7.
Currency
The Currency data type is not supported in VB7. Instead, there is a new data type named Decimal , which can handle more digits on both sides of the decimal point, for all money variables and calculations. The Decimal data type is also directly supported by the .NET Framework and Runtime.
Variant
In VB6 variants served as the universal data type. In VB7 variants no longer exist. In VB7 Object is the universal data type. All functionality of variants is supplied by Object . Since there is no longer a variant data type, the VarType function -- the function that in VB6 was used to get an integer that indicates the subtype of a variable -- also ceases to exist. As a replacement, you can use the Object class''''s GetType method that returns an object of type Type . You can then use the Type class''''s GetTypeCode method to obtain the TypeCode enumeration. Using the latter, you can then get the type code of an object. You may think this is a much more complicated process, but this can be done in a single line of code.
Imports System Dim aType As Byte '''' replace Byte with any other type Dim TypeCode As Integer '''' an integer to hold the type code TypeCode = Type.GetTypeCode(aType.GetType)
If aType is a Byte , then TypeCode will have a value of 6. If aType is an Integer , Typecode will be 9.
Strings
The VB7''''s String data type is a wrapper for the System.String class that derives directly from System.Object . String manipulation functions in VB6, such as Left$ , Right$ , Mid$ , etc, are replaced by the methods in the System.String class. The following lists changes to the old String variable.
String Declaration
In VB6 you can specify the length of a string in its declaration. This causes the string to have a fixed length.
Dim Name As String * 100
In VB7, you cannot declare a string to have a fixed length. You must declare the string without a length. When your code assigns a value to the string, the length of the value determines the length of the string Dim s As String
s = "Hello World" '''' Length is 11
String Manipulation Functions
Len You can still use this function in your Windows applications, but now you can also use the Length property of the System.String class. Therefore, if s is a String object, you can obtain its length by writing s.Length .
Left$, Right$, Mid$
Left$ , Right$ , Mid$ can be replaced by the Substring method. This method returns a substring of the instance of the current String object. This one method is sufficient to do the operations that you used to achieve with Left$ , Right$ and Mid$ For instance, the following shows a Left$ function and its equivalent of the Substring method. Dim s As String
s = Left$("Hello World", 5) '''' returns "Hello"
is equivalent to Imports System
Dim s As String
s = "Hello World".Substring(0, 5) '''' returns "Hello"
Similarly, the Right$ function is also replaced by the Substring method. The following VB6 code that uses the Right$ function: Dim s As String
s = Right$(Hello World", 5) '''' returns "World"
is equivalent to the following VB7 code: Imports System
Dim s As String, s1 As String
s = "Hello World"
s1 = s.Sub[1] [2] [3] 下一页 [办公软件]GETPIVOTDATA函数语法介绍 [常用软件]桌面搜索新利器 “88Data” [VB.NET程序]定制VB.NET控件编程之拦截击键动作 [VB.NET程序]VB.NET VS C#. [VB.NET程序]VB.NET 中调用浏览目录对话框 [VB.NET程序]VB.NET and C# 语法比较手册 [VB.NET程序]VB.NET 拖动无边框的窗体 [VB.NET程序]C# to VB.NET 翻译器 [VB.NET程序]101 VB.NET Applications 读书笔记(1) [VB.NET程序]vb.net 程序设计规范(1)
|