VB.NET Data Types[2]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 sonicdater 的 blog

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$

本文关键:VB.NET Data Types
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top