为什么此ArrayList在Visual Basic中没有声明就可以工作? [英] Why does this ArrayList work with no declaration in Visual Basic?

查看:70
本文介绍了为什么此ArrayList在Visual Basic中没有声明就可以工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理Word 2010中的一些示例代码,但是在添加10个变量后它就中断了.我现在正在尝试了解VB中的ArrayList.

I'm working off some example code in Word 2010, but it breaks after 10 added variables. I'm trying to understand ArrayLists in VB now.

 If (ActiveDocument.Name = "template.docm") Then
 With ActiveDocument

    On Error Resume Next
    .Variables.Add Name:="1", Value:="1"
    .Variables.Add Name:="2", Value:="2"

我认为代码开始将对象添加到ArrayList中,但是我在VBA Arraylists上阅读的所有内容都需要声明,例如:

I think the code starts adding objects to an ArrayList, but everything I've read on VBA Arraylists require declaration like:

 Dim Variables As Object
 Set Variables = CreateObject("System.Collections.ArrayList") 

如果我遵循该模式并使用.Variables.add创建更多对象,则索引将在10之后中断.现在,我只是在尝试理解列表.

If I follow the pattern and create more objects using .Variables.add then the index breaks after 10. Right now I'm just trying to understand the list.

推荐答案

来自VincentG.-变量是对象Document的集合属性,而不是ArrayList,并且在添加10个变量后似乎不会中断.至少在我的系统上."

From Vincent G. - "Variables is a collection property of the object Document, not an ArrayList, and does not seems to break after 10 variables were added, at least on my system."

这帮助我弄清楚问题不是在声明数组/集合.

This helped me figure out the problem was not declaring the array/collection.

我的实际问题是在以后的循环中进行迭代.我以前有:

My actual problem was iterating through a later loop. I previously had:

     Dim z As String
     int i = 1
     For Each f In myarray
        .Variables(i).Value = f
        i = i + 1
     Next f

并且为索引变量传递的参数实际上是一个字符串

and the parameter passed for indexing Variables is actually a string

.Variables(name) 

所以:

     int i = 1
     For Each f In myarray
     z = CStr(i)
        .Variables(z).Value = f
        i = i + 1
     Next f

解决了我的问题,感谢所有花时间帮助的人!

fixed my problem, thanks to everyone who took time to help!

这篇关于为什么此ArrayList在Visual Basic中没有声明就可以工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆