Which classes are automatically passed by ref and which are not?

In VB.NET, parameters are passed ByVal by default (i.e., when you specify no ByVal/ByRef modifier). This behavior was changed from VB where parms are ByRef unless you explicitly declare them as ByVal. All classes (both value types and ref types) passed as parameters are subject to this rule.
 
All parameters are passed by val?

I was under the impression that complex objects are always passed by ref. Otherwise the runtime environment would need to perform a deep copy clone of each object.
 
> I was under the impression that complex objects are always passed by ref.

Nope. I suppose by "complex objects" you meant reference types, right? Remember that ref types are just references. When you pass a ref type, you are passing a pointer ref and not the object itself.

When you pass a ref type byval, it means the ref will point to the same object. Try following the sample code below; the comments should guide you:

Visual Basic:
Public Class Tester
    Public Shared Sub Main()
        Dim t As New Tester(), referenceType As New MyRefType()
        referenceType.RefTypeProp = 1
        t.PassByVal(referenceType)
        '-referenceType.RefTypeProp will revert to 1 since it was passed ByVal.
        MsgBox("After PassByVal: MyRefType.RefTypeProp = " & referenceType.RefTypeProp)
        t.PassByRef(referenceType)
        '-referenceType.RefTypeProp will become 2000 since it was passed ByRef.
        MsgBox("After PassByRef: MyRefType.RefTypeProp = " & referenceType.RefTypeProp)
    End Sub

    Public Sub PassByVal(ByVal refType As MyRefType)
        Dim newRefType As New MyRefType()
        newRefType.RefTypeProp = 1000
        refType = newRefType
        '-This assignment will NOT persist after method call because of ByVal.
        MsgBox("Inside PassByVal: MyRefType.RefTypeProp = " & refType.RefTypeProp)
    End Sub
    Public Sub PassByRef(ByRef reftype As MyRefType)
        Dim newRefType As New MyRefType()
        newRefType.RefTypeProp = 2000
        '-This assignment will persist after method call because of ByRef.
        reftype = newRefType
        MsgBox("Inside PassByRef: MyRefType.RefTypeProp = " & reftype.RefTypeProp)
    End Sub
End Class

Public Class MyRefType
    Private m_intRefTypeProp As Integer
    Public Property RefTypeProp() As Integer
        Get
            Return m_intRefTypeProp
        End Get
        Set(ByVal Value As Integer)
            m_intRefTypeProp = Value
        End Set
    End Property
End Class
 
Everything except the primitive data types and structures is passed by reference whether you like it or not. Primitive data types and structures are passed by value unless you specify otherwise (ByRef in vb, ref in C#).
 
When you pass ref types, it's true that a reference is passed, not the object itself. I think it's not the same w/ the notion of the "pass-by-reference" term as commonly used in the terminology of programming languages.

When a ref type is passed byval, the ref type will point to the same object it pointed to prior to the function call, even if w/in the function, the ref is set to another object.

When a ref type is passed byref and the it's set to another object w/in the function, the ref type will no longer point to the original object after the function call.
 
I am assuming that a struct is always passed by ref, correct? (Without getting into any technicalities speaking)
 
Sorry divil. I didn't read it with my brain the first time...It didn't register in sector "common sense". Thanks.
 
In my humble opinion, reference types can be passed to methods either byval or byref (just like primitive or value types). My code snippet above demonstrates this where I have virtually the same sub except that PassByVal accepts a ref type byval while PassByRef accepts a ref type byref. As can be seen, the two subs exhibit different results even though they are passed the one and the same ref type variable.
 
No offense ment JABE, but you're flat out wrong. Paremeters are not passed by value by default.

To repeat divil, all objects are passed by reference, everything else is by value (enums, structs, basic types). Remember that arrays and strings are objects. Structs and enums are value types (no, a struct is not an object in .NET.. do not treat it as such!)

Sometimes there's confusion as to how passing by reference works. Instead of trying to explain it myself, I'll just toss out a few links on the MSDN library which will hopefully shed some light on things.

General method ref/out index:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfRef.asp

Passing by ref:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfRef.asp

Passing by out:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfout.asp

Passing arrays by ref and out:
http://msdn.microsoft.com/library/d...sref/html/vclrfpassingarraysusingrefoutpg.asp
 
No offense taken, wyrd :) I've read the links you posted. In vclrf.Refasp, it states "To use a ref parameter, the argument must explicitly be passed to the method as a ref argument." If you don't explicitly use ref, then the parm will be passed byval, hence byval is the default.

Have you tried running my code above? Pleeeeease do. If you'd like a C# version, I could rewrite it (as I feel that most of you are in C#). It will actually settle once and for all that reference types can be passed byval OR byref to methods, just like primitive types.
 
*raises hands in the air and sighs*

Okay, no point taking this any further. I'll just take it to my grave :)

I have high regard for divil as most of you do but between anyone's words and what really happens in code, I'll go with the latter.
 
Back
Top