分享
 
 
 

VB.NET Data Types

王朝vb·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

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).

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.Substring(s.Length - 5) ' returns "World"

From the two examples, the Mid$ can easily be replaced by the Substring method as well.

Trim$

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 ToLower() and ToUpper methods of the String class. As an example,

LCase$("Hello World")

is the same as

"Hello World".ToLower()

and

UCase$("Hello World")

returns the same result as

"Hello World".ToUpper()

Replace

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"

The 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 String arguments.

String comparison

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

The 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.

Dates

Forget the Date and 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 Day, Date, Month and 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

When using 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)

The 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 AddHours and AddMinutes methods.

Object

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 GetType and 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.

Structure

In VB6, a structure is declared using the Type...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 Public, Protected, Friend, 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:

Structure Employee

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.

End 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.

Variable Scopes

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 End, Loop, or 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 Function or Sub statement. You must individually declare each local variable you want to be static.

Summary

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.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有