Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

class private shared member

Status
Not open for further replies.

rcloutie

Programmer
May 26, 2005
164
CA
Hi there,

I have a class which has standard members (i.e. string / integer fields). But there's a private variable of type TObjectList and the most beautiful thing of that member is that it is a list of object of itself... recursivity...

Also, there's another private variable object which I need to share between all instance stored in the TObjectList.

Here's some code to illustrate my point:
----------------------------------------------------------
type
TlstBase = class;
TclsBase = class
private
{ Private declarations }
sID : String;
sDesc : String;
iLevel : Integer;
-> oShare : TclsShare;
lstChild : TlstBase;
public
{ Public declarations }
property ID: string read sID write sID;
property Desc: string read sDesc write sDesc;
property Level: integer read iLevel write iLevel;
-> property Items: TlstBase read lstChild write SetItems;
end;

TlstBase = class(TObjectList)
private
function GetItem(Index: Integer): TclsBase;
procedure SetItem(Index: Integer; AObject: TclsBase);
public
function Add(AObject: TclsBase): Integer;
property Items[Index: Integer]: TclsBase read GetItem write SetItem; default;
end;
---------------------------------------------------------

So, the private member I need to share between Items is oShare. Is there a magic keyword that will do so???

Thanks a lot for reading,

Rej Cloutier
 
Private members of a class are visible to all classes declared within the same unit. Private members are only invisible to code outside of the unit the class is declared in.

So - add helper code to the same unit, or raise the visibility of oShare to public (this is best done by adding a public property that refers to oShare).
 
ok but even if the member is public, if declared within the scope of a class, it owns seperate instance of oShare. What I need to do is having oShare defined on the unit scope instead of TclsBase scope.

Therefore, oShare must exist and be visible only within the unit. But I think I solved my problem (not yet tested but in my mind it makes sense)...

What do you think of declaring var oShare in the implementation section of the unit? Would it be a good practice?

Thank you for help,

Rej Cloutier
 
Sorry - I didn't fully understand what you were asking the first time. Yes - sharing a variable between multiple classes is best done with a global var declared within the implementation section.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top