Discussion:
Creating a Dynamic Array referencing a Class Object
(too old to reply)
Nick DeLallo
2008-07-13 21:09:26 UTC
Permalink
I define a dynamic array of Unit2.TestClassExample;

XClassEx : array of Unit2.TclassExample; // okay
//xClassEx := unit2.TClassExample(self); // does not work
setlength(xclassEx,100); // okay

// add to the array I get an access error
// How do I initialize the array.
// TclassExample is a record containing variable data
Warren Postma
2008-07-18 13:25:38 UTC
Permalink
Post by Nick DeLallo
I define a dynamic array of Unit2.TestClassExample;
XClassEx : array of Unit2.TclassExample; // okay
//xClassEx := unit2.TClassExample(self); // does not work
setlength(xclassEx,100); // okay
// add to the array I get an access error
// How do I initialize the array.
// TclassExample is a record containing variable data
Probably TclassExample is not a record, it's a class. You need to
understand the difference between a record and a class.

The main thing to remember about Class types is that declaring one is
not enough, you must also construct it.

var
Foo: Array of TMyClass
begin
SetLength(Foo,10);
/// Foo[0] := TMyClass.Create; // if we don't do this first...
Foo[0].property1 := 10; // ... then we get an Access violation!
end;

Loading...