A simple question about ByVal.

  • Thread starter Thread starter MarcoGG
  • Start date Start date
M

MarcoGG

Guest
Maybe my misunderstanding about the real "ByVal" thing.

I have two Classes, one let's say clsMain has a single Method that gets an Object of the second Class, let's say clsTest.

I pass clsTest Object ByVal, and at the end the clsTest Object has changed.

Example is very simple.

The clsTest Class :

Public Class clsTest

Private mPropertyA As Integer
Private mPropertyB As String

Public Property PropertyA As Integer
Get
Return mPropertyA
End Get
Set(value As Integer)
mPropertyA = value
End Set
End Property

Public Property PropertyB As String
Get
Return mPropertyB
End Get
Set(value As String)
mPropertyB = value
End Set
End Property

Public Overrides Function ToString() As String
Return mPropertyA.ToString & ". " & mPropertyB
End Function

End Class


The clsMain Class :

Public Class clsMain

Public Sub DoSomeTest(ByVal pTest As clsTest)

pTest.PropertyA = 2
pTest.PropertyB = "TWO"

End Sub

End Class

The Job :



Dim T As New clsTest
T.PropertyA = 1
T.PropertyB = "ONE"

Dim M As New clsMain
M.DoSomeTest(T)

MessageBox.Show(T.ToString)

As you can see passing ByVal the Object has changed also first Object's Properties just like it was passed ByRef.

How should I pass the Object being sure it won't be changed by clsMain Method ?

Thanks.

Continue reading...
 
Back
Top