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
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