Re: Dynamic arrays of record/object
- From: "alanglloyd@xxxxxxx" <alanglloyd@xxxxxxx>
- Date: Thu, 13 Mar 2008 02:32:55 -0700 (PDT)
On 12 Mar, 18:46, sakkie...@xxxxxxxxx wrote:
Hi Al,<Snip>
I ready to pull out my hair on the creation of dynamic arrays of a
record or object. For some reason I'm having problems setting the
lenght of the array and assigning values to the elements of the array
(which is a record). I have included source code. Please help!
type
� TSectionData = record
� � DataHeader: string;
� � Data: string;
� end;
� TXMLSectionData = class(TObject)
I would recommend using descendants from TCollection /
TcollectionItem. These give you the "glue & skeleton" of a collection
of different objects. Used by Delphi in many of their components. It
holds the multiple items as a TList of objects.
Here's some code which implements your stuff into TCollection/
TCollectionItem descendants, and providing you put two buttons & a
listbox on the form, will enable you to add & display your test
objects.
This arrangement (TXMLSections / TXMLSectionData) also aids clarity of
code, because all those function which apply to the whole collection
you add to TXMLSections and all the functions which apply to an item
are added to TXMLSectionsData.
type
TXMLSectionData = class; // forward declaration
TXMLSections = class(TCollection)
private
function GetItem(Index : integer) : TXMLSectionData;
public
function Add : TXMLSectionData;
property Items[Index : integer] : TXMLSectionData read GetItem;
default;
{default keyword means you can access by
XMLSections[n]}
end;
TXMLSectionData = class(TCollectionItem)
private
FDataHeader: string;
FData: string;
public
property DataHeader: string read FDataHeader write FDataHeader;
property Data: string read FData write FData;
end;
TForm1 = class(TForm)
AddBtn: TButton;
ListBox1: TListBox;
ShowBtn: TButton;
procedure AddBtnClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure ShowBtnClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
var
XMLSections : TXMLSections;
{ TXMLSections }
function TXMLSections.Add : TXMLSectionData;
{saves having to typecast every Tcollection.Add()}
begin
Result := TXMLSectionData(inherited Add);
end;
function TXMLSections.GetItem(Index : integer) : TXMLSectionData;
{necessary for an indexed property
saves having to typecast every Tcollection.Items[]}
begin
Result := nil;
if (Index < Count) then
Result := TXMLSectionData(inherited Items[Index]);
end;
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
{create collection}
XMLSections := TXMLSections.Create(TXMLSectionData);
end;
procedure TForm1.AddBtnClick(Sender: TObject);
begin
{add an item to the collection}
with XMLSections.Add do begin
DataHeader := 'Data1 ' + IntToStr(XMLSections.Count);
Data := 'Test Data ' + IntToStr(XMLSections.Count);
end;
end;
procedure TForm1.ShowBtnClick(Sender: TObject);
var
i : integer;
begin
for i := 0 to XMLSEctions.Count - 1 do begin
ListBox1.Items.Add(XMLSections[i].DataHeader);
ListBox1.Items.Add(' ' + XMLSections[i].Data);
end;
end;
end.
Look up more in Delphi Help.
Alan Lloyd
.
- References:
- Dynamic arrays of record/object
- From: sakkieLFS
- Dynamic arrays of record/object
- Prev by Date: Re: Dynamic arrays of record/object
- Next by Date: Re: Dynamic arrays of record/object
- Previous by thread: Re: Dynamic arrays of record/object
- Index(es):