Published: Thursday, January 06, 2000
Using the With Block
With ASP programming focusing heavily upon external objects, the following lines of
code have become common-place with today's developer:
Dim objSomeObject
Set objSomeObject = Server.CreateObject("Some.Object")
objSomeObject.SomeProperty = "SomeValue"
objSomeObject.SomeMethod
...
|
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:
objSomeObject.SomeProperty0 = "SomeValue"
objSomeObject.SomeProperty1 = "SomeValue"
objSomeObject.SomeProperty2 = "SomeValue"
objSomeObject.SomeProperty3 = "SomeValue"
objSomeObject.SomeProperty4 = "SomeValue"
objSomeObject.SomeProperty5 = "SomeValue"
objSomeObject.SomeProperty6 = "SomeValue"
objSomeObject.SomeProperty7 = "SomeValue"
objSomeObject.SomeProperty8 = "SomeValue"
objSomeObject.SomeProperty9 = "SomeValue"
objSomeObject.SomeMethod
...
|
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:
With objectReference
Statements
End With
|
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:
With objSomeObject
.SomeProperty0 = "SomeValue"
.SomeProperty1 = "SomeValue"
.SomeProperty2 = "SomeValue"
.SomeProperty3 = "SomeValue"
.SomeProperty4 = "SomeValue"
.SomeProperty5 = "SomeValue"
.SomeProperty6 = "SomeValue"
.SomeProperty7 = "SomeValue"
.SomeProperty8 = "SomeValue"
.SomeProperty9 = "SomeValue"
.SomeMethod
End With
...
|
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
With statement to affect a number of
different objects."
- "You can nest
With statements by placing one With block
within another. However, because members of outer With blocks are masked
within the inner With blocks, you must provide a fully qualified object
reference in an inner With block to any member of an object in an outer
With block."
- "Do not jump into or out of
With blocks. If statements in a With
block are executed, but either the With or End With statement is
not executed, you may get errors or unpredictable behavior."
There you have, from the horse's mouth.
Happy Programming!