Domino Code Fragment

Code Name*
Conditional Branching: The ForAll Statement
Date*
04/28/2024
Source (or email address if you prefer)*
Rlatulippe@romac.com
IP address:.18.217.208.72
Description*
The ForAll statement was made especially for arrays. It has the ability to operate on all the elements in an array without you knowing how many elements are in the array.
Type*
LotusScript
Categories*
(Misc)
Implementation:
Required Client:
Server:
Limitations:
Comments:
Files/Graphics attachments (if applicable): Code:
In order to understand the ForAll statement, you must have a good understanding of Arrays. If you are not familiar with arrays,
use this doclink to go to the chapter entitled Arrays and Redim to learn about arrays before learning about ForAll. Database 'Formulas & Scripts Design', View 'LotusScripts'

When using the For statement, a number has to be used to tell the For statement how many times to execute the loop. This number is either predefined or entered by a user. For instance, a program might ask a user how many deposits the user would like to make. The user enters the number 5, and the program proceeds to ask for 5 deposits. Obviously, the number 5 was used in a For loop.

Using the For loop is just fine in the case above, but what happens when you want to run a loop on an array and you do not know how many elements are in the array? The first answer that probably comes to mind is to use the UBound function to find out the size of the array. That answer is correct, but there is an easier way using the ForAll statement.

The ForAll statement was made especially for arrays. It has the ability to operate on all the elements in an array without you knowing how many elements are in the array. For example, if you have an array with 148 elements, and want to check each value in the array for something, you can use the ForAll statement, and it will automatically walk through each element in the array.

Syntax
ForAll refvar in arrayname
statements
End ForAll

The only trick to the ForAll statement is the refvar. Refvar stands for reference variable. You never declare it, and it should have a unique name from any other variables in your script. Refvar is the part of ForAll which keeps track of which element is being used. Simply put, make up a name for refvar that you will never use again in the program.

As an example, the code below has an array that has 7 elements. The ForAll statement is going to ask for values for each element in the array, even though there is no code checking to see how big the array is. Notice the name for refvar, Foo, was never declared.

Sub Click(Source As Button)
Dim MyArray$(1 to 7)
'Declare MyArray with a size of 7
Forall Foo In MyArray
'Use ForAll with a refvar of Foo to traverse the entire array
Foo = Inputbox$("Please enter a name")
'For each element in the array, ask for a name
End Forall
'End the ForAll loop
Forall Foo In MyArray
'Use ForAll with a refvar of Foo to traverse the entire array
Messagebox Foo
'Display the contents of each element using the refvar Foo
End Forall
'End the ForAll loop
End Sub


Click here to view the example.