string(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 上一页 [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)
|