Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
500 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (3)


static property of class in DelphiFormat this article printer-friendly!Bookmark function is only available for registered users!
How can I implement a static property like C++ static member
Product:
Delphi 6.x (or higher)
Category:
Object Pascal
Skill Level:
Scoring:
Last Update:
02/23/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet static property member
Times Scored:
5
Visits:
2900
Uploader: ya xiang
Company: fyao soft co. ltd.
Reference: N/A
 
Question/Problem/Abstract:
static property of class
Answer:



Because Delphi not provide static member for our using, so if you want a static
member of class in Delphi what should I do ?
Ok! At least there are three ways to implement this.
1,Using private global variant(in implement section of unit),but there are a lit

visibility scope in project or unit.
2,Using global variant, a more visibility scope in project or unit.a lit same as

above.
3,Using typed constant,just using compiler directive $J+ or Project properties

|compiler Option Typed constant.
There are some simple source code as follows:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  TStaticMemberObject = class
  private
    class procedure SetName(const Value: string);
    class function GetName: string;
    class function StaticMethod(ReadFlag: boolean; var Value: string):boolean;
  published
    property Name: string read GetName write SetName;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ TStaticMemberObject }

class function TStaticMemberObject.StaticMethod(ReadFlag: boolean;
  var Value: string): boolean;
const static: string = '';
begin
  result := True;
  if ReadFlag then
     Value := static
  else
    static := value;
end;


class function TStaticMemberObject.GetName: string;
var
  Value: string;
begin
  StaticMethod(true, Value);
  result := Value;
end;

class procedure TStaticMemberObject.SetName(const Value: string);
var
  setvalue: string;
begin
  setvalue := Value;
  StaticMethod(false, setvalue);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  so: TStaticMemberObject;
begin
  so.Name := 'Hello, my static property!';
  ShowMessage(so.Name);
  so.SetName('hi, my static property!');
  ShowMessage(so.GetName);
end;

end.

  Please pay attention to the class method used here. The Name property's modifiers

both use class method. In fact if you use normal static method with no class

directive the code still works well. like this:
    procedure SetName(const Value: string);
    function GetName: string;
Because we do not access other fields and other instance methods of this

TStaticMemberObject class or some object of TStaticMemberObject type, so there is

no AV using normal static method.
OK, if you need this trick just try it.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Interesting approach...
    Scott Price (Feb 26 2002 4:13PM)

An interesting article.  Ok, so the english isn't brilliant, but it is far and away better than my Chinese!

Whilst not many people will find this as interesting, I am particularly drawn to it due to a current project I am helping with to convert the Quake 2 source code from C to Delphi.  There are a lot of Static elements, albeit not in a class, but still this is an interesting read and an interesting approach.
Respond

RE: Interesting approach...
ya xiang (Feb 26 2002 5:12PM)

Thank you. I'm having my best to impove my english writing skill. And
I'm happy to do this.
Respond

RE: Interesting approach...
Apz28 Pham (Mar 11 2002 6:16AM)

//Here is the class

unit Unit4;

interface

type
  TStaticProperty = class(TObject)
  private
    class function GetIntVal: Integer;
    class procedure SetIntVal(AValue: Integer);
  public
    property IntVal: Integer read GetIntVal write SetIntVal;
  end;

implementation

var
  StaticPropertyIntVal: Integer;
  
{ TStaticProperty }
class function TStaticProperty.GetIntVal: Integer;
begin
  //If use in multithread, you must implement the lock codes
  Result:= StaticPropertyIntVal;
end;

class procedure TStaticProperty.SetIntVal(AValue: Integer);
begin
  //If use in multithread, you must implement the lock codes  StaticPropertyIntVal:= AValue;
end;

end.


//How to use it
unit Unit5;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
  TForm1 = class(TForm)
    procedure FormShow(Sender: TObject);
  private
  public
  end;

var
  Form1: TForm1;

implementation

uses
  Unit4;
  
{$R *.DFM}

procedure TForm1.FormShow(Sender: TObject);
var
  StaticProperty: TStaticProperty;
begin
  //No need to create an instance of the class before use
  StaticProperty.IntVal:= 5;  
  Showmessage(IntToStr(StaticProperty.IntVal));
end;

end.


Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
M. Kleiner
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)