object

  • Thread starter Thread starter TonyJ
  • Start date Start date
T

TonyJ

Hello!

I have read this small example on some page on the internet but I can't
understand
how myValue can be a member of the x object.becaude object is a reserved key
object.
The same question is also valid how the y object can contain the member
myValue?

object x = new object();
x.myValue = 10;
object y = x;
y.myValue = 20; // after this statement both x.myValue and y.myValue equal
20


//Tony
 
I have read this small example on some page on the internet but I can't
understand how myValue can be a member of the x object.becaude
object is a reserved key
object.
The same question is also valid how the y object can contain the member
myValue?

It can't - the code you gave isn't valid. Where did you get the sample
from?

Jon
 
Jon said:
Right. It's probably worth dropping the author a mail to explain that
his code is broken then :)

The actual article says:

"Consider the following piece of code, in which two variables are given
a reference to the same object (for the sake of the example, this object
is taken to contain the numeric property 'myValue').

object x = new object();
x.myValue = 10;
object y = x;
y.myValue = 20; // after this statement both x.myValue and
y.myValue equal 20"

I guess, either the "for the sake of the example" (ie. this isn't valid
in real life) bit was missed, or it has just been added following
feedback from someone.

D
 
The actual article says:

"Consider the following piece of code, in which two variables are given
a reference to the same object (for the sake of the example, this object
is taken to contain the numeric property 'myValue').

object x = new object();
x.myValue = 10;
object y = x;
y.myValue = 20; // after this statement both x.myValue and
y.myValue equal 20"

I guess, either the "for the sake of the example" (ie. this isn't valid
in real life) bit was missed, or it has just been added following
feedback from someone.

Okay, so it's hypothetical code - but it would still be clearer to use
a hypothetical class in that case, rather than one which *does* exist
but *doesn't* contain the property:

MyClass x = new MyClass();
x.MyValue = 10;
MyClass y = x;
y.MyValue = 20;

(That would also use appropriate casing for MyValue :)

Jon
 
Eitherway, you have gotten away from the OP who is still trying to
understand the concept. Let's not forget, some folks are still new to this.
You never explained why the code is broken or what the intent of the code
was.

Tony J,

The reason the code raised an alarm is because the base type "Object" was
used, which is inherintly nondiscript and has value properties to assign.
But since this snippet was for juxtapose illustration, we can see passed it
to give you this explaination:

What the code was attempting to illustrate is (as Jon explained on his
website http://pobox.com/~skeet/csharp/parameters.html) that two reference
types may point to the same piece of data, and as such, when you alter the
data from one variable, your other variable will reflect the same change.
In this way, the variable is not the data, rather it is a pointer to the
data. Since y was assigned to x, y was in affect assigned the same pointer
to the data that x pointed to, the developer now just has two ways of
accessing the data.

The difference is of course value types:

int i = 0;
int j = i;
i = 5;

console.writeline(string.format(i == j));

The output will be false, becase these variables are value types which is
the data and not a pointer and therefore cannot point to the same piece of
information.

Jon's last comment with the sample code would have made a better
illustration, as we all should know that "object" is like saying vehicle
when referring to a bike, a car, a plane, or any other mode of
transportation. In .Net all things (anything that requires a New statement
to create or instantiate) derive from "object".
 
Back
Top