Search code examples
vb.netvbscriptasp-classicpass-by-valuecomvisible

"Invalid procedure call or argument" when calling a sub without parenthesing one parameter


I wrote a COM-Visible DLL with this piece of code (VB.NET)

' .NET Class with implementing an interface to make it visible
Public Class VisibleClass
    Public Sub callableSub(ByVal names As ACustomCollection, _
                           Optional ByVal doSomething As Boolean = True) _
        Implements IVisibleClass.visibleCallableSub
        Me.doSub(names.process, doSomething)
    End Sub
End Class

' Interface for COM-visibility
<InterfaceType(ComInterfaceType.InterfaceIsDual)> _
Public Interface IVisibleClass
    Sub visibleCallableSub(ByVal names As ACustomCollection, _
                           Optional ByVal doSomething As Boolean = True)
End Interface

Here is also the ASP web page creating the object and invoking its methods :

' stuff.asp
Dim visibleObject
Dim aCustomCollection
Set visibleObject = getNewVisibleInstance (aCollection)
Set aCustomCollection = createEmptyCollection

aCustomCollection.add someStuffs
aCustomCollection.add otherStuffs
aCustomCollection.add lastStuffs

' These calls work:
visibleObject.visibleCallableSub(aCustomCollection)
visibleObject.visibleCallableSub(aCustomCollection), False
visibleObject.visibleCallableSub(aCustomCollection), True
Call document.fetchPropertyCollection ((properties), false)

' These ones don't:
visibleObject.visibleCallableSub someparams
visibleObject.visibleCallableSub someparams, False
visibleObject.visibleCallableSub someparams, True
Call document.fetchPropertyCollection (properties, false)

Non working calls produce the following error :

Invalid procedure call or argument

I don't understand why I have to put parenthesis. I know this tells the interpreter to make a copy before passing theme, but not why it's mandatory.

Note : It's the same issue than this one, i.e. about passing a reference where a copy is required. However, the question was told like it's due to "passing the return value of another function", which make it harder to reach through research.


Solution

  • If the called Sub/Function asks for a value (ByValue), you can't pass a reference ('pointer'). Putting the 'make a copy' parentheses around the argument makes sure the called Sub/Function gets a value.

    To make your intention explicit, you should write:

    visibleObject.visibleCallableSub (aCustomCollection), False
    

    Cf. same error, parentheses, adventures.