[Translation] Visual Basic 2005 Enhancements in Language (5) Using, Continue Statements and Global Keys

xiaoxiao2021-03-06  41

Using statement

The using statement is a shortcut to get an object, execute code, and quickly release the object. There are many objects (referred to as .NET Framework, 淌), such as Graphics, File Handles, Communication Ports, and SQL Connections require you to release these objects yourself to avoid leaks of memory in your application. Suppose you want to draw a rectangle with 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

After you run over the graphics and brush object, you must destroy them, and

The USING statement makes it easy for this. Compared to you have used the try / catch statement in Visual Basic .NET and then release the object in the FINALLY block.

The USING statement is simple and refreshing.

Continue statement

The Continue statement can jump directly to the beginning of the next loop, making the logic of the loop more refined, readability is also stronger:

DIM J

As integer

DIM PROD

As integer

For i

As integer = 0

TO 100

J = 0

While J

PROD = i * j

IF prod> 5000

THEN

'Value of jumping to the next for

Continue for

END IF J = 1

End while

NEXT

The Continue statement is very simple and makes the program jumps out of the inner layer loop easier, without having to help a statement tag and one

GOTO statement. The Continue statement can act on For, While, and Loop loops.

Global keyword

Global Keywords can quickly access the topmost root namespace of the namespace, or a space namespace. In the past, you can't define a system.io in the hierarchy of your company's namespace:

Namespace mycompany.system.io

Doing so will make the application to a reference to the SYSTEM namespace under the framework. But now, you can use it.

Global keywords to eliminate the second meaning of namespace:

DIM Myfile

AS

Global.system.IO.FILE

At the time of code generation, if you want to make sure that the reference to generate a namespace is absolutely in line with your ideas, then

The global keyword is especially useful. I think you will find that Visual Basic uses it in all generated code.

Global keyword.

@ 以下 below is original for your reference @

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 overce 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 Releaseing 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

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 name, you.................................... ..

Namespace mycompany.system.io

Doing So Would Mess Up All References To The Framework's System Namespace Wtem Application. You Can Now Use Theglobal 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.

转载请注明原文地址:https://www.9cbs.com/read-76552.html

New Post(0)