467,926 Members | 1,505 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,926 developers. It's quick & easy.

Passing objects ByVal vs ByRef

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
  • viewed: 9478
Share:
2 Replies
"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
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.

Similar topics

7 posts views Thread by Bruce A. Wilkinson | last post: by
3 posts views Thread by Andy Read | last post: by
3 posts views Thread by Scott M. | last post: by
4 posts views Thread by Erik Foreman | last post: by
11 posts views Thread by John Pass | last post: by
8 posts views Thread by Peter | last post: by
4 posts views Thread by John Sheppard | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.