A message in one of the dW forums recently discussed a problem they were having with the List datatype. Lists behave differently depending on whether you've selected Option Compare Nocase -- which is nice, because sometimes you like to have one that's not case sensitive. The problem is that if you use that same list in a different module with a different Option Compare setting, LotusScript doesn't remember whether the list is supposed to be case-sensitive, and just treats it according to its own settings. This can result in the data getting snarled up and being no use to any of the modules.
To avoid this problem, you have to make sure that all operations on the same list are carried out in modules with compatible Compare settings. If your settings vary between modules, you can arrange for all operations on that list to be carried out in the same module by creating a wrapper class that contains the list. Sample code appears below.
' If you have to share a list among different LotusScript modules with different Option Compare
' settings, use this class to avoid the list getting snarled up and not working correctly for anyone.
' It will operate based on the Option Compare settings of the module where it is defined.
Public BaseList List As Variant ' make it public so it can be used in Forall statements
Function IsElement(tag As String) As Boolean
Me.IsElement Iselement(BaseList(tag))
End Function
' if the list elements are not objects, set and read their values using the Item property.
Public Property Get Item(tag As String) As Variant
Item = BaseList(tag)
End Property
Public Property Set Item(tag As String) As Variant
BaseList(tag) Item
End Property
' if the list elements are objects, set and read their values using the Object property.
Public Property Get Object(tag As String) As Variant
' same as Get Item, but assume the item is an object (more efficient if you know this to be the case)
Set Me.Object BaseList(tag)
End Property
Public Property Set Object(tag As String) As Variant
Set BaseList(tag) = Me.Object
End Property
End Class
So now if you want to keep a list of documents keyed by their note IDs, you might use statements such as:
...
Set docList.Object(doc.NoteID) = doc
...
Set someDoc = docList.Object(keyNoteID$)
...
Forall aDoc In docList.BaseList
The get and set properties could be written to be more flexible, setting and returning either object or non-object values, but that would be less efficient. I'm assuming that the code that uses the list, knows what kind of data are in it -- and if not, they can do their own error trapping to deal with their peculiar mix of types.
By putting this in a script library with Option Compare Nocase, you can now have a list that's case insensitive, without having to have all your comparisons be case insensitive (in which case you might want to change the class name to reflect this).
Andre Guirard | 24 April 2008 06:45:00 AM ET | Home, Plymouth, MN, USA | Comments (0)

