GetList 返回内部使用的list
GetKeys 返回list的键为一个变量数组
GetValues 返回list的值为一个变量数组
%REM Class Dictionary Description: Comments for Class %END REM Public Class Dictionary Private dic List As Variant Private count As Long %REM Sub New Description: Comments for Sub %END REM Sub New() End Sub Sub Delete() Erase dic End Sub %REM Function GetList Description: Comments for Function %END REM Public Function GetList() GetList=dic End Function %REM Function GetKeys Description: Get the keys in the list as a variant array %END REM Public Function GetKeys() Dim keys As New NArray(-1) ForAll v In dic keys.Add(ListTag(v)) End ForAll GetKeys=keys.container End Function %REM Function GetValues Description: Get the values in the list as a variant array %END REM Public Function GetValues() Dim values As New NArray(-1) ForAll v In dic values.Add(v) End ForAll GetValues=values.container End Function %REM Function Add Description: Comments for Function %END REM Public Function Add(key As String, value As Variant) If IsObject(value) Then Set dic(key)=value Else dic(key)=value End If End Function %REM Function Item Description: Comments for Function %END REM Public Function Item(key As String) As Variant 'Handle the ErrListItemDoesNotExist error explicitly If Not me.Contains(key) Then Exit Function End If If IsObject(dic(key)) Then Set Item=dic(key) Else Item=dic(key) End If End Function %REM Function RemoveItem Description: Remove the element associated with the key 'Remove' cannot be used as the function name %END REM Public Function RemoveItem(key As String) Erase dic(key) End Function %REM Function Clear Description: Comments for Function %END REM Public Function Clear() Erase dic End Function %REM Function Contains Description: Comments for Function %END REM Public Function Contains(key As String) As Boolean If IsElement(dic(key)) Then Contains=True Else Contains=False End If End Function End Class
在一个代理中测试这个自定义类。
Public Function Test() Dim d As New Dictionary Call d.Add("first", 1) logger.info(d.Contains("first")) logger.info(d.Contains("second")) logger.info(d.Item("first")) Call d.RemoveItem("first") logger.info(d.Contains("first")) End Function其中,logger是该代理中使用的另一个用于记录日志的类的实例。测试结果如下:
11/2/2011 10:20:58 AM[INFO] - True
11/2/2011 10:20:58 AM [INFO] - False
11/2/2011 10:20:58 AM [INFO] - 1
11/2/2011 10:20:58 AM [INFO] - False
与使用LotusScript自带的处理List的语法规则相比,上述的自定义类在使用时更加方便和符合面向对象的程序的惯例,这还仅仅是小小的便利和习惯上的偏好,并没有完全体现面向对象的编程范式的优点,而这些优点,都可以利用新的LotusScript编辑器充分发挥。