7

My Property Class:

unit SubImage;

interface

type
TSubImage = class
private
  { private declarations }
  function getHeight: Integer;
  function getWidth: Integer;
  procedure setHeight(const Value: Integer);
  procedure setWidth(const Value: Integer);
protected
  { protected declarations }
public
  { public declarations }
  property width : Integer read getWidth write setWidth;
  property height : Integer read getHeight write setHeight;
published
  { published declarations }
end;
implementation

{ TSubImage }

function TSubImage.getHeight: Integer;
begin
  Result:= height;
end;

function TSubImage.getWidth: Integer;
begin
  Result:= width;
end;

procedure TSubImage.setHeight(const Value: Integer);
begin
  height:= Value;
end;

procedure TSubImage.setWidth(const Value: Integer);
begin
  width:= Value;
end;

end.

Assignment:

objSubImg.width:= imgOverview.width;
objSubImg.height:= imgOverview.heigh

Interesting Error:

stackoverflow at xxxxxx

I am learning to properties in Delphi. I created a class, but it gives an error. I couldn't understand, where is my mistake?

Also i dont understand why we use property instead of setter/getter methods. Anyway can someone help me, how can i fix this code ?

I can not set property value.

5
  • why always vote down? I just trying to learn....... Commented Jan 16, 2015 at 14:12
  • 2
    I really don't understand the downvote. This is a quite beautifully asked question. You could not have asked it better. Commented Jan 16, 2015 at 14:14
  • @DavidHeffernan thank you to support me Commented Jan 16, 2015 at 14:16
  • 1
    One of the most important things to learn is: how to use the debugger. When you get an error like this, put a break-point on the line that causes the stack overflow error. When you hit the break-point, press F7 to step into the next procedure that get called. You'll soon notice you keep going into the same method (because you've written an infinite recursion). This is why you run out of stack space and get the error. Commented Jan 16, 2015 at 14:19
  • @CraigYoung yes, i debugged and occured infinite recursion. You are right but i thought i made simple mistake. Because, i am new on Delphi Commented Jan 16, 2015 at 14:21

1 Answer 1

6

This is a non terminating recursion. The getter looks like this:

function TSubImage.getHeight: Integer;
begin
  Result := height;
end;

But height is the property. So the compiler re-writes this as:

function TSubImage.getHeight: Integer;
begin
  Result := getHeight;
end;

And that's a non-terminating recursion. Hence the stack overflow.

You need to declare fields to store the values:

type
  TSubImage = class
  private
    FHeight: Integer;
    FWidth: Integer;
    function getHeight: Integer;
    function getWidth: Integer;
    procedure setHeight(const Value: Integer);
    procedure setWidth(const Value: Integer);
  public
    property width: Integer read getWidth write setWidth;
    property height: Integer read getHeight write setHeight;
  end;

And then get and set the values:

function TSubImage.getHeight: Integer;
begin
  Result:= FHeight;
end;

procedure TSubImage.setHeight(const Value: Integer);
begin
  FHeight:= Value;
end;

And likewise for the other property.

In this simple example you don't need to use getter and setter functions. You could declare the properties like this:

property width: Integer read FWidth write FWidth;
property height: Integer read FHeight write FHeight;

But I guess you know that and are exploring how getter/setter functions work.

As for why we use properties rather than getter and setter functions, that comes down to clarity and readability of code. You can replace properties with getter and setter functions. After all, that's all that the compiler does. But it is often clearer to write:

h := obj.Height;
obj.Height := h*2;

than

h := obj.GetHeight;
obj.SetHeight(h*2);
Sign up to request clarification or add additional context in comments.

2 Comments

Or obj.Height := obj.Height + 10; is IMO easier to understand than obj.SetHeight(obj.GetHeight + 10);. although it is exactly equivalent.
Beginners might mistake a property for a var. While objects without variables are possible it rarily makes sense if they'd not have their own storage for memory. Properties don't store memory, because they're not variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.