Parentheses in SUB call

Zauberkind
2StarLounger
Posts: 141
Joined: 21 Oct 2011, 10:08

Parentheses in SUB call

Post by Zauberkind »

Greetings,
this is not a problem, but a request for information.
I spent some time chasing down a problem, and it raised a 'Huh?' question.
I had a function which took an object as an argument.
I called Foo with an object, and everything was fine; Foo returned a string:

Code: Select all

Function Foo(objFiddle as Object) as String
x=Foo(objMyObject)
Then I decided I didn't need x, so I turned Foo into a Sub:

Code: Select all

Sub Foo(objFiddle as Object)
Foo(objmyObject)
That didn't work; I got an "Object required" error, so I change the parameter type to Variant.

Code: Select all

Sub Foo(vFiddle as Variant)
Foo(objmyObject)
With the parens around the argument, the received vFiddle was a Variant/String with the value "[object]".
Remove the parens, and it works as expected.
Can anyone give any insight into what just happened here?

TIA
Zk.

User avatar
HansV
Administrator
Posts: 78481
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Parentheses in SUB call

Post by HansV »

You may have noticed that the line

Code: Select all

Foo(objmyObject)
was changed automatically to

Code: Select all

Foo (objmyObject)
In such usage, the () stand for 'evaluate': (myObject) when used stand-alone tries to evaluate myObject to a value.

Let's take the following examples using Excel:

Code: Select all

Sub Test1()
    Dim myObject As Object
    Set myObject = Range("A1")
    Foo (myObject)
End Sub
In this example, (myObject) evaluates Range("A1") to its value, i.e. a string or number. But you can't call foo with a string or number as argument, so you get an "Object required" error.

Code: Select all

Sub Test2()
    Dim myObject As Object
    Set myObject = ActiveSheet
    Foo (myObject)
End Sub
Here, (myObject) tries to evaluate ActiveSheet, but a sheet doesn't have a value, so you get a different error: "Object doesn't support this property or method".

Removing the parentheses lets the code pass myObject directly, instead of evaluating it first.

An alternative is to use the Call instruction:

Code: Select all

Sub Test3()
    Dim myObject As Object
    Set myObject = Range("A1")
    Call Foo(myObject)
End Sub
Note that there is no space before (myObject). Call passes Range("A1") as a Range object to the Foo procedure.
Best wishes,
Hans

Zauberkind
2StarLounger
Posts: 141
Joined: 21 Oct 2011, 10:08

Re: Parentheses in SUB call

Post by Zauberkind »

AHA!
No, I didn't notice the space before the parens.
I also didn't know about using parentheses to mean 'Evaluate', except in formulas.
Does that mean that "[object]" is the default value of the HTMLDocument object or is it supplied from somewhere else?
Thanks for the insight.
Zk.

User avatar
HansV
Administrator
Posts: 78481
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Parentheses in SUB call

Post by HansV »

I don't know whether HTMLDocument has a value, sorry.
Best wishes,
Hans