Description
This example shows how to use a Property with a static data member. StaticProperties.cpp StaticProperties.h Copyright © 1997-2002 Rodolfo A. Frino. All rights reserved.
Form 1 contains a TButton button.
#include <vcl.h>
#pragma hdrstop
#include "StaticProperties.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//SECOND DECLARATION OF MyStaticInt AND INITIALIZATION
int TMyClass::MyStaticInt = 99;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
int __fastcall TMyClass::GetMyStaticInt()
{
return MyStaticInt;
}
void __fastcall TMyClass::SetMyStaticInt( int i )
{
MyStaticInt = i ;
}
int __fastcall TMyClass::Get()
{
return MyStaticInt;
}
void __fastcall TMyClass::Set( int i )
{
MyStaticInt = i ;
}
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TMyClass oMyClass;
oMyClass.MyInt = 1000;
ShowMessage("oMyClass.MyInt = " + String( oMyClass.MyInt ) );
//DOUBLE CHECK THE VALUE
ShowMessage("MyStaticInt = " + String( oMyClass.Get() ) );
oMyClass.MyInt = 2000;
ShowMessage("oMyClass.MyInt = " + String( oMyClass.MyInt ) );
//DOUBLE CHECK THE VALUE
ShowMessage("MyStaticInt = " + String( oMyClass.Get() ) );
}
#ifndef StaticPropertiesH
#define StaticPropertiesH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.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:
static int MyStaticInt; //FIRST DECLARATION
int __fastcall GetMyStaticInt();
void __fastcall SetMyStaticInt( int i );
public:
__property int MyInt = { read = GetMyStaticInt, write = SetMyStaticInt };
// TO ACCESS STATIC DATA MEMBER IN THE PROGRAM
// TO DOUBLE CHECK THE VALUE OF THE STATIC DATA MEMBER
int __fastcall Get();
void __fastcall Set( int i );
};
extern PACKAGE TForm1 *Form1;
//------------------------------------------------------------
#endif