Domino Code Fragment

Code Name*
Arrays and Redim
Date*
04/29/2024
Source (or email address if you prefer)*
Rlatulippe@romac.com
IP address:.3.149.230.44
Description*
Arrays, like variables, hold values of a single data type. Unlike variables, however, arrays can hold multiple elements of the same data type.
Type*
LotusScript
Categories*
(Misc)
Implementation:
Required Client:
Server:
Limitations:
Comments:
Files/Graphics attachments (if applicable): Code:
The syntax for declaring an array is as follows:

Dim arrayname(1 to numberofelements) As data type

Declaring an array is similar to declaring a variable, except an array declaration always includes parenthesis after the array name, either with or without lower and upper bounds. Since the elements of an array are accessed through an index number, Notes must know what the smallest and largest index numbers are going to be. These numbers are represented by the lower and upper bounds. If an array is declared with bounds 1 to 4, the index numbers for the array would be 1,2,3,4. For the declaration:

Dim Fruits(1 to 4) As String

and array would be created as such:

Array Fruits:
Value
Index #
Apple
1
Orange
2
Pear
3
Cherry
4

To retrieve the value apple, use the following technique:

Dim Food As String
Food = Fruits(1)

To replace the value Pear with Mango, use the following code:

Fruits(3) = "Mango"

It is important to realize that the declaration of bounds sets up the index numbering for the array. By default, LotusScript starts an array index at zero. If you were to declare an array as such:

Dim Fruits(5) As String

Notes would set up the array index as 0, 1, 2, 3, 4; five elements, beginning with zero. For this reason, when retrieving properties from Notes classes through an array that LotusScript has created automatically, begin retrieving values at index zero, not one.

You can use the For statement to retrieve all of the values of an array, as long as you know how large the array is. Consider the following example:

Click here to view the example.

Dim X As Integer 'Declare X as type Integer
Dim Fruits(1 To 4) As String
'Declare Fruits as an array of 4 positions and type string
For X = 1 To 4
'Run the following code 4 times
Fruits(X) = Inputbox$("Enter a fruit","Enter a fruit")

'Ask the user for the name of a fruit
Next 'Loop
For X = 1 To 4
'Run the following code 4 times
Messagebox Fruits(X)
'Display the Fruits entered above by position X
Next 'Loop

If you are not sure how large an array is going to be, you can declare the array with no bounds, and then later on change the size of the array on the fly using the Redim statement. The only problem with using Redim is that by default, you lose whatever information is in the array when the size is changed. To avoid this, use the keyword Preserve when redimming the array. Preserve will keep your data intact so that nothing is lost when the array size is changed.

Click here to view the example.

Dim Fruits() As String 'Declare Fruits as an array and type string
Dim NumFruits As String
'Declare NumFruits as type string
Dim I As Integer
'Declare I as type integer
I = 1
'Initialize I to 1
NumFruits = Inputbox$("Enter a fruit name, or nothing to quit.","")

'Ask for the name of a Fruit and store in Num
While NumFruits <> ""
'Loop while NumFruits is not equal to "" (no entry)
Redim Preserve Fruits(1 To I)
'Redim Fruits to I as the upper bound, and Preserve existing entries
Fruits(I) = NumFruits
'set Fruit position I to the value of NumFruits
For X = 1 To I 'Run the following code I times
Messagebox Fruits(X)
'Display the value in Fruits at X position
Next
'Loop
I = I + 1
'Increment I by 1
NumFruits = Inputbox$("Enter a fruit name, or nothing to quit.","")

'Ask for the name of a Fruit and store in NumFruits
Wend 'Loop