Solved

Accessing a property of TCollectionItem's owner component

Posted on 2004-10-17
10
448 Views
Last Modified: 2013-11-23
I am creating a component that has a collection.  Within the creation of the collection item, I need to get access to a property of the component object that owns the collection.  Here is the component code:

TSubject = class(TCollectionItem)
private
  {Private declarations
  FName: String;
  FListener: ITibrvListener;
  FOnMessage: TTIbrvComLibITibrvListenerEvents;

  FOnMessageReceived: TMessageReceived;

  procedure SetName(const Value: String);
protected
  {Protected declarations}
  function GetDisplayName: String; override;

  procedure MessageReceived(Sender: TObject; const listener: ITibrvListener; const message: ITibrvListener; const Message: ITibrvMsg); dynamic;
published
  {Published declarations}
  property Name: String read FName write SetName;

  property OnMessageReceived: TMessageReceived read FOnMessageReceived write FOnMessageReceived;
public
  {Public declarations}
  constructor Create(Collection: TCollection); override;
  destructor Destroy; override;
end;

TSubjects = class(TOwnedCollection)
private
  {Private declarations}
  function GetItem(Index: Integer): TSubject;
  procedure SetItem(Index: Integer; const Value: TSubject);
protected
  {Protected declarations}
published
  {Published declarations}
public
  {Public declarations}
  function Add: TSubject;
  procedure Delete(Index: Integer);
  function Insert(Index: Integer): TSubject;
  property Items[Index: Integer]: TSubject read GetItem write SetItem; default;
end;

TtbrvListener = class(TComponent)
private
  {Private declarations}
  FTibrv: ITibrv;
  FNetTransport: ITibrvTransport;
  FSubjects: TSubjects;
protected
  {Protected declarations}
public
  {Public declarations}
  constructor Create(AOwner: TComponent); override;
  destructor Destroy; override;
published
  {Published declarations}
  property Subjects: TSubjects read FSubjects write FSubjects;
end;

Now, within the create procedure of the TSubject object, I need to access the FTibrv and FNetTransport objects of the collection's owning component (the TtbrvListener object).  Here is the constructor code for the TSubject collection item object where I need to do this.  Note that I have simply inserted [OwnerComponent] in the code where I need to get into the TtbrvListener owner component and access the FTibrv and FNetTransport objects because I don't know how to do this.

constructor TSubject.Create(Collection: TCollection);
begin
  inherited Create(Collection);
  FListener.initialize([OwnerComponent].FTibrv.GetDefaultQueue, [OwnerComponent].FNetTransport, FName, 0);
end;
 
0
Comment
Question by:meyousikmann
  • 5
  • 4
10 Comments
 
LVL 9

Accepted Solution

by:
ginsonic earned 500 total points
ID: 12336190
FListener.Initialize(TtbrvListener(Self.Collection.Owner).FTibrv.GetDefaultQueue,TtbrvListener(Self.Collection.Owner).FNetTransport, FName, 0);
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 12336200
Your collection item owner is : TtbrvListener(Self.Collection.Owner)

With it you can do everthing.
0
 
LVL 4

Expert Comment

by:ceoworks
ID: 12336869
Add a FListener variable to your TSubject object which has a type of TtbrvListener. At that time you'll have a link to the TtbrvListener object but you need to change your TSubject's constructor like this :

constructor TSubject.Create(Collection: TCollection; AListener: TtbrvListener);
begin
  FListener := AListener;
  ..
  ..
end;

For to access the private properties of a TtbrvListener, you need to put TSubject and TtbrvListener to the same unit. And then you can access like this :

FListener.FTibrv := ...

Or you may choose to add share the FTibrv and FNetTransport properties by declaring a couple of property which will make them read only into the public section :

TtbrvListener = class(TComponent)
private
  {Private declarations}
  FTibrv: ITibrv;
  FNetTransport: ITibrvTransport;
  FSubjects: TSubjects;
protected
  {Protected declarations}
public
  {Public declarations}
  constructor Create(AOwner: TComponent); override;
  destructor Destroy; override;
  property Tibv: ITibrv read FTibrv;
  property NetTransport: ITibrvTransport read FNetTransport;
published
  {Published declarations}
  property Subjects: TSubjects read FSubjects write FSubjects;
end;

Regards,

Oktay
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 4

Expert Comment

by:ceoworks
ID: 12336906
<Or you may choose to add share the FTibrv and FNetTransport properties by declaring a couple of property which will make them read only into the public section>

should be :

"Or you may choose to share the FTibrv and FNetTransport fields by declaring a couple of property which will make them read only into the public section" :)
0
 
LVL 4

Expert Comment

by:ceoworks
ID: 12336931
If TSubject objects will need to set some new values to the FTibrv and FNetTransport, you can change their properties from readonly to read/write :

property Tibrv: ITibrv read FTibrv write FTibrv;
property NetTransport: ITibrvTransport read FNetTransport write FNetTransport;

Cheers,

Oktay Sancak
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 12338189
Why do you complicate the code when is so easy to get the collection owner :)

I use NOW on my new VCL exactly proposed solution. And work PERFECT!
0
 
LVL 4

Expert Comment

by:ceoworks
ID: 12338468
Sorry i didn't notice that TCollectionItem has a Collection property.. Of course the thing you said is the easier way to do it. Anyway, my example can be use for an another situation :)

Thanks for you to remind ginsonic, cheers..

Oktay
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 12338539
Just a friendly comment. See on net ! :)
0
 
LVL 4

Expert Comment

by:ceoworks
ID: 12341363
<Just a friendly comment>

I know :) have a nice day, see ya :)
0
 

Author Comment

by:meyousikmann
ID: 12347867
Thanks for all the responses........the help is much appreciated
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question