Passing objects ByVal vs ByRef

  • Thread starter Witold Iwaniec via .NET 247
  • Start date

W

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
 
Ad

Advertisements

Ad

Advertisements

M

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top