Solved

Accessing a property of TCollectionItem's owner component

Posted on 2004-10-17
10
439 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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.

910 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now