Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
TCollection The Class for Master Detail Relations Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
07-Nov-02
Category
OO-related
Language
Delphi 3.x
Views
129
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Pieter Valentijn

Getting A master Detail Relation in a component on a way it's easely streamed. 
Using a TStream Class Decendant

Answer:

The collection Class is one of my favorit when it comes to storing multiple Data 
with one component its even posible to include the Collection in its own item 
making it useful for recursion (The Collection Can have a item withs can hold a 
other collection. 

If you want the standard Editor for Collections u have to use a TOwnedCollection i 
think its ijn the unit Classes from delphi 4 but if u have D3 u need to Make that 
class first like this 

1   TOwnedCollection = class(TCollection)
2   private
3     FOwner: TPersistent;
4   protected
5     function GetOwner: TPersistent; override;
6   public
7     // Fil in the AOwner in The Fowner proeprty on the Create Constructor .
8     constructor Create(AOwner: TPersistent; ItemClass: TCollectionItemClass);
9   end;
10  
11  //Heres a Example Of a collection That does that 
12  
13  unit Unit1;
14  
15  interface
16  
17  uses
18    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
19    StdCtrls;
20  
21  type
22    TMyCollection = class(TOwnedCollection)
23  
24    end;
25  
26    TMyCollectionItem = class(TCollectionItem)
27    private
28      FANummer: Integer;
29      FAString: string;
30      FMoreCollections: TMyCollection;
31      procedure SetANummer(const Value: Integer);
32      procedure SetAString(const Value: string);
33      procedure SetMoreCollections(const Value: TMyCollection);
34    public
35      constructor Create(Collection: TCollection); override;
36      destructor Destroy; override;
37    published
38      property AString: string read FAString write SetAString;
39      property ANummer: Integer read FANummer write SetANummer;
40      property MoreCollections: TMyCollection read FMoreCollections write
41        SetMoreCollections;
42    end;
43  
44    TCollectionWrapper = class(TComponent)
45  
46    private
47      FCollection: TMyCollection;
48      procedure SetCollection(const Value: TMyCollection);
49    public
50      constructor Create(AOwner: TComponent); override;
51      destructor Destroy; override;
52  
53    published
54      property Collection: TMyCollection read FCollection write SetCollection;
55    end;
56  
57  implementation
58  
59  { TMyCollectionItem }
60  
61  constructor TMyCollectionItem.Create(Collection: TCollection);
62  begin
63    inherited;
64    FMoreCollections := TMyCollection.Create(self, TMyCollectionItem);
65  end;
66  
67  destructor TMyCollectionItem.Destroy;
68  begin
69    FMoreCollections.free;
70    inherited;
71  
72  end;
73  
74  procedure TMyCollectionItem.SetANummer(const Value: Integer);
75  begin
76    FANummer := Value;
77  end;
78  
79  procedure TMyCollectionItem.SetAString(const Value: string);
80  begin
81    FAString := Value;
82  end;
83  
84  procedure TMyCollectionItem.SetMoreCollections(const Value: TMyCollection);
85  begin
86    FMoreCollections := Value;
87  end;
88  
89  { TCollectionWrapper }
90  
91  constructor TCollectionWrapper.Create(AOwner: TComponent);
92  begin
93    inherited;
94    FCollection := TMyCollection.Create(self, TMyCollectionItem);
95  end;
96  
97  destructor TCollectionWrapper.Destroy;
98  begin
99    FCollection.free;
100   inherited;
101 
102 end;
103 
104 procedure TCollectionWrapper.SetCollection(const Value: TMyCollection);
105 begin
106   FCollection := Value;
107 end;
108 
109 end.
110 
111 This is how you could addres it Run time
112 
113 procedure TForm1.Button1Click(Sender: TObject);
114 var
115   ACollection: TCollectionWrapper;
116 begin
117   ACollection := TCollectionWrapper.Create(Self);
118   try
119     // Default add gives u a TCollectionItem So u need to cast it
120     with TMyCollectionItem(ACollection.Collection.Add()) do
121     begin
122       AString := 'Hallo';
123       ANummer := 5;
124       MoreCollections.add;
125 
126     end;
127 
128   finally
129     ACollection.Free;
130   end;
131 end;


If you register this component u will be able to use the default Collection Editor 
Design time

Component Download: http://www.xs4all.nl/~suusie/Pieter/Programs/CollectionComponent.ziphttp://www.xs4all.nl/~suusie/Pieter/Programs/CollectionComponent.zip

			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC