Re: Dynamic arrays of record/object



On Mar 13, 8:05 am, Rob Kennedy <m...@xxxxxxxxxxx> wrote:
sakkie...@xxxxxxxxx wrote:
On Mar 13, 1:31 am, Rob Kennedy <m...@xxxxxxxxxxx> wrote:
sakkie...@xxxxxxxxx wrote:
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;

Hi, if I remove the constructor, desctructor and unnecesary properties
it works fine,

No it doesn't. None of those were problems. They were just things that
made your code more complicated.

The problem is where you call Data.Create. It's a common mistake, but
the compiler should warn you about it because you're calling a method on
a variable that hasn't been initialized.

excuse my ignorance, but isn't the constructor the way to initialize
the variable/class?


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????

You need to be more specific than "doesn't work." What fails? Why? What
debugging effort have you made?

Sorry, I have included the full source again. The problem is in
AddSection. It sets the length of the array without problem (FCount is
0 and lenght of the array is 1), but when I try to assign to 1 of the
2 variables in the class it gives an access violation?


Can this be done?

  TSectionData = class(TObject)

  private
    DataHeader: string;
    Data: string;
  public
  end;

That's a useless class. It's entirely private data, which isn't
accessible outside the class.


its useless now, but I need this to be a class for future
functionality.

--
Rob- Hide quoted text -

- Show quoted text -

type

TSectionData = class
private
public
DataHeader: string;
Data: string;
end;

TXMLSectionData = class(TObject)

private
public
FCount: Integer;
FSectionData: array of TSectionData;
procedure AddSection(aDataHeader: string; aData: 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(aDataHeader, aData: string);
begin
SetLength(FSectionData,FCount+1);
FSectionData[FCount].DataHeader:= aDataHeader;
FSectionData[FCount].Data:= aData;
Inc(FCount);
end;

procedure TForm1.Button1Click(Sender: TObject);
var aData: TXMLSectionData;
begin
aData.FCount:= 0;
aData.AddSection('Header1','test data');
aData.AddSection('Header1','test data');
end;


end.
.


Quantcast