Re: Why does this.Location.X = 0; generate a compile error?
- From: "Dave" <none@xxxxxxxxxxx>
- Date: Thu, 27 Apr 2006 00:55:58 -0400
Ah, because Point is a struct and not a class and structs are a value type.
I get it.
Thanks.
"Rene" <a@xxx> wrote in message
news:%235LZgObaGHA.4236@xxxxxxxxxxxxxxxxxxxxxxx
This is something that gets many developers confused. As you probably
already know, "Location" is not a variable but a property. This means that
the property internal (get) implementation looks something like this:
public Point Location
{
get { return _Point; }
}
This code is then translated by the compiler to a method like:
public Point get_Location()
{
return _Point;
}
And there is the problem, what you are getting out of this function is a
fresh new Point variable, not a direct reference to the _Point varaible.
Keyword here is you are getting a full *new* Point variable so what you
are basicaly trying to do is telling the compiler to put a "Point" sturct
value into a "Point.X" wich is not a variable and that's why you get the
error.
What you need to do is to first create a new Point varaible with the new
values and then use that to set the new value as:
Point myNewPoint == new Point();
myNewPoint.X = 123;
myNewPoint.Y = 456;
this.Location = myNewPoint;
"Dave" <none@xxxxxxxxxxx> wrote in message
news:OX0ML8YaGHA.4160@xxxxxxxxxxxxxxxxxxxxxxx
.X (.Y too) are ints with get/set methods so why does trying to set
either one generate a compile error??
.
- Follow-Ups:
- References:
- Prev by Date: Re: Why does this.Location.X = 0; generate a compile error?
- Next by Date: RE: Unit Test / Config File / Logging Application Block
- Previous by thread: Re: Why does this.Location.X = 0; generate a compile error?
- Next by thread: Re: Why does this.Location.X = 0; generate a compile error?
- Index(es):
Relevant Pages
|