我有这样的代码:
Dim MyACL As Variant
Dim Person As List
Redim MyACL(0)
Person("Detail1") = "Something1"
.
.
.
Person(Detailx") = "Somethingx"
ForAll name in names
ReDim Preserve MyAcl(Ubound(MyACL)+1)
Person("Name") = name
MyACL = ArrayAppend(MyACL,Person)
End ForAll
抛出错误“Type Mismatch”。你知道吗,如何创建一个列表数组?谢谢。
答案 0 :(得分:6)
这是您想要使用类的典型示例,并创建该类的数组。反过来,该类可以包含列表(以及其他内容)。可以非常强大!
<强>更新强>
使用类的好处是您可以在类中添加业务逻辑,并且以后可以使用更多功能扩展它。以下是基于上述问题的示例,但具有其他功能。
Class PersonObject
Public PersonAttribute List As String
Public NABdoc As NotesDocument
Public PersonName As String
Public Sub New(personname As String)
Dim nab as New NotesDatabase("Server/Domain","names.nsf")
Dim view as NotesView
'*** Get person document from Domino directory
Set view = nab.GetView("PeopleByFirstName")
Set me.NABdoc = view.GetDocumentByKey(personname)
'*** Set person name in object
me.PersonName = personname
'*** Set some values from person doc
me.PersonAttribute("Email") = GetValue("InternetAddress")
me.PersonAttribute("Phone") = GetValue("OfficePhone")
End Sub
Public Function GetValue(fieldname as String) as String
GetValue = me.NABdoc.GetItemValue(fieldname)(0)
End Function
Public Sub AddAttribute(attributename as String, value as string)
me.PersonAttribute(attributename) = value
End Sub
End Class
现在,您可以使用此类轻松构建列表(并假设 names 是唯一名称列表):
Dim person List As PersonObject
Dim personname As String
ForAll n in names
'*** Create person object based on name
person(n) = New PersonObject(n)
'*** Store additional info about this person
person.AddAttribute("Age","35")
End ForAll
希望这可以让您了解如何使用课程。
您还可以查看以下两篇关于面向对象Lotusscript基础知识的博客文章:
http://blog.texasswede.com/object-oriented-lotusscript-for-beginners-part-1/
http://blog.texasswede.com/object-oriented-lotusscript-for-beginners-part-2/
答案 1 :(得分:2)
如果明确地将变量声明为Array(就像在Redim语句中那样),则无法使用arrayappend“重新分配”它。
没有必要这样做。只需将MyACL = ArrayAppend(MyACL,Person)
替换为MyACL(Ubound(MyACL)) = Person
注意:使用该示例代码,您将永远不会填充MyACL(0),因为填充的第一个元素是MyACL(1)
要开始使用元素0填充数组,需要像下面这样更改代码:
Dim max As Integer
max = 0
ForAll thisName In names
ReDim Preserve MyAcl(max)
Person("Name") = thisName
MyACL(max) = Person
max = max + 1
End ForAll
但是:我不知道,如果这是一个好主意,因为你不能直接访问Person的“Detail1- Property”。
像
这样的东西detail = MyACL(1)("Detail1")
是不可能的。你总是必须有这样一个临时变量:
person = MyACL(1)
detail = person("Detail1")