OpenNTF.org - Assigning Initial Values To An
    Advanced
   OpenNTF Code Bin
Edit Document Code By Date > Code Document
About This Code
Brief Description:
Assigning Initial Values To An Array 
Rating:
Not Rated Yet 
Contributor:
Peter D Presnell 
Category:
Lotusscript 
Type:
Example Code 
Notes Version:
R6.x, R7.x 
Last Modified:
16 Jan 2008 
OpenNTF Disclaimer

All of the program code and information presented in the OpenNTF.org Code Bin are provided "as-is", and should be used at your own risk. OpenNTF.org make no express or implied warranty about anything in the Code Bin, and OpenNTF.org will not be responsible or liable for any damage caused by the use or misuse of anything from this site. OpenNTF.org makes no guarantees about anything. Please thoroughly test all of the knowledge and code you find here before you attempt to use them in your production environment.

Code / Description
Most programming languages support an away to initialize an array in a single statement, but not LotusScript. Maybe it is just me doing this, but I have always found myself writing LS code something like


Dim Month(12) As String
Month(1) = "JAN"
Month(2) = "FEB"
...
Month(12) = "DEC"

Eventually this bugged me enough that I searched for a way to initialize an array such as the above in a single statement. To make my code "easy" to read I have created an Array function which takes a string and evaluates it using @Formulae (which does allow a "List" to be assigned).

I can now write a single LS statement such as

Months = Array({"JAN":"FEB":"MAR":"APR":"MAY":"JUN":"JUL":"AUG":"SEP":"OCT":"NOV":"DEC"})

This works for both text and numeric values.

The code for the Array function is as follows:-

'/**
' * Returns an array populated with initial values
' *
' * @author Peter Presnell
' * @param Source The initial values separated by colon
' * @return An array created from the initial values
' */
Function Array(Source As String) As Variant
Dim MyArray(0) As String
Try:
On Error Goto Catch
Array = Evaluate(Source$)
Exit Function
Catch:
Stop
MyArray(0) = Source$
Array = MyArray
Exit Function

End Function

Usage / Example
Dim Months As Variant
Months = Array({"JAN":"FEB":"MAR":"APR":"MAY":"JUN":"JUL":"AUG":"SEP":"OCT":"NOV":"DEC"})

Dim DaysInMonth As Variant
DaysInMonth=Array({31:28:31:30:31:30:31:31:30:31:30:31})
 Comments
Posted by Remko v Laarhoven on 01/25/2008 05:39:31 AMOr
Function Array( source As String) As Variant
Array = Split(source,":")
End Function
Posted by bruce lill on 02/07/2008 02:22:59 PMSplit works only with strings
you would have to be sure to use only strings in the array
 Add your comment!