473,399 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Objects referencing smae location

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
2 1420
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Robert Mark Bram | last post by:
Hi All! Is there a way to reference a window by name without doing something like this: open (, 'windowName'); The open method will open a blank window if there is no window with such a name....
25
by: Stuart Hilditch | last post by:
Hi all, I am hoping that someone with some experience developing nTier apps can give me some advice here. I am writing an nTier web app that began with a Data Access Layer (DAL), Business...
2
by: SpamProof | last post by:
I'm creating a new application that will use/need to refer to some business objects I have in another separate application. What the best way to do this? It appears that adding a reference and...
8
by: vvenk | last post by:
Hello: I just wrote my first ASP.Net application. It worked fine on my machine and when I put into production, the ASP.Net process reaches 50% quite fast and then the system does not work...
2
by: Terry | last post by:
What is the syntax for referencing the properties of objects within Collections of objects? EXAMPLE: Dim MCoil As New Coil MCoil = CoilCollection("3456") TextBox1.Text() = MCoil.CoilDesc ...
17
by: Paul Helmuth | last post by:
All, (here's an easy one)... This is probably a stupid question - please bare with me as I am new to dotNet. How does one reference objects on a form from a module? In 6.0 you could simply...
0
by: NM | last post by:
Hello, I've got a problem inserting binary objects into the postgres database. I have binary objects (e.g. images or smth else) of any size which I want to insert into the database. Funny is it...
3
by: =?Utf-8?B?UGhpbCBKb2huc29u?= | last post by:
Hi, I have some business objects written in Visual Studio 2003. I have a web service written in VS2008 that references these .net 1.1 objects. The web service makes some calls on the 1.1...
1
by: Tom | last post by:
My unsigned DLL works in my project that references it as long as I set Copy Local = true. Now I have signed the DLL with the sn.exe generated keys but have not yet moved the DLL into the GAC. ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.