Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Accessing a property of TCollectionItem's owner component

Posted on 2004-10-17
10
452 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
delphi prevent click fast 2 238
Delphi: ForceDirectory plain function 7 42
FMX and jaudiotracker playing memory stream 29 85
Delphi and Access based Enumeration 9 70
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…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

809 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