C++ Builder

La Isla Bonita



Private Properties
Private Properties


Description

In general, properties are declared publicaly, however it is possible to declare properties privately. The use of private properties should be avoided whenever posible. However, this method is given to illustrate how to use private properties, if necessary.
Form 1 contains a TButton button.

PrivateProperties.cpp

#include <vcl.h>
#pragma hdrstop

#include "PrivateProperties.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------


String __fastcall TMyClass::GetMyString()
{
    return Name;
}

void __fastcall TMyClass::SetMyString( String S )
{
    Name = S ;
}


void __fastcall TForm1::Button1Click(TObject *Sender)
{

   String S;
   TMyClass O;

   O.SetPropName("Hello");

   ShowMessage("PropName = " + O.GetPropName() );

}

PrivateProperties.h

#ifndef PrivatePropertiesH
#define PrivatePropertiesH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <sForms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
        TButton *Button1;
        void __fastcall Button1Click(TObject *Sender);
private:	// User declarations
public:		// User declarations
        __fastcall TForm1(TComponent* Owner);
};

class TMyClass
{
  private:
    String Name;
    String __fastcall GetMyString();
    void __fastcall   SetMyString( String S );
    __property String PropName = { read = GetMyString, write = SetMyString };

  public:

    String GetPropName()
    {
        return PropName;
    }

    void SetPropName(String S)
    {
        PropName = S;
    }

};

//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

Homepage

Copyright © 1997-2002 Rodolfo A. Frino. All rights reserved.