Objects referencing smae location

H

HankD

Hi, I am having a problem with instantiating two custom objects so they
DO NOT point to the same memory location. What is happening is that
changes I am making to my object1 are changing object2. I beleive this
is because I set both to be equal to the same session variable. So when
I change the value in test1.name it updates test2.name as well as the
session variable itself. What I want to do is keep an "ORIGINAL" copy
of the data in case I want to rollback. Can anyone explain this to me?
Thanks.

CODE SAMPLE:
CUSTOM OBJECT-
Public Class test
Private _aName As String = ""
Property aName() As String
Get
Return _aName
End Get
Set(ByVal Value As String)
_aName = Value
End Set
End Property
Public Sub New()

End Sub
End Class
CODE BEHIND PAGE1-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim x As New TestSession.test
x.aName = "TestOrg"
Session("test") = x
Response.Redirect("WebForm1.aspx")
End Sub

CODE BEHIND PAGE2-
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Page.IsPostBack Then
process()
End If
End Sub
Private Sub process()
Dim test1 As New TestSession.test
test1 = CType(Session("test"), TestSession.test)
Dim test2 As New TestSession.test
test2 = CType(Session("test"), TestSession.test)
test1.aName = "TestChange"
Me.TextBox1.Text = test1.aName
Me.TextBox2.Text = test2.aName
End Sub

Text boxes both display TestChange instead of one with TestChange and
one with TestOrg
 
M

Marina Levit [MVP]

I don't see the problem here.

You are getting the same object out of session. test1 and test2 point to
the same exact object. So of course if you change a property through one
reference, the other will reflect it - after all, they are pointing to the
same object.

This is how objects work.

Did you think a copy is made for every variable you use? If you have 100
variables, you assign them the same object - there will still only ever be 1
instance of that object.

If you need separate objects, then you have to create 2. Create 2 objects
using 'New', put them both in session under different session variable
names.
 
H

HankD

I understand how it works but I guess I was looking at it like the
functionality behind the string datatype (the objects are referencing
the same point until one is modified then a new location is used). I
was just looking for perhaps an easier way then to create a second
session variable. That way I only would need to pass one session
variable to the next page.
I was thinking that by using the new on the second page it would create
a new instance and fill it with the data coming from the session
variable.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top