Lotus字符串数组非法边界求助:如何用变量定义数组?
解决LotusScript中字符串数组非法边界问题
LotusScript里固定大小数组的边界必须是常量,不能用变量定义,这就是你直接写Dim tablelabels(count) As String报错的核心原因。要实现动态大小的数组,得用动态数组结合ReDim语句处理,下面给两种可行方案:
方案1:先统计数量再初始化数组(推荐,性能更优)
先遍历一次文档统计总数,再用这个数值定义数组大小,避免频繁扩容操作:
Dim tablelabels() As String Dim count As Integer Dim doc As NotesDocument Dim n As Integer ' 第一步:统计目标文档总数 count = 0 Set doc = view.GetFirstDocument() While Not (doc Is Nothing) count = count + 1 Set doc = view.GetNextDocument(doc) Wend ' 初始化数组(数组下标从0开始,所以大小设为count-1) ReDim tablelabels(count - 1) As String ' 第二步:遍历填充数组内容 n = 0 Set doc = view.GetFirstDocument() While Not (doc Is Nothing) tablelabels(n) = doc.GetItemValue("Reply")(0) n = n + 1 Set doc = view.GetNextDocument(doc) Wend
方案2:动态扩容数组(无需提前统计数量)
如果不想遍历两次文档,可以在每次新增元素时用ReDim Preserve动态扩容数组,它会自动保留已有的数组数据:
Dim tablelabels() As String Dim doc As NotesDocument Dim n As Integer n = 0 Set doc = view.GetFirstDocument() While Not (doc Is Nothing) ' 扩容数组到当前下标n的位置 ReDim Preserve tablelabels(n) As String tablelabels(n) = doc.GetItemValue("Reply")(0) n = n + 1 Set doc = view.GetNextDocument(doc) Wend
注意:ReDim Preserve每次都会重新分配内存并复制原有数据,当文档数量较多时,性能会比方案1差一些,可根据你的实际场景选择。
内容的提问来源于stack exchange,提问作者rexwang
相关产品推荐
相关产品推荐

