Using...End Using Statement


Using...End Using Statement

Syntax

     Using resourceExpression...        [statements]     End Using 


resourceExpression (required)

One or more resources to be allocated and used within the Using block. Multiple resources are separated by commas. Resource expressions are similar to what you would see in a Dim statement, either:

     resourceName As type[(arguments)] 

or:

     resourceName As type = expression 

where resourceName is the instance name to use, type is the data type of the resource instance, arguments are the constructor arguments for the data type (if required), and expression is an expression that evaluates to an instance of type.


statements (optional)

Program code to execute using the allocated resource(s).

Description

New in 2005. The Using...End Using block construct is used to execute a series of statements on one or more resource objects, and to have those objects automatically release all of their allocated resources when the block is left in any way. The Using statement is basically a formal method of ensuring that the object's Dispose method is called when the object is no longer needed.

Usage at a Glance

  • Each object in resourceExpression must implement the IDisposable interface.

  • The Dispose method for each allocated resource object will always be called when the code exits the block, even if the block is exited abnormally due to an exception.

  • You cannot jump into or out of a Using block using the GoTo statement. If you need to exit a block early, you can place a label on the End Using line and use a GoTo statement to jump to that line.

  • The various resourceExpression objects cannot be assigned a new instance while in the Using block.

  • You can nest Using blocks. If there are no conditions on the allocation of the nested objects, including those multiple objects in the original Using clause will achieve the same purpose as nesting.

  • If the requested resource cannot be allocated, it will be set to Nothing.

Version Differences

The Using...End Using statement is new with Visual Basic 2005.

See Also

With...End With Statement




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net