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

Objects referencing smae location

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

Aug 17 '06 #1
Share this Question
Share on Google+
2 Replies


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

"HankD" <Mr*****@gmail.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...
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

Aug 17 '06 #2

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

Marina Levit [MVP] wrote:
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.

"HankD" <Mr*****@gmail.comwrote in message
news:11*********************@h48g2000cwc.googlegro ups.com...
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
Aug 17 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.