Solved

Accessing a property of TCollectionItem's owner component

Posted on 2004-10-17
10
436 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
Comment Utility
FListener.Initialize(TtbrvListener(Self.Collection.Owner).FTibrv.GetDefaultQueue,TtbrvListener(Self.Collection.Owner).FNetTransport, FName, 0);
0
 
LVL 9

Expert Comment

by:ginsonic
Comment Utility
Your collection item owner is : TtbrvListener(Self.Collection.Owner)

With it you can do everthing.
0
 
LVL 4

Expert Comment

by:ceoworks
Comment Utility
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
Comment Utility
<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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 9

Expert Comment

by:ginsonic
Comment Utility
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
Comment Utility
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
Comment Utility
Just a friendly comment. See on net ! :)
0
 
LVL 4

Expert Comment

by:ceoworks
Comment Utility
<Just a friendly comment>

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

Author Comment

by:meyousikmann
Comment Utility
Thanks for all the responses........the help is much appreciated
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

11 Experts available now in Live!

Get 1:1 Help Now