Compile error when used as Property

  • Thread starter Thread starter Zany
  • Start date Start date
Z

Zany

Hi,

Could someone please explain why the following doesn't compile. This turned
up as I was programming yesterday.

Thanks,
Zany

public class A {



public Rectangle r;



public Rectangle GetRect {

get {return r;}

}



public void Test() {



r.Width = 5; // works

Rectangle r1 = GetRect; // works

GetRect.Width = 5; // <-- won't compile



//

// Cannot modify the return value of 'A.GetRect' because it is not a
variable

//

}

}
 
System.Drawing.Rectangle is a struct (value type) rather than a class
(reference type).
 
Thanks Clive

Lightbulb. Got it. Works now.

public class A {



public Rectangle r;



public Rectangle Rect {

get {return r;}

set {r = value;}

}



public void Test() {



r.Width = 5; // works (but exposed)



// or

Rectangle r1 = Rect; // works (through property)

r1.Width = 5;

Rect = r1;

}

}
 
I think the problem is the following:

GetRect.Width = 5; // <-- won't compile

needs to be changed to this:

GetRect().Width = 5; //

The class A method 'GetRect' is a function, and you used syntactically like
a variable....

In fact:

Rectangle r1 = GetRect; // works

should have also generated an error, although you say it didn't, which is
weird...

[==P==]
 
Oh, wait. My bad. The problem is you haven't defined GetRect as a PROPERTY,
which I now see was your intent...

[==P==]
 
Back
Top