Parenthesising arguments: What is MyByRefSub((arg)) doing?

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,081
Programming Experience
10+
Suppose we have the following:

VB.NET:
    Public Sub MyByRefSub(ByRef sb As StringBuilder)
        sb = New StringBuilder("All New SB By Ref")
    End Sub

And we call it with:

VB.NET:
  Dim abc as New StringBuilder("Old StringBuilder")
  MyByRefSub(abc)

When the method completes, we now have abc as the "All New SB By Ref" string builder.
I know why this is

But my question is, what, behind the scenes exactly is this doing:

VB.NET:
  MyByRefSub((abc))

It behaves exactly like ByVal but I cant think of a way of investigating whether it is just ignoring ByRef and passing ByVal, or whether it's doing some funky cloning.

Interestingly, I had also the same Sub, but with an Object arg:

VB.NET:
    Public Sub MyByRefSub(ByRef o As Object)
        o = New Object()
    End Sub

When I pass in a child type:

VB.NET:
  MyByRefSub((abc))
  [U]MyByRefSub(abc)[/U]

only the second one gets a wiggly line and a: "Option Strict On disallows narrowing from type 'Object' to type 'System.Text.StringBuilder' in copying the value of 'ByRef' parameter 'o' back to the matching argument. " error

Is this a clue to what is going on?
 
Open your local MSDN library, select the Index tab, set the filter to Visual Basic, type arguments into the search box, go down the index list to the arguments [Visual Basic] -> in parentheses topic.
 
I dont have a local MSDN and I havent succeeded in finding any info on the net that might be what you are referring to.. Would you mind pasting your local MSDN entry to the forum?
 
How to: Force an Argument to Be Passed by Value

I find the local MSDN of great value, all VS editions have it, the Express a limited version. Wish MSDN online was fast as that, searching out the class documentation and related articles requires a great deal of cross-lookups :)
 
Ah, so there is no magic or voodoo here.. its just the ByRef is conceptually rewritten to ByVal. Thanks guys!
 
Back
Top