Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6036877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:59:23+00:00 2026-05-23T05:59:23+00:00

I was just wondering about why should I use property in a class instead

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:59:24+00:00Added an answer on May 23, 2026 at 5:59 am

    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!
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just wondering about the performance impact of copying very large php variables. For example
Just wondering about this code below... when I turn off my internet connection and
Just wondering about some practice about this; I have made a simple visual C#
Possible Duplicate: When should I make explicit use of the this pointer? I'm wondering
I just wondering about this.It is said that .NET is better than MFC in
I'm just wondering about large projects - say an airlines reservation system, how many
I was just wondering about something. I have a frame that loads pages and
I'm new to web development and am just wondering about best practices for java
I am using .Net3.5. Just wondering my understanding about the insert, update, delete are
Just wondering how you would go about implementing something similar to stackoverflow'd related questions.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.