Edit structure members when a property

  • Thread starter Thread starter Steve
  • Start date Start date
S

Steve

Hi, I was wondering why I couldn't assign values to a rectangles member when
the rectangle is a property of a class ie.

// This works ok as expected.
Rectangle r = new Rectangle(10, 10, 10, 10);
r.Y = 20;

// when a rectangle is a property of my own class, I can't assign directly
to the Y value.
MyClass c = new MyClass();
c.MyRectangle = new Rectangle(10, 10, 10, 10);
c.MyRectangle.Y = 20; // Error: Cannot modify the return value because it is
not a variable!!!!!!!!!

c.MyRectangle = new Rectangle(10, 20, 10, 10); // ok


Thanks in advance.
Steve.
 
Steve said:
Hi, I was wondering why I couldn't assign values to a rectangles member when
the rectangle is a property of a class ie.

// This works ok as expected.
Rectangle r = new Rectangle(10, 10, 10, 10);
r.Y = 20;

// when a rectangle is a property of my own class, I can't assign directly
to the Y value.
MyClass c = new MyClass();
c.MyRectangle = new Rectangle(10, 10, 10, 10);
c.MyRectangle.Y = 20; // Error: Cannot modify the return value because it is
not a variable!!!!!!!!!

c.MyRectangle = new Rectangle(10, 20, 10, 10); // ok

The c.MyRectangle part would return the current rectangle, which is a
*value* type value. Changing the value of Y would make no difference
whatsoever to the value underlying the c.MyRectangle property.

I don't have time to explain further, but basically you need to look
into the differences between reference types and value types.
 

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

Back
Top