WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

VB.NET Data Types
Pages: 1, 2, 3

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

Comment on this articleWhat are your experiences with migrating from VB 6 to VB .NET?
Post your comments

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.

Pages: 1, 2, 3

Next Pagearrow