Replace an Element in an Array

This function will substitute a single element in an array with a replacement value.

How It Works

The loop counter variable called i is initialized to zero. The function then loops through each element in the search array and compares the current element to the target value. If the element is found, the element value is replaced with the new value. All values are stored in a temporary array called ReturnArray, which is subsequently returned to the calling code.

Implementation

To implement this technique, add the following to a LotusScript library. Three values must be passed to this functionan array of values, the search value, and the replacement value.

Function ReplaceElement ( varFromList As Variant, varTarget As Variant,
varReplacement As Variant) As Variant

 Redim ReturnArray(0) As Variant
 Dim i As Integer
 i = 0

 Forall varElement In varFromList
 Redim Preserve ReturnArray (i) As Variant
 If varElement = varTarget Then
 ReturnArray(i) = varReplacement
 Else
 ReturnArray(i) = varElement
 End If
 i = i + 1
 End Forall

 ReplaceElement = ReturnArray

End Function

The following illustrates how this function could be called from an action button. In this example, team member Tom is replaced with team member Tammy (see Figure 13.1). The updated array is subsequently returned assigned to the newTeam array.

Sub Click(Source As Button)

 Dim newTeam As Variant
 Dim theTeam (0 To 2) As String
 theTeam (0) = "Tom"
 theTeam (1) = "Henry"
 theTeam (2) = "Mark"
 Msgbox Implode( theTeam, ", " ), , "Old Team"
 newTeam = ReplaceElement ( theTeam, "Tom", "Tammy" )
 Msgbox Implode ( newTeam, ", "), , "New Team"

End Sub

Figure 13.1. Replace an element in an array

Remember to include the Use LIBRARY statement in the calling design element if the function is stored in a LotusScript library.

Note

The ArrayReplace function could also be used to achieve a similar result. Refer to the Domino Designer help for additional information pertaining to this function.


Remove a Character from a String

An Introduction to the Lotus Domino Tool Suite

Getting Started with Designer

Navigating the Domino Designer Workspace

Domino Design Elements

An Introduction to Formula Language

An Introduction to LotusScript

Fundamentals of a Notes Application

Calendar Applications

Collaborative Applications

Reference Library Applications

Workflow Applications

Web Applications

Design Enhancements Using LotusScript

Design Enhancements Using Formula Language

View Enhancements

Sample Agents

Miscellaneous Enhancements and Tips for Domino Databases

Data Management

Security

Application Deployment and Maintenance

Troubleshooting

Appendix A. Online Project Files and Sample Applications

Appendix B. IBM® Lotus® Notes® and Domino®Whats Next?



Lotus Notes Developer's Toolbox(c) Tips for Rapid and Successful Deployment
Lotus Notes Developers Toolbox: Tips for Rapid and Successful Deployment
ISBN: 0132214482
EAN: 2147483647
Year: N/A
Pages: 293
Authors: Mark Elliott

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net