[譯]Visual Basic 2005在語言上的增強(十二)默認實例和編譯器警告

默認實例

另一個對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.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章