[译]Visual Basic 2005在语言上的增强(五)Using、Continue语句及Global关键字

类别:.NET开发 点击:0 评论:0 推荐:
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.

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