[譯]Visual Basic 2005在語言上的增強(五)Using、Continue語句及Global關鍵字

Using語句

Using語句是獲取一個對象、執行代碼、並迅速地釋放對象的捷徑。框架(指.NET Framework,涕淌注)下有很多的對象,譬如graphics、file handles、communication ports和SQL Connections都需要你自行釋放這些對象,以避免應用程序中出些了內存的泄漏。假設你想用brush對象來畫一個矩形:
Using g As Graphics = Me.CreateGraphics()
    Using br As System.Drawing.SolidBrush = New SolidBrush(System.Drawing.Color.Blue)
        g.FillRectangle(br, New Rectangle(30, 50, 230, 200))
    End Using
End Using

在你用完了graphics和brush對象後,你就必須銷燬它們,而Using語句使得這一切都易如反掌了。比起你曾經在Visual Basic .NET裏使用Try/Catch語句然後在Finally塊中釋放對象的方式,Using語句都簡易清爽的多。

Continue語句

Continue語句能夠直接跳到下一次循環的開始,使得循環的邏輯更精煉,可讀性也更強了:
Dim j As Integer
Dim prod As Integer
For i As Integer = 0 To 100
    j = 0
    While j < i
        prod = i * j
        If prod > 5000 Then 
            '跳到下一個For的值            
            Continue For
        End If
        j += 1
    End While
Next

Continue語句非常簡潔,並且使得程序跳出內層循環變得更加容易,而不需要求助於一個語句標籤和一個Goto語句。Continue語句能夠作用於For、While、和Loop循環。

Global關鍵字

Global關鍵字能迅速地訪問到命名空間層次最頂層的根命名空間或是空命名空間。在過去,你不可能在你公司的命名空間層次內部定義一個System.IO:
Namespace MyCompany.System.IO

這樣做將會把應用程序對框架下的System命名空間的引用搞得一團糟。但現在好了,你可以使用Global關鍵字來消除命名空間的二義性:
Dim myFile As Global.System.IO.File

在代碼生成時,如果你想確保生成命名空間的引用都絕對地符合你的思路,那麼Global關鍵字就尤其有用。我想你會發現,Visual Basic自己就在所有生成的代碼裏使用Global關鍵字。


@以下是原文供大家參考@
Using Statement

The Using statement is a shortcut way to acquire an object, execute code with it, and immediately release it. A number of framework objects such as graphics, file handles, communication ports, and SQL Connections require you to release objects you create to avoid memory leaks in your applications. Assume you want to draw a rectangle using a brush object:
Using g As Graphics = Me.CreateGraphics()
    Using br As System.Drawing.SolidBrush = New SolidBrush(System.Drawing.Color.Blue)
        g.FillRectangle(br, New Rectangle(30, 50, 230, 200))
    End Using
End Using

You want to dispose of the graphics and brush objects once you're done using them, and the Using statement makes doing this a snap. The Using statement is much cleaner than using Try / Catch and releasing the object in the Finally block as you have to in Visual Basic .NET.

Continue Statement

The Continue statement skips to the next iteration of a loop, making the loop logic more concise and easier to read:
Dim j As Integer
Dim prod As Integer
For i As Integer = 0 To 100
    j = 0
    While j < i
        prod = i * j
        If prod > 5000 Then 
            'skips to the next For value           
            Continue For
        End If
        j += 1
    End While
Next

The Continue statement is very clean and makes escaping the inner loop quite easy without resorting to a label and a Goto statement. The Continue statement operates on For, While, and Do loops.

Global Keyword

The Global keyword makes the root, or empty, namespace at the top of the namespace hierarchy accessible. In the past, you could not define a System.IO namespace within your company's namespace hierarchy:
Namespace MyCompany.System.IO

Doing so would mess up all references to the framework's System namespace within your application. You can now use the Global keyword to disambiguate the namespaces:
Dim myFile As Global.System.IO.File

The Global keyword is especially useful in code generation scenarios where you want to be absolutely sure that generated namespace references point to what you intend. I expect that you'll see Visual Basic itself use the Global keyword in all generated code.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章