VB.NET Data Types
发表于2002/4/5 9:20:00 1114人阅读
VB.NET Data Typesby Budi Kurniawan
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).
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|
||4 bytes||True or False|
||1 byte||0 to 255 (unsigned)|
||2 bytes||0 to 65535 (unsigned)|
||8 bytes||January 1, 1 CE to December 31, 9999|
||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|
||8 bytes||-1.79769313486231E308 to -4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values|
||4 bytes||-2,147,483,648 to 2,147,483,647|
||8 bytes||-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807|
||4 bytes||Any type can be stored in a variable of type |
||2 bytes||-32,768 to 32,767|
||4 bytes||-3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values|
||10 bytes + (2 * string length)||0 to approximately two billion Unicode characters|
|User-Defined Type (structure)||(inherits from
||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:
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||
|32 bits, signed||
|64 bits, signed||(none)||
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 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.
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
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.
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)
aType is a
TypeCode will have a value of 6. If
aType is an
Typecode will be 9.
String data type is a wrapper for the
System.String class that derives directly from
System.Object. String manipulation functions in VB6, such as
Mid$, etc, are replaced by the methods in the
System.String class. The following lists changes to the old
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
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
Left$, Right$, Mid$
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
Mid$ For instance, the following shows a
Left$ function and its equivalent of the
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"
Right$ function is also replaced by the
Substring method. The following VB6 code that uses the
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.Substring(s.Length - 5) ' returns "World"
From the two examples, the
Mid$ can easily be replaced by the
Substring method as well.
In VB6, you could use the
Trim$ function as in the following code.
Dim s1 As String, s2 As String s1 = " Hello World " s2 = Trim$(s1)
In VB7, use the
Trim method of the
System.String class, instead.
Imports System Dim s1 As String, s2 As String s1 = " Hello World " s2 = s1.Trim()
LCase$ and UCase$
In VB6, the
LCase$ returns a new string in lower case and the
UCase$ function returns a new string in upper case. These two functions are complemented by the
ToUpper methods of the
String class. As an example,
is the same as
returns the same result as
In VB6, the
Replace function is used to replace part of a string with a substring. For example, the following VB6 code will result in "kingkong."
Dim s As String s = Replace("dingdong", "d", "k") ' s = "kingkong"
String class has the
Replace function that does a similar task. For example, the following VB7 code does the same thing.
Dim s as string = "dingdong".Replace("d"c, "k"c)
Note that the
Replace method of the
String class accepts two
Char arguments, not
You can still use the string manipulation functions:
If s1 = s2 Then
which is the same as writing the following:
If s1.Equals(s2) Then
The StringBuilder Class
System.String class represents an immutable string of characters, just like
String in VB6. This means the value of a
String variable cannot be modified. If you try to modify a
String, a new
String object is created instead. Remember that object creation is an expensive operation. The .NET Framework also provides the
System.Text.StringBuilder class that is more efficient to work with. For more information, see the .NET Framework Reference.
Time statements in VB6. VB7 now uses the
DateTime structure in the
System namespace to represent a date and time value. This structure has a large number of methods and properties that are more than sufficient to work with to get any date and time value. The most important property is probably
Now, which retrieves the current date and time. And then there are
Year properties that you can use to display the date in any format. For example, the following code displays the current date and time in the "dd/mm/yyyy" format.
imports System Module Module1 Sub Main() Dim now As DateTime now = DateTime.Now Console.writeline(now.Day & "/" & _ now.Month & "/" & now.Year) End Sub End Module
DateTime, you will probably use its
Ticks property a lot to retrieve the 100-nanosecond tick count for this instance. This property can replace the
Timer function in VB6 when you want to benchmark an operation, as in the following code.
imports System Dim t1 As Long, t2 As Long t1 = DateTime.Now.Ticks ' operation to be measured here t2 = DateTime.Now.Ticks System.Console.writeline(t2 - t1)
DateTime structure is also convenient to use for some "date-time mathematics." For example, you can add
x hour or
y minutes to an instance of
DateTime using the
As mentioned above, the
Object class in VB7 represents the .NET Framework Object class. This class is the root of every other type in the .NET Framework. In VB7, when you create a new class, that new class implicitly inherits from
Object. Of particular importance are the
ToString methods. The
GetType method enables you to obtain the
Type of the object, and the
ToString method returns a
String that represents the current object.
In VB6, a structure is declared using the
End Type construction. The structure and its members all default to public access. Explicit access declaration is optional. The following example shows a valid structure declaration:
Type Employee ID As Integer FirstName As String LastName As String End Type
In VB7, the
Type statement is not supported. You must declare structures using the
Structure ... End Structure construction. Every member of a structure must have an access modifier, which can be
Protected Friend, or
Private. You can also use the
Dim statement, which defaults to public access. The structure in the preceding example can be declared as follows:
Public ID As Integer ' Must declare access, even if Public.
Dim FirstName As String ' Still defaults to Public access.
Private LastName As String ' Can be made Private inside Structure.
VB7 unifies the syntax for structures and classes. Structures support most of the features of classes, including methods. Structures can serve as light-weight classes that are cheap to create. However, structures do not support inheritance.
Some Other Changes
The following are some important changes that can ease your migration from VB6.
Option Explicit is Imposed by default
You must now declare a variable before using it.
Declare and Assign in the Same Line
You can now declare and assign a variable in the same line. A long-awaited feature.
Dim myVar As Integer = 9
Multiple Variable Declaration
In VB6, you can declare variables of different types in the same statement, but you must specify the data type of each variable or it defaults to
Variant. The following example shows multiple declarations and their resulting data types:
Dim I, J As Integer ' I is Variant, J is Integer. Dim L As Integer, M As Integer ' L is Integer, M is Integer. Dim N As Integer, X As Double ' N is Integer, X is Double.
In VB7, you can declare multiple variables of the same data type without having to repeat the type keyword. The declarations equivalent to those in the preceding example are as follows:
Dim I ' I is Object. Dim J As Integer ' J is Integer. ' -- OR -- Dim I As Object, J As Integer ' I is Object, J is Integer. Dim L, M As Integer ' L is Integer, M is Integer. Dim N As Integer, X As Double ' N is Integer, X is Double.
External Procedure Declaration
In Visual Basic 6.0, when you declare a reference to an external procedure with the
Declare statement, you can specify
As Any for the data type of any of the parameters and for the return type. The
As Any keyword disables type checking and allow any data type to be passed in or returned.
VB7 does not support the
Any keyword. In a
Declare statement, you must specifically declare the data type of every parameter and of the return. This improves type safety. You can overload your procedure declaration to accommodate various return types.
In VB6, any variable declared inside a procedure has procedure scope, so it can be accessed anywhere else within the same procedure. If the variable is declared inside a block -- that is, a set of statements terminated by an
Next statement -- the variable is still accessible from outside the block. The following example illustrates procedure scope:
For I = 1 To 10 Dim N As Long ' N has procedure scope although ' it was declared within a block. N = N + Incr(I) Next W = Base ^ N ' N is still visible outside the block
In VB7, a variable declared inside a block has block scope, and it is not accessible from outside the block. The preceding example can be rewritten as follows:
Dim N As Long ' N is declared outside the block ' and has procedure scope. For I = 1 To 10 N = N + Incr(I) Next W = Base ^ N ' N is visible outside the block but I is not.
However, the lifetime of a variable is still that of the entire procedure. This is true whether the variable has block scope or procedure scope. If you declare a variable inside a block, and if you enter that block several times during the lifetime of the procedure, you should initialize the variable to avoid unexpected values.
Static Local Variables
In VB6, you can declare a procedure with the
Static modifier. This causes every local variable within the procedure to be static and to retain its value between calls. In VB7, the
Static modifier is not supported in a
Sub statement. You must individually declare each local variable you want to be static.
In this article, you have learned the new data types in VB7 that are different from VB6. These are not the only changes in the language, but hopefully this is a good start for VB6 programmers to begin understanding VB7.
- VB.net 连接 MySql,Use MySql.Data.dll
- How To: Use DPAPI to Encrypt and Decrypt Data (C#/VB.NET)
- vb.net data编写，员工ｐａｙｒｏｌｌ
- VB.NET+三层 机房收费系统之组合查询