尝试从带有参数的模块类调用 sub 方法时出现错误 438



I get

错误 438:对象不支持此属性或方法

尝试运行以下代码时。你能帮我吗?

我的宏代码:

Sub test() 
   Dim upList As New ListRoot
   upList.msg 'this one works fine 
   Dim g As New Gradient
   upList.AppendRoot g 'line that raises the issue
End Sub

我的模块类代码:

Public Sub AppendRoot(grad As Gradient)
    If IsEmpty(content.content) Then
       Set content.content = grad
    Else
       content.Append (grad)
    End If
End Sub
Public Sub msg()
    MsgBox "HEY"
End Sub

我已经尝试了我的方法的不同调用:

upList.AppendRoot(g) 
Call upList.AppendRoot(g) 

和上面引用的那个。没有一个有效。

请注意,调用子/函数而不返回值必须不带括号:

content.Append grad

如果函数返回某个值,则必须添加括号

ReturnValue = content.Append(grad)

但是,如果您将括号添加到不像您那样返回任何值的子/函数中

content.Append (grad)

。这会强制提交变量grad ByValByRef是提交它们的标准方法。因此,通过在此处添加括号,您可以从ByRef更改为ByVal(您还可以看到函数/子名称和括号之间有一个空格(。


例:

Sub TestFunction(Param1, Param2, Param3) 'all 3 parameters are ByRef by default in VBA (not in VB.NET!)
End Sub

调用此函数的一些示例:

'submit 3 parameters ByRef, don't return anything
TestFunction Param1, Param2, Param3 
TestFunction(Param1, Param2, Param3) 'throws error
'submit 3 parameters ByRef, return a Value
ReturnValue = TestFunction(Param1, Param2, Param3) 
ReturnValue = TestFunction Param1, Param2, Param3   'throws error 
'submit parameters 1 and 3 ByVal and 2 ByRef, don't return anything
TestFunction (Param1), Param2, (Param3) 
'submit parameters 1 and 3 ByVal and 2 ByRef, return a Value
ReturnValue = TestFunction((Param1), Param2, (Param3)) 

虽然有 3 个参数,如果在不应该的地方添加括号,则会抛出错误,但只有一个参数会更困难:

TestFunction Param1   'correct
TestFunction (Param1) 'this threw an error with 3 parameters, but does not throw an error 
                      'with one parameter instead it changes behavior ByRef to ByVal
ReturnValue = TestFunction(Param1) 'correct
ReturnValue = TestFunction Param1  'throws error 

最新更新