尝试从带有参数的模块类调用子方法时出现错误 438 [英] Error 438 when trying to call a sub method from a module class with parameters
问题描述
我明白了
错误 438:对象不支持此属性或方法
error 438 : Object does not support this property or method
尝试运行以下代码时.你能帮我解决这个问题吗?
when trying to run the following code. Could you help me with that please?
我的宏代码:
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
我已经尝试了我的方法的不同调用:
I have already tried different calls of my method:
upList.AppendRoot(g)
Call upList.AppendRoot(g)
和上面引用的那个.没有工作.
and the one that is quoted above. None works.
推荐答案
注意调用子/函数不返回值必须不带括号调用:
Note that calling a sub/function without returning a value must be called without parenthesis:
content.Append grad
如果函数返回某个值,则必须添加括号
If the function returns some value then parenthesis must be added
ReturnValue = content.Append(grad)
但是,如果您将括号添加到不返回像您那样返回任何值的子/函数
But if you add parenthesis to a sub/function that does not return any value like you did
content.Append (grad)
... 强制变量 grad
被提交 ByVal
而 ByRef
是提交它们的标准方法.因此,通过在此处添加括号,您可以将 ByRef
更改为 ByVal
(您还可以看到函数/子名称和括号之间有一个空格).
… that forces the variable grad
to be submitted ByVal
while ByRef
is the standard method to submit them. So by adding parenthesis here you change from ByRef
to ByVal
(you can also see that there is a space between the function/sub name and the parenthesis).
示例:
Sub TestFunction(Param1, Param2, Param3) 'all 3 parameters are ByRef by default in VBA (not in VB.NET!)
End Sub
调用这个函数的一些例子:
Some examples to call this function:
'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 个参数,但如果在不应该出现的位置添加括号,它会抛出错误,但只有一个参数会更困难:
While with 3 parameters it will throw an error if you add parenthesis where they should not be, it will be more difficult with only one parameter:
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
这篇关于尝试从带有参数的模块类调用子方法时出现错误 438的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!