A control statement that causes all the variable and property references within it to first assume that they are properties of the specified object.

Syntax

WITH <oRef>
 <statement block>
ENDWITH

<oRef>

A reference to the default object.

<statement block>

A statement block that assumes that the specified object is the default.

ENDWITH

A required keyword that marks the end of the WITH structure.

Description

Use WITH when working with multiple properties of the same object. Instead of using the object reference and the dot operator every time you refer to a property of that object, you specify the object reference once. Then every time a variable or property name is used, it is first checked to see if that name is a property of the specified object. If it is, then that property is used. If not, then the variable or property name is used as-is.

You cannot take advantage of the WITH syntax to create properties. For example:

with someObject

existingProperty = 2 

newProperty = existingProperty 

endwith

Suppose that existingProperty is an existing property of the object, and newProperty is not. In the first statement in the WITH block, the value 2 is assigned to the existing property. Then in the second statement, newProperty is treated like a variable, because it does not exist in the object. The statement creates a variable named newProperty, assigning to it the value of the existingProperty property.

Method name conflicts

You may encounter naming conflicts when calling methods inside a WITH block in two ways:

The name of the method matches the name of a built-in function. The built-in function takes precedence. For example, you create a class with a method center( ) and try to call it within a WITH block:

with x

center( ) 

// other code 

endwith

The CENTER( ) function would be called. It expects parameters, so you’ll get a runtime error. You might check your center( ) method, which has no parameters, and wonder what’s going on.

It may be possible to call your method by using the explicit object reference, which is normally redundant in a WITH block, and will not work if the object happens to have a property with the same name as the object reference. For example, you could call your center( ) method like this:

with x

x.center( ) 

// other code 

endwith

If the object x happens to have a property named x, then you would have to create a temporary duplicate reference that does not have the same name as the any other property of x outside the WITH block first:

y = x

with x

y.center( ) 

// other code 

endwith

The name of the method matches the first word of a command. For example, if the object f has a method named open( ), the method call with the dot operator would look like:

f.open( )

Using WITH, it would be:

with f

open( ) 

endwith

but that code will not work because the name of the method matches the first word in a dBL command; there are some commands that start with the word OPEN. When the compiler sees the word OPEN, it considers that statement to be a command starting with that keyword, and looks for the rest of the command; for example, OPEN DATABASE. When it doesn’t find the rest of the command, it considers the statement to be incorrect and generates a compile-time error.

To call such a method inside a WITH block, you may use an explicit object reference as shown above, or change the statement from a direct method call to an indirect method call—an assignment or through the EMPTY( ) function. Many methods return values. By assigning the return value of the method call to variable, even a dummy variable, you bypass the naming conflict. For example, with another object that has a copy( ) method (there are several commands that begin with the word COPY):

with x

dummy = copy( ) // As long as x does not have property named dummy! 

endwith

For methods that don’t return values, you may use the EMPTY( ) function, which will safely "absorb" the undefined value:

with x

empty( copy( ) ) 

endwith