Re: Why does this.Location.X = 0; generate a compile error?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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??





.



Relevant Pages

  • Re: komplexes Problem mit Funktionszeigern
    ... Die Funktion gibt einen struct "by value" zurueck und bei der ... am Borland Compiler, da der Microsoft Compiler ... Man kann Argumente fuer eine Funktion z.B auf dem Stack uebergeben. ... Das funktioniert natuerlich nur fuer "Basis Typen" die in ein Register passen. ...
    (de.comp.lang.c)
  • Re: Naming typedefs
    ... problem that a scalar could be returned in the A register ... to have waste precious cpu cycles copying the struct there ... of the hardware or twentieth century compiler technology. ... # to include the header defining it into every other header (assuming ...
    (comp.unix.programmer)
  • Re: assembly language and reverse engineering
    ... "return arguments to struct pointed to by register" and ... assumptions of CDECL-like conventions (also supports STDCALL, ... arg for handling struct return (slightly compiler, and compiler version, ...
    (alt.lang.asm)
  • Re: HardBound and SoftBound (was "The State of Software")
    ... I will explain to you one last time that the problem is not whether a compiler can generate checks for all buffer overflows. ... By the way, in the last example of such code that I saw, it had been changed to use the address of the struct, not of the first element of the struct: ... to take address of struct rather than address of first element. ... such tricks are used to create a pointer which is then passed on. ...
    (comp.arch)
  • Re: C++ in ternms of C
    ... >about class and struct difference. ... How is the private and public part ... I tried to analise the ASM file produ ced my the VC++ compil er. ... >epresented by the c++ compiler in translation to intermediate stage. ...
    (microsoft.public.vc.language)