[译]Visual Basic 2005在语言上的增强(十二)默认实例和编译器警告

类别:.NET开发 点击:0 评论:0 推荐:

默认实例

另一个对Visual Basic .NET的改变就是窗体的默认实例,不少从Visual Basic 6.0转向.NET开发的人员因为找不到窗体的默认实例而感到困惑。你必须在使用一个窗体对象时先创建它的实例:
Dim frm As New Form2
frm.Show()

Visual Basic 2005支持了默认实例,所以你可以使用类似下面的句法:
Form2.Show()

如果你能通过My.Forms集合来访问默认窗体实例那就更好了:
My.Forms.Form2.Show()

编译器警告
Visual Basic 2005支持编译器警告,这便允许你在程序运行时出乱子之前就尽可能地把问题搞定。警告通过在代码下划绿色波浪线的方式给出(而错误是通过蓝色波浪线给出的)。

编译器警告包括了递归属性访问,Catch语块或Case语块重叠,创建了一个没有返回值的函数,等等。我最喜欢举的警告例子就是引用一个尚未初始化的对象:
Dim cust As Customer
If cust.Name.Length = 0 Then
    '...
End If

在这里,后台编译器就会在If语句的cust下划上绿色波浪线。警告文字是这样写的:“variable 'cust' is used before it has been assigned a value.”(变量“cust”未被赋值就被使用)。一个空值引用异常将在程序运行时被引发。数不清有多少次当程序运行时我在我的代码中找到这种类型的错误,而现在,编译器能够在编译时就发现这些错误了。

顺便提醒一下,当你在代码编辑器里敲入上面的代码时,编译器最初会在Dim语句中的cust下划上绿色波浪线并给出一条“unused local variable 'cust'.”(未使用的局部变量“cust”)。一开始你会觉得这蛮讨厌的,因为你只是刚刚加上这个变量罢了,但是这项功能最终却能确保代码的简洁。

所有的错误不再是被统一地显示在IDE里的Task List里了,而是用一个新的Errors List窗口来分离错误、警告和信息(参见图2和图6)。你可以在应用程序设计器的Compile面板里自行设置编译器是否标记警告或错误,面板里的复选框控件可以用来控制是关闭所有的警告还是把所有警告都视为错误。

使用Option Strict On语句可以用来自动生成多种情况的编译时错误。如果你试图隐式地进行收缩转换(有可能导致数据损失),Option Strict将在代码中标记出来:
Dim iValue As Integer
Dim lValue As Long
iValue = lValue '收缩转换错误
lValue = iValue '正确

如果使用晚期绑定你也会得到一个错误。一个对象的晚期绑定就是说通过Object类型的变量进行成员的访问:
Dim myObj As Object
Call myObj.method1() '晚期绑定错误

现在在Visual Studio中使用Option Strict来写程序是一个很好的习惯。只要可能,你都应该在Tools | Options菜单里打开Option Strict,方法是选中Project设置下Option Strict对应的复选框。或者,你可以在某个类或模块文件的顶端写上Option Strict On语句。


@以下是原文供网友参考@


Default Instances

Another change to Visual Basic .NET that has tripped up many developers migrating from Visual Basic 6.0 is the lack of a default instance for forms. You need to create an instance of the form before using it:
Dim frm As New Form2
frm.Show()

Visual Basic 2005 supports form default instances, so you can use the familiar syntax:
Form2.Show()

It's best if you access this default form instance through the My.Forms collection:
My.Forms.Form2.Show()


Compiler Warnings

Visual Basic 2005 supports compiler warnings, which give you a heads-up on issues that may cause problems at run time. A warning is shown as a green squiggly line under your code (errors are shown as blue squiggles).

Compiler warnings include recursive property access, overlapping catch blocks or case statements, creating a function without a return value, and others. My favorite warning is for a variable reference on an uninitialized object:
Dim cust As Customer
If cust.Name.Length = 0 Then
    '...
End If

Here the background compiler puts a green squiggly line under cust in the If statement. The warning text displayed reads "variable 'cust' is used before it has been assigned a value." A null reference exception could result at run time. I don't know how many times I've found this type of error in my code at run time, and now the compiler finds these errors at compile time.
Incidentally, as you type the code above in the editor, the compiler initially puts a green squiggly under cust in the Dim statement with the message "unused local variable 'cust'." At first this seems a bit annoying because you've just added the variable, but it helps you keep your code cleaner in the end.

Instead of showing all errors in the Task List in the IDE, there's a new Errors List window that separates messages into errors, warning, and messages (see Figure 2 and Figure 6). You have some control over whether the compiler flags warnings or errors on the Compile tab in the application designer, which has checkboxes for either disabling all warnings or for treating all warnings as errors.

Use the Option Strict On statement to generate compile-time errors for several scenarios. Option Strict will flag your code if you attempt an implicit narrowing conversion (one in which data could be lost):
Dim iValue As Integer
Dim lValue As Long
iValue = lValue 'narrowing conversion error
lValue = iValue 'error

You also get an error if you use late binding. An object is late bound when it is assigned to a variable of type Object:
Dim myObj As Object
Call myObj.method1() 'late binding error

Use of Option Strict is now considered a best practice for programming with Visual Studio. You should turn on Option Strict in your code wherever possible. You can turn on Option Strict in the Tools | Options menu, under the Project settings by selecting the checkbox for Option Strict. You can also put the Option Strict On statement at the top of an individual class or module file.

本文地址:http://com.8s8s.com/it/it41968.htm