.NET开发

本类阅读TOP10

·NHibernate快速指南(翻译)
·vs.net 2005中文版下载地址收藏
·【小技巧】一个判断session是否过期的小技巧
·VB/ASP 调用 SQL Server 的存储过程
·?dos下编译.net程序找不到csc.exe文件
·通过Web Services上传和下载文件
·学习笔记(补)《.NET框架程序设计(修订版)》--目录
·VB.NET实现DirectDraw9 (2) 动画
·VB.NET实现DirectDraw9 (1) 托管的DDraw
·建站框架规范书之——文件命名

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
[译]Visual Basic 2005在语言上的增强(五)Using、Continue语句及Global关键字

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

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.


相关文章

相关软件