Class 属性 作为 ByRef 参数不起作用

Class property as ByRef argument not working

我正在使用一个函数来修改一系列字符串,将它们作为参数传递给修改函数。调用者的字符串变量都按预期进行了修改,但是 class 属性 的一个参数没有改变,这是否可能? class 的要点是:-

Private cRptRef As String

Public Property Get TestRefID() As String
    TestRefID = cRptRef
End Property

Public Property Let TestRefID(Test_Ref As String)
    cRptRef = Test_Ref
End Property

修改字符串的函数有如下声明

Public Function GetTestFileNames(ByRef hdrFile As String, _
                                 ByRef calFile As String, _
                                 ByRef dataFile As String, _
                                 ByRef testRef As String _
                                 ) As Boolean

GetTestFilenames的调用如下:

If GetTestFileNames(HEADERpath, CALpath, RAWDATApath, _
                    ref) = False Then

所有字符串参数都声明为全局字符串,并且在调用前为空 ("")。通话结束后,他们通常会有类似“d:{文件路径{filename.csv}的内容。 所以函数中的所有这些语句都可以填充目标字符串。

hdrFile = Replace(userFile, "##", PT_Rpt.Info.FindNode(TEST_REF_HDRsuffix).data, , , vbTextCompare)
dataFile = Replace(userFile, "##", PT_Rpt.Info.FindNode(TEST_REF_DATAsuffix).data, , , vbTextCompare)
calFile = Replace(userFile, "##", PT_Rpt.Info.FindNode(TEST_REF_CALsuffix).data, , , vbTextCompare)

但是这个语句没有给它的目标字符串分配任何东西

testRef = Mid(userFile, InStrRev(userFile, Application.PathSeparator) + 1)
testRef = Left(testRef, InStrRev(testRef, "_") - 1)
Debug.Print "Class.TestRefID="; testRef

Debug.Print 语句打印预期的字符串,但外部引用不受影响。这与它是 属性 有什么关系吗? 目标字符串是 Class.TestRefID 代替 testRef 参数。 如果我用标准字符串变量替换参数列表中的 class 属性,然后将其分配给 class 属性 然后我得到预期的结果,这似乎不必要的工作。 是不是我遗漏了什么或者这在 VBA 中是不可能的?

成员访问表达式是一个表达式,必须先由VBA求值,然后它的结果才能传递.

如果我有这样的 Class1 模块:

Option Explicit
Public Foo As String

然后是快速调用程序:

Sub test()
    With New Class1
        bla .Foo
        Debug.Print .Foo
    End With
End Sub

Sub bla(ByRef bar As String)
    bar = "huh"
End Sub

test 过程将输出一个空字符串。

这是因为当您将成员传递给 VBA 中过程的 ByRef 参数时,您并没有将引用 传递给成员 - 您正在传递对 该成员持有的值的引用

因此 成员访问表达式 被求值,求值为 "",因此 "" 被传递给 ByRef 过程,它赋值它到 "huh",但调用者没有持有对 "" 值的引用,因此它永远看不到分配的 "huh" 字符串。

If I replace the class property in the argument list with a standard string variable, and then assign that to the class property then it I get the expected result, which seems unnecessary work

这不是不必要的工作,它是强制执行的,否则没有任何内容持有对成员表达式结果的引用。

那么真正的问题又是一个设计问题,:函数不想byref-return 4个值,它想取对此对象的引用,并为其分配属性。