[lazarus] More of a FPC question but... dynamic arrays

Tony Maro tonym at nlisc.com
Fri Oct 4 09:41:18 EDT 2002


On Sun, 2002-09-29 at 20:44, Andrew Johnson wrote:
> 
> 
> var
>   myarray : ^TObject;
> begin
>   ...//do stuff
>   reallocmem(myarray, 4*SizeOf(TObject));//allocmem to handle 4 objects
>   myarray[2] := thisobject;//set the 3 entry
>   ...//do stuff.
>   reallocmem(myarray, 0);//free memory
> end;
> 
> you simply use reallocmem in place of setlength, and multiply the length by 
> the sizeof the type it contains.. at one point I actually created wrappers 
> arround this to make more like Delphi dynamic arrays..but its been awhile so 

That's been working great!  Thanks...

Next question:  How can I return one from an object similar to TStrings?
I checked the LCL sources and couldn't figure it out.

Here's what I want:

Type TMyObj = class(TObject)
Private
  FMyArray: ^TMyRecord
public
  procedure Add(MyItem: TMyRecord);
  procedure Delete(MyIndex: Integer);
  function Count: Integer;
  function Items{here's where I lose it - is it Items(Index: Integer)?}
end; 
	

{Sample usage:}
MyObj := TMyObj.Create;

MyObj.Add(ThisItem); // adds an item to the array
MyItem := MyVar.Items[1]; // grabs the first item on array


Thanks!
Tony






More information about the Lazarus mailing list