Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use property in a class?

I was just wondering about why should I use property in a class instead of "normal" variables (class attributes?). What I mean is this:

TSampleClass = class
  public
    SomeInfo: integer;
end;

TPropertyClass = class
  private
    fSomeInfo: integer;
  public
    property SomeInfo: integer read fSomeInfo write fSomeInfo;
end;

What is the big difference? I know that I can define getter and setter methods for getting or saving the property respectively, but that is possible even without the variable being a "property".

I tried searching for why to use it, but nothing useful came up, so I'm asking here.

Thank you

like image 361
Martin Melka Avatar asked Jun 17 '11 20:06

Martin Melka


People also ask

What does the properties class actually do?

As you've stated, the Properties class does give the ability to read/save from/to disk, but it does a lot more than just that. It can read and store from any InputStream, so you can parse Properties from a String, or read Properties from a URL, or a Socket, or what have you.

What is a property in C programming?

Using Properties (C# Programming Guide) Properties combine aspects of both fields and methods. To the user of an object, a property appears to be a field, accessing the property requires the same syntax. To the implementer of a class, a property is one or two code blocks, representing a get accessor and/or a set accessor.

What is a property in Java?

To the implementer of a class, a property is one or two code blocks, representing a get accessor and/or a set accessor. The code block for the get accessor is executed when the property is read; the code block for the set accessor is executed when the property is assigned a new value. A property without a set accessor is considered read-only.

What are the advantages of properties in programming languages?

If you put such data in your code (as a enum or class) and want to modify it, you have to build the code again. The main advantage of properties is that they are stored externally or outside the source code thus allowing you to modify them as and when required.


1 Answers

This is just a very simple example of a specific case, but still, it is a very common case.

If you have a visual control, you might need to repaint the control when you change a variable/property. For instance, let's say your control has a BackgroundColor variable/property.

The simplest way of adding such a variable/property is to let it be a public variable:

TMyControl = class(TCustomControl)
public
  BackgroundColor: TColor;
...
end;

And in the TMyControl.Paint procedure, you paint the background using the value of the BackgroundColor. But this doesn't do it. Because if you change the BackgroundColor variable of an instance of the control, the control doesn't repaint itself. Instead, the new background colour will not be used until the next time the control redraws itself for some other reason.

So you have to do it like this:

TMyControl = class(TCustomControl)
private
  FBackgroundColor: TColor;
public
  function GetBackgroundColor: TColor;
  procedure SetBackgroundColor(NewColor: TColor);
...
end;

where

function TMyControl.GetBackgroundColor: TColor;
begin
  result := FBackgroundColor;
end;

procedure TMyControl.SetBackgroundColor(NewColor: TColor);
begin
  if FBackgroundColor <> NewColor then
  begin
    FBackgroundColor := NewColor;
    Invalidate;
  end;
end;

and then the programmer using the control has to use MyControl1.GetBackgroundColor to obtain the colour, and to use MyControl1.SetBackgroundColor to set it. That's awkward.

Using properties, you can have the best of both worlds. Indeed, if you do

TMyControl = class(TCustomControl)
private
  FBackgroundColor: TColor;
  procedure SetBackgroundColor(NewColor: TColor);
published
  property BackgroundColor: TColor read FBackgroundColor write SetBackgroundColor;
end;

...

procedure TMyControl.SetBackgroundColor(NewColor: TColor);
begin
  if FBackgroundColor <> NewColor then
  begin
    FBackgroundColor := NewColor;
    Invalidate;
  end;
end;

then

  • from the programmer's point of view, he can both read and set the background colour using a single identifier, the MyControl1.BackgroundColor property, and
  • the control is repainted when he sets it!
like image 153
Andreas Rejbrand Avatar answered Sep 28 '22 21:09

Andreas Rejbrand