By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,273 Members | 1,892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,273 IT Pros & Developers. It's quick & easy.

Passing objects ByVal vs ByRef

P: n/a
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>
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Witold Iwaniec via .NET 247" <an*******@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/>

Nov 21 '05 #2

P: n/a
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.

"Witold Iwaniec via .NET 247" <an*******@dotnet247.com> wrote in message
news:e%****************@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>

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.