Executing a series of statements on a single question
If you have a series of statements that all apply to one question, you may find that using a With...End With block helps you reduce the amount of repetition in the code and makes the code easier to read. This is certainly the case when you are defining the appearance of a question by setting style properties, where each property starts with the words Qname.Style. For example, if you want to specify the font, color and effect for the Unaided awareness question, as well as the orientation and number of rows for the response list, you can type:
HoursTV.Label.Style.Color = "Blue"
HoursTV.Style.Font.Effects = FontEffects.feBold +
FontEffects.feItalic
HoursTV.Style.Font.Family = "Arial"
HoursTV.Style.Orientation = Orientations.orRow
HoursTV.Style.Rows = 3
HoursTV.Ask()
Defining these requirements using With...End With means that you type the repeated words once only and the interviewing program automatically applies them to all the statements in the block.
Syntax for an With...End With block
With Qname
Statements
End With
Parameters
Qname
The question (or other interview scripting object) that the statements in the block apply to.
Statements
The statements that are to executed for the named question or object.
Example
To specify the requirements for the Unaided question, you could type:
With HoursTV
.Label.Style.Color = "Blue"
.Style.Font.Effects = FontEffects.feBold + FontEffects.feItalic
.Style.Font.Family = "Arial"
.Style.Orientation = Orientations.orRow
.Style.Rows = 3
.Ask()
End With
Even though this reduces the amount of repetition, there are still further improvements that can be made. The
With statement can name any object in the Interview Object Model (see
Interview Object model), not just questions, so another change you can make is to have the
With statement refer to the Style object for the
Unaided question, and then move the
Ask and
Label.Style statements outside the block:
With HoursTV.Style
.Font.Effects = FontEffects.feBold + FontEffects.feItalic
.Font.Family = "Arial"
.Orientation = Orientations.orRow
.Rows = 3
End With
HoursTV.Label.Style.Color = "Blue"
HoursTV.Ask()
See also