object reference assignment doesn't update object in session

T

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
 
M

Marina

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.
 
C

Chris R. Timmons

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.
 
T

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 said:
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.
 
C

Chris R. Timmons

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-us/csspec/html/vclrfcsharpspec_1_2.asp

System.Object.ReferenceEquals
http://msdn.microsoft.com/library/d...rlrfsystemobjectclassreferenceequalstopic.asp
 
K

Kevin Yu [MSFT]

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."
 
K

Kevin Yu [MSFT]

You're welcome.

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

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

Top