【问题标题】:Set Class property using Sub使用 Sub 设置 Class 属性
【发布时间】:2020-08-12 05:18:21
【问题描述】:

我正在尝试创建一个Sub,它将接受一个值和一个Class Property,并为我设置Property

在我的Class (Class1) 我有以下内容:

Option Explicit

Private vValue As String, vTest As String
Public Property Let Value(v As String)
    vValue = v
End Property
Public Property Get Value() As String
    Value = vValue
End Property
Public Property Let Test(v As String)
    vTest = v
End Property
Public Property Get Test() As String
    Test = vTest
End Property

然后我在Module1 中使用以下Sub 调用它

Sub TestSetProperty()
    Dim cl As New Class1

    SetProperty "Test", cl.Value

    Debug.Print "Value:", cl.Value
    Debug.Print "Test:", cl.Test

End Sub

我的SetProperty Sub 包含

Sub SetProperty(v As String, ByRef prop)
    prop = v
End Sub

现在我希望这会将属性传递给 SetProperty Sub 并设置值:

Value:        Test
Test: 

在即时窗口中,但是它会返回

Value:        
Test:         

我怎样才能按预期进行这项工作?

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    您发布的代码试图通过引用传递方法。这在 VBA 中是不可能的。您可以获得的最接近的是 CallByName 函数

    https://docs.microsoft.com/en-us/office/vba/Language/Reference/user-interface-help/callbyname-function

    【讨论】:

    • 这里有一个链接显示一些sample code
    • to pass a method by reference - 属性,即。他们在 VB.NET 中完成了implement。这个答案对 VBA 来说是正确的。
    • 这很令人失望,但感谢您的替代方案 - 将接受它作为回答
    • 是否有您无法使用 cl.Test=cl.Value 的原因
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多