With Block
With ASP programming focusing heavily upon external objects, the following lines of code have become common-place with today's developer:
|
When using an object that requires you set a number of properties, or execute a large number
of methods, your source code can quickly become cluttered. For example, imagine that
objSomeObject needed to have 10 properties set and one method executed to
accomplish the task you need accomplished. Such a feat would require source code like:
|
Egad, the characters objSomeObject sure do appear a large number of times. It
would be nice if we could somehow shrink-wrap this code. With the VBScripting Engine Version
5.0 (download now)
you can use a With block to help tighten up your code.
(Curious as to what version of the VBScripting Engine you are using?
Read Determining the Server-Side Scripting Language and Version
to obtain code you can use to test what version you are using!)
The With block has the following syntax:
|
The With block tightens up your code by not requiring you to explicitly
name the object whose properties and methods you are manipulating. Once inside the
With block, the object is inherently available. For example, in the above
example where we set 10 properties and executed a method, we could have done:
|
Pretty slick, eh? There are some precautionary measures you should take when using the
With block. These warnings are from Microsoft's
technical documentation.
- Once a block is entered, the object can't be changed.
"As a result, you can't use a single
Withstatement to affect a number of different objects." - "You can nest
Withstatements by placing oneWithblock within another. However, because members of outerWithblocks are masked within the innerWithblocks, you must provide a fully qualified object reference in an innerWithblock to any member of an object in an outerWithblock." - "Do not jump into or out of
Withblocks. If statements in aWithblock are executed, but either theWithorEnd Withstatement is not executed, you may get errors or unpredictable behavior."
Happy Programming!




