首页 文章

Catia VBA到.CATScript类型“Collection”

提问于
浏览
1

在我的VBA代码中,我使用以下内容:

Dim docsToSave As Scripting.Dictionary
Set docsToSave = New Scripting.Dictionary

Dim toRemove As Collection
Set toRemove = New Collection

...
More Code
...

For i = 1 To toRemove.Count
    docsToSave.Remove (toRemove.Item(i))
Next

与Dictionaries相比,Collections的优点在于您可以使用其项目编号来检索密钥 .
我的问题是我需要将我的VBA代码转换为CATScript(类似于VBScript) .
我很容易得到一个像上面那样简单的代码,用于从另一个列表中减去一个"key, item"列表 .
修复我的代码以便它在CAT / VB脚本中工作的最佳方法是什么?
我相信它可能有一个数组,因为在我尝试使用数组之前,我首先要求更好的方法 .

编辑:
此问题与CATIA VBA Enable use of “Scripting.Dictionary” in .CATScript不同,因为此问题专门请求有关如何在VBScript中使用字典的信息,以便它可以复制现有VBA脚本中使用的Collection的行为 .

2 回答

  • 2

    您必须使用数组,并在向其中添加和删除项目时自行管理调整大小,或者您可以使用字典并将键作为整数进行管理 . 我通常做后者 .

    'create dictionary
    set dict = CreateObject("Scripting.Dictionary")
    
    'add object
    dict.add dict.count,objectOrValue
    
    'loop
    for i = 0 to dict.count -1
       objectOrValue = dict.item(i)
       ...
    next
    

    如果你想保持vba集合的一个基于行为的行为使用“dict.count 1”作为密钥,那么这应该与基于零的集合非常相似

  • 1

    对于发布的代码在VBScript中的行为方式与在VBA中的行为方式相同,可以使用以下代码:

    Dim docsToSave
    Set docsToSave = CreateObject("Scripting.Dictionary")
    
    Dim toRemove
    Set toRemove = CreateObject("Scripting.Dictionary")
    
    ...
    More Code
    ...
    
    For i = 0 To toRemove.Count - 1
        docsToSave.Remove (toRemove.keys()(i))
    Next
    

    此外,要添加到Dictionary中,与Collection相比使用了不同的语法:

    'VBA-code for a collection:
    toRemove.Add (x)
    
    'VBScript for a dictionary:
    Call toRemove.Add(x, i)
    

    这些更改足以让我的VBA脚本在VBScript中工作 .

相关问题