469,627 Members | 943 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,627 developers. It's quick & easy.

object reference assignment doesn't update object in session

TS
I was under the impression that when you get a reference to an object, then
update that reference, the object gets updated. Apparently this isn't the
case using session?

code (note at this point this session object is null):
---------------------------------------------------------------------------
object o = Session["TheYearDataSet"];

if(o == null)
{
o = oh.FillTheYearDataSet(CISYear.ToString());
}
---------------------------------------------------------------------------

At this point, i would think that Session["TheYearDataSet"] would be the
same value as o, but it is still null even though o is now set. I thought
that o is just a reference to that object, and in changing the reference,
you change the object you are referencing????

thanks
Nov 17 '05 #1
7 2330
No, because there are 2 places in memory. There is Session["TheYearDataSet"]
which can point to an object. And there is 'o', which can also point to it.
Just because o points to one - doesn't mean your session variable will too.

It's not as if they are both aliases for the same reference variable - which
would be the only way this could work.

They are two completely different variables, that can point to different
objects. Or they can point to the same object. But if you change where one
of them points, that has no effect on the other.

"TS" <ma**********@nospam.nospam> wrote in message
news:ui**************@TK2MSFTNGP12.phx.gbl...
I was under the impression that when you get a reference to an object, then
update that reference, the object gets updated. Apparently this isn't the
case using session?

code (note at this point this session object is null):
---------------------------------------------------------------------------
object o = Session["TheYearDataSet"];

if(o == null)
{
o = oh.FillTheYearDataSet(CISYear.ToString());
}
---------------------------------------------------------------------------

At this point, i would think that Session["TheYearDataSet"] would be the
same value as o, but it is still null even though o is now set. I thought
that o is just a reference to that object, and in changing the reference,
you change the object you are referencing????

thanks

Nov 17 '05 #2
"TS" <ma**********@nospam.nospam> wrote in
news:ui**************@TK2MSFTNGP12.phx.gbl:
I was under the impression that when you get a reference to an
object, then update that reference, the object gets updated.
Apparently this isn't the case using session?

code (note at this point this session object is null):
-----------------------------------------------------------------
---------- object o = Session["TheYearDataSet"];

if(o == null)
{
o = oh.FillTheYearDataSet(CISYear.ToString());
}
-----------------------------------------------------------------
----------

At this point, i would think that Session["TheYearDataSet"]
would be the same value as o, but it is still null even though
o is now set. I thought that o is just a reference to that
object, and in changing the reference, you change the object you
are referencing????


Session["TheYearDataSet"] is not an object. It is a reference to an
object, just like o.

The statement:

object o = Session["TheYearDataSet"];

makes o refer to the same object that Session["TheYearDataSet"] is
referring to.

Changing o to refer to a different object will have no effect on what
object Session["TheYearDataSet"] refers to.
--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 17 '05 #3
TS
this is true though, correct?

Textbox b;
Textbox a = b;
Textbox c = b;

a.Text = "good";

now, b.Text and c.Text both are "good"?

"Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in message
news:Xn*********************************@207.46.24 8.16...
"TS" <ma**********@nospam.nospam> wrote in
news:ui**************@TK2MSFTNGP12.phx.gbl:
I was under the impression that when you get a reference to an
object, then update that reference, the object gets updated.
Apparently this isn't the case using session?

code (note at this point this session object is null):
-----------------------------------------------------------------
---------- object o = Session["TheYearDataSet"];

if(o == null)
{
o = oh.FillTheYearDataSet(CISYear.ToString());
}
-----------------------------------------------------------------
----------

At this point, i would think that Session["TheYearDataSet"]
would be the same value as o, but it is still null even though
o is now set. I thought that o is just a reference to that
object, and in changing the reference, you change the object you
are referencing????


Session["TheYearDataSet"] is not an object. It is a reference to an
object, just like o.

The statement:

object o = Session["TheYearDataSet"];

makes o refer to the same object that Session["TheYearDataSet"] is
referring to.

Changing o to refer to a different object will have no effect on what
object Session["TheYearDataSet"] refers to.
--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/

Nov 17 '05 #4
"TS" <ma**********@nospam.nospam> wrote in
news:##**************@TK2MSFTNGP09.phx.gbl:
this is true though, correct?

Textbox b;
Textbox a = b;
Textbox c = b;

a.Text = "good";

now, b.Text and c.Text both are "good"?


Yes. But just to nitpick, the first line should be:

TextBox b = new TextBox();

One way of looking at this is there are four entities in your
code: a TextBox instance (created by the "new" statement),
and three references that point to that instance (a, b and c).

Since all of the references point to the same instance, the
following lines of code will all modify the same instance:

a.Text = "good";
b.Text = "good";
c.Text = "good";

Your original question was different. In it, you were changing
an object reference, not a property of the underlying instance.

Unfortunately, I can't find a good tutorial on object references
for C#. However, these parts of the documentation might help:

C# Language Specification - 1.2 Types:
http://msdn.microsoft.com/library/en...rpspec_1_2.asp

System.Object.ReferenceEquals
http://msdn.microsoft.com/library/de...qualstopic.asp

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 17 '05 #5
Yes, TS, the o = oh.FillTheYearDataSet(CISYear.ToString()); is changing o
to point to a newly created object returned by FillTheYearDataSet method.
But this has no effect on the Session["TheYearDataSet"];.

The text in your last example changed, because all the references (a,b,c)
point to the same instance of textbox. And the code a.Text = "good"; is NOT
changing a, b or c's reference. It is changing a member variable of the
TextBox.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #6
TS
thanks all, i have it now
"Kevin Yu [MSFT]" <v-****@online.microsoft.com> wrote in message
news:xW*************@TK2MSFTNGXA01.phx.gbl...
Yes, TS, the o = oh.FillTheYearDataSet(CISYear.ToString()); is changing o
to point to a newly created object returned by FillTheYearDataSet method.
But this has no effect on the Session["TheYearDataSet"];.

The text in your last example changed, because all the references (a,b,c)
point to the same instance of textbox. And the code a.Text = "good"; is
NOT
changing a, b or c's reference. It is changing a member variable of the
TextBox.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #7
You're welcome.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

106 posts views Thread by A | last post: by
28 posts views Thread by Daniel | last post: by
3 posts views Thread by Virendra Verma | last post: by
13 posts views Thread by | last post: by
2 posts views Thread by Jon Slaughter | last post: by
275 posts views Thread by Astley Le Jasper | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.