涕淌居

牛~冬~@~瘋~牛~涕~淌~の~涕~淌~居~~GnuDoyng's Web Log~~~Contact me~niu_dong@msn.com~ICQ:277276843~QQ:123930135~

原创 [译]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.

发表于 @ 2004年12月26日 03:16:00|评论(loading...)

新一篇: [译]Visual Basic 2005在语言上的增强(六)IsNot运算符和TryCast语句 | 旧一篇: [译]Visual Basic 2005在语言上的增强(四)泛型

用户操作
[即时聊天] [发私信] [加为好友]
疯牛涕淌
订阅我的博客
XML聚合  FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
疯牛涕淌的公告
123930135~有蝦米代誌請Q我哦! 本blog已搬家...
文章分类
收藏
百科全書
Britannica
Encyclopedia Mythica
Encyclopedia of Philosophy
Encyclopedia.com
MSN Encarta
WIKIPEDIA
大師之家
Code/Tea/Etc...
Mind View
Panopticon Central
Robert Green's VB Blog
Rockford Lhotka
The Mountain of Worthless Information
The VB Team
VB.NET Expert
侯捷網站
話語資源
American Dialects
Ethnologue
Glossary
IPA HTML Entities
Lexicon of Linguistics
The Linguistic Society of America
Übungen Für Deutsch
Word 2 Word
WordNet
Yamada Language Guides
yourDictionary
技術社區
.NET Framework Windows Forms
ASP.NET Heaven
ASP.NET Web
C# Corner
C# Friends
C# Help
Code Guru
Dev Guru
Developer Fusion
DotNet Zone
Got DotNet
MIT開放式課程網頁
Most Valuable Professional
MSDN
SuperSite for Windows
The Code Project
VB Forums
VB Info Zine
VB.NET Heaven
VBCity
中國 Dot Net Nuke
微軟中國社區
外國朋友
Bram Blogs
Дневник elishena
Добре дошли, приятели!
Заметки обалдевшей от счастья матери
友情鏈接
Amoeba原蟲
kgd jeyp
Let's Eat Noodles at Last
Nature Is Restless
YOYO Forever
三生石坊
去日留痕
咖啡愛我
夢想飛翔
天高任我飛
奔騰不息
宜軒品
寧靜*海豚灣
形而上的憂傷
我愛茉莉
我的生活
既生甲何生乙
明天的寂寞
沫言沫語
異域星空
裝配中的腦袋
閩中龍
阿貴
風一代
與我有關
Linguistics Paradise
UÕH UỎNG SANG
閩東語論壇
只是路過
安妮寶貝
微微微笑
存档
软件项目交易
Csdn Blog version 3.1a
Copyright © 疯牛涕淌