Re: Dynamic arrays of record/object
- From: sakkieLFS@xxxxxxxxx
- Date: Wed, 12 Mar 2008 22:57:12 -0700 (PDT)
On Mar 13, 1:31 am, Rob Kennedy <m...@xxxxxxxxxxx> wrote:
sakkie...@xxxxxxxxx wrote:
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!
According to this code, your problem should not be in setting the
array's length. The problem occurs beforehand.
type
TSectionData = record
DataHeader: string;
Data: string;
end;
TXMLSectionData = class(TObject)
private
FCount: Integer;
FSectionData: array of TSectionData;
function GetCount: integer;
public
constructor Create;
destructor Destroy; override;
property Count: integer read GetCount write FCount;
Don't have a property that directly writes the value of that field. If
you assign a new value of the property, then FCount is no longer an
accurate representation of the length of the array.
You don't really even need FCount. The array already knows its own
length, and it's easy to detect with the Length function.
procedure AddSection(DataHeader: string; Data: string);
end;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TXMLSectionData }
procedure TXMLSectionData.AddSection(DataHeader, Data: string);
Declare those parameters const to avoid unnecessary reference counting.
begin
SetLength(FSectionData,FCount+1);
FSectionData[FCount].DataHeader:= DataHeader;
FSectionData[FCount].Data:= Data;
Inc(FCount);
end;
constructor TXMLSectionData.Create;
begin
inherited;
FCount:= 0;
SetLength(FSectionData,FCount);
Dynamic arrays start out with length zero, so you don't really need to
set it explicitly.
Likewise, a class begins life with numeric fields zero and dynamic
arrays empty, so you don't really need a constructor at all.
end;
destructor TXMLSectionData.Destroy;
begin
inherited;
end;
If that's all you're really doing, then you don't need to have a
destructor in this class. The one it inherits from its ancestor is
sufficient.
procedure TForm1.Button1Click(Sender: TObject);
var Data: TXMLSectionData;
begin
Data.Create;
That line will surely fail. Doesn't the compiler warn you about using an
uninitialized variable?
Data.AddSection('Header1','test data');
end;
function TXMLSectionData.GetCount: integer;
begin
Result := FCount;
end;
--
Rob- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -
Hi, if I remove the constructor, desctructor and unnecesary properties
it works fine, but if I now change TSectionData to a class instead of
a record (which I ultimately need it to be) again it doesn't work????
Can this be done?
TSectionData = class(TObject)
private
DataHeader: string;
Data: string;
public
end;
.
- Follow-Ups:
- Re: Dynamic arrays of record/object
- From: Rob Kennedy
- Re: Dynamic arrays of record/object
- References:
- Dynamic arrays of record/object
- From: sakkieLFS
- Re: Dynamic arrays of record/object
- From: Rob Kennedy
- 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
- Next by thread: Re: Dynamic arrays of record/object
- Index(es):