Home All Groups Group Topic Archive Search About

Passing objects ByVal vs ByRef

Author
6 Apr 2005 6:38 PM
Witold Iwaniec via .NET 247
It seems that when you pass an object to a function it is always passed by reference even if it is explicitly declared ByVal. Is it the behavior of VB.Net?

Here is sample code from sample Asp.Net application. The sub loadValueByVal takes the argument by value so after returning to calling method, the object should be unchanged but it is not

Public Class ITest

    Private MyName As String
    Public TestId As String

    Public Sub New()
        MyName = "Test"
        TestId = "0"
    End Sub

    Public Property OneName() As String
        Get
            Return MyName
        End Get
        Set(ByVal theValue As String)
            MyName = theValue
        End Set
    End Property

End Class


Imports DevTestC
Imports System.Diagnostics

Public Class WebForm1
    Inherits System.Web.UI.Page

...

    Private Sub loadValueByVal(ByVal oneClass As ITest)
        With oneClass
            .OneName = "Passed Class ByVal"
            .TestId = "1"
        End With
    End Sub

    Private Sub loadValueByRef(ByRef oneClass As ITest)
        With oneClass
            .OneName = "Passed Class ByRef"
            .TestId = "2"
        End With
    End Sub

    Private Sub btnTestClass_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTestClass.Click

        Dim testClass As ITest = New ITest

        Debug.WriteLine("Initial")
        Debug.WriteLine("OneName: " & testClass.OneName)
        Debug.WriteLine("ID: " & testClass.TestId)

        loadValueByVal(testClass)
        ' Passed ByVal - ITest should be the same as above
        Debug.WriteLine("===============================")
        Debug.WriteLine("After passed ByVal")
        Debug.WriteLine("OneName: " & testClass.OneName)
        Debug.WriteLine("ID: " & testClass.TestId)

        loadValueByRef(testClass)
        Debug.WriteLine("===============================")
        Debug.WriteLine("After Passed ByRef")
        Debug.WriteLine("OneName: " & testClass.OneName)
        Debug.WriteLine("ID: " & testClass.TestId)

    End Sub

And the output is:

Initial
OneName: Test
ID: 0
===============================
After passed ByVal
OneName: Passed Class ByVal
ID: 1
===============================
After Passed ByRef
OneName: Passed Class ByRef
ID: 2

Passing ByVal and ByRef works properly with Strings and other data types provided by VB but not for objects

--------------------------------
From: Witold Iwaniec

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>i78N7CDdxEuzgzyu2Sdv9g==</Id>

Author
6 Apr 2005 6:42 PM
Herfried K. Wagner [MVP]
"Witold Iwaniec via .NET 247" <anonym***@dotnet247.com> schrieb:
> It seems that when you pass an object to a function it is always passed by
> reference even if it is explicitly declared ByVal. Is it the behavior of
> VB.Net?

See (complete thread):

<URL:http://www.google.de/groups?selm=KQMVd.3820%24wH4.1180%40fe09.lga>

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
6 Apr 2005 6:44 PM
Marina
ByVal when passing class references, means the ByVal is applied to the
reference, not to the object.
If you pass in a datatable ByVal with 10,000 rows, you don't expect, the
datatable to be copied, now having 20,000 rows in memory, do you?

So in most cases, ByVal and ByRef when passing in objects, will not effect
you. Unless you plan to reset the object reference to an instance of another
object or to null. Since this is not what most methods tend to do, there is
really no difference, but of course it is an important distinction.

Show quoteHide quote
"Witold Iwaniec via .NET 247" <anonym***@dotnet247.com> wrote in message
news:e%23axLftOFHA.4000@TK2MSFTNGP15.phx.gbl...
> It seems that when you pass an object to a function it is always passed by
> reference even if it is explicitly declared ByVal. Is it the behavior of
> VB.Net?
>
> Here is sample code from sample Asp.Net application. The sub
> loadValueByVal takes the argument by value so after returning to calling
> method, the object should be unchanged but it is not
>
> Public Class ITest
>
>    Private MyName As String
>    Public TestId As String
>
>    Public Sub New()
>        MyName = "Test"
>        TestId = "0"
>    End Sub
>
>    Public Property OneName() As String
>        Get
>            Return MyName
>        End Get
>        Set(ByVal theValue As String)
>            MyName = theValue
>        End Set
>    End Property
>
> End Class
>
>
> Imports DevTestC
> Imports System.Diagnostics
>
> Public Class WebForm1
>    Inherits System.Web.UI.Page
>
> ..
>
>    Private Sub loadValueByVal(ByVal oneClass As ITest)
>        With oneClass
>            .OneName = "Passed Class ByVal"
>            .TestId = "1"
>        End With
>    End Sub
>
>    Private Sub loadValueByRef(ByRef oneClass As ITest)
>        With oneClass
>            .OneName = "Passed Class ByRef"
>            .TestId = "2"
>        End With
>    End Sub
>
>    Private Sub btnTestClass_Click(ByVal sender As System.Object, ByVal e
> As System.EventArgs) Handles btnTestClass.Click
>
>        Dim testClass As ITest = New ITest
>
>        Debug.WriteLine("Initial")
>        Debug.WriteLine("OneName: " & testClass.OneName)
>        Debug.WriteLine("ID: " & testClass.TestId)
>
>        loadValueByVal(testClass)
>        ' Passed ByVal - ITest should be the same as above
>        Debug.WriteLine("===============================")
>        Debug.WriteLine("After passed ByVal")
>        Debug.WriteLine("OneName: " & testClass.OneName)
>        Debug.WriteLine("ID: " & testClass.TestId)
>
>        loadValueByRef(testClass)
>        Debug.WriteLine("===============================")
>        Debug.WriteLine("After Passed ByRef")
>        Debug.WriteLine("OneName: " & testClass.OneName)
>        Debug.WriteLine("ID: " & testClass.TestId)
>
>    End Sub
>
> And the output is:
>
> Initial
> OneName: Test
> ID: 0
> ===============================
> After passed ByVal
> OneName: Passed Class ByVal
> ID: 1
> ===============================
> After Passed ByRef
> OneName: Passed Class ByRef
> ID: 2
>
> Passing ByVal and ByRef works properly with Strings and other data types
> provided by VB but not for objects
>
> --------------------------------
> From: Witold Iwaniec
>
> -----------------------
> Posted by a user from .NET 247 (http://www.dotnet247.com/)
>
> <Id>i78N7CDdxEuzgzyu2Sdv9g==</Id>