类获取函数错误ByRef

时间:2017-11-02 17:46:02

标签: vba excel-vba class get excel

我有一个名为cCavity的自定义类。该类的许多属性之一是名为pAdjacency的字符串数组,其中包含每个索引处的节点的字符串名称(格式为C [节点数])。我试图通过使用两个节点名称相互结合来创建边缘名称。每当我尝试为类对象调用邻接GET函数时,我得到一个ByRef参数类型不匹配,我看不出原因。

类获取功能:

Public Property Get Adjacency(Index As Integer) As String
    Adjacency = pAdjacency(Index)
End Property

收到错误的代码部分:

Sub CalculateEdges(cCavities() As cCavity, dEdges As Scripting.Dictionary)
    'Dim i as integer
    For i = 1 To UBound(cCavities)
        If cCavities(i).AdjacencySize > MaxEdges Then MaxEdges = cCavities(i).AdjacencySize
        'Dim j as Integer
        For j = 1 To cCavities(i).AdjacencySize
            dEdges.Add cCavities(i).Name & cCavities(i).Adjacency(j), 0 ' The error is taking place here, with the .Adjacency(j)
        Next j
    Next i
End Sub

有问题的错误是:“编译错误:ByRef参数类型不匹配”

我错过了一些明显的东西吗?参数j应该是一个整数,并且我试图在弄清楚发生了什么时尝试明确地定义它。

2 个答案:

答案 0 :(得分:3)

首先指定Downloading template "ghcjs" to create project "ghcjsSetup" in ghcjsSetup/ ... The following parameters were needed by the template but not provided: author-email, author-name, category, copyright, github-username You can provide them in /home/u/.stack/config.yaml, like this: templates: params: author-email: value author-name: value category: value copyright: value github-username: value Or you can pass each one as parameters like this: stack new ghcjsSetup ghcjs -p "author-email:value" -p "author-name:value" -p "category:value" -p "copyright:value" -p "github-username:value" Looking for .cabal or package.yaml files to use to init the project. Using cabal packages: - ghcjsSetup/ghcjsSetup.cabal Selecting the best among 11 snapshots... * Partially matches lts-9.11 ghcjs-base not found - ghcjsSetup requires -any Downloaded nightly-2017-11-01 build plan. Unable to parse cabal file: FromString "This package requires at least Cabal version 2.0" Nothing 并声明所有变量,然后使参数Option Explicit有意义:对象指针和值可以通过值传递,数组必须传递ByVal

首先从这开始:

ByRef

现在,这个4线程代码段解除引用Public Property Get Adjacency(ByVal Index As Long) As String Adjacency = pAdjacency(Index) End Property 5次。您是否考虑过引入局部变量,并取消引用对象一次

cCavities(i)

现在,For i = LBound(cCavities) To UBound(cCavities) Dim foo As cCavity Set foo = cCavities(i) For j = 1 To foo.AdjacencySize Dim edge As String edge = foo.Name & foo.Adjacency(j) Next Next 分配是否有效?如果是这样,恭喜,您现在在foo成员呼叫上拥有 IntelliSense ,以及更高效的代码。

答案 1 :(得分:1)

这是编译错误,而不是运行时错误。如果j是Variant(如果它没有显式声明的那样),编译器将不会让它通过。用适当的类型声明所有变量。

编辑:由于某种原因,编译器只在运行时抱怨这个,但它仍然是编译错误。