This is inefficient because every time you call SetLength, it re-allocates the array in memory, then copies the contents from the old allocation to the new one.
A better method is to initially declare the array big enough to fit what you expect to put into it. Before doing the insert, check that the array is big enough to fit it. Only if it does not have spare space call SetLength to re-size it. So you keep track of how much of the array you are using, and if you need more make it bigger. You can also make it smaller by checking it's size when you do deletes if you want. -----Original Message----- From: vss [mailto:[EMAIL PROTECTED] Sent: Monday, 31 March 2003 11:00 a.m. To: Multiple recipients of list delphi Subject: Re: [DUG]: Array of components Al. you need to do SetLength since your array is dynamic. So, SetLength(DirMonArray, High(DirMonArray)+1) DirMonArray[High(DirMonArray) - 1].Create(self); notice I have replace the DminCount with "High(DirMonArray)" as then you dont have to maintain any global variables. Jeremy -----Original Message----- From: Alistair George <[EMAIL PROTECTED]> To: Multiple recipients of list delphi <[EMAIL PROTECTED]> Date: Mon, 31 Mar 2003 10:50:26 +1200 Subject: [DUG]: Array of components > Hi All having probs creating an array (in Create line): > > DirMonArray: array of TDirMonitor; //can this be an open array - or > must it > have [0..100] for example assigned? > > procedure TMainform.DirMonArrayAdd(WatchDir: string); > var Tval: Integer; > CurrDir: string; > begin > //Irrelevant lines removed > inc(DmonCount, 1); > DirMonArray[DmonCount - 1].Create(self); //<<= creation error here > end; > > > > > Help?? > > ----------------------------------------------------------------------- > ---- > New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED] > Website: http://www.delphi.org.nz > To UnSub, send email to: [EMAIL PROTECTED] > with body of "unsubscribe delphi" > Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/ --------------------------------------------------------------------------- New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED] Website: http://www.delphi.org.nz To UnSub, send email to: [EMAIL PROTECTED] with body of "unsubscribe delphi" Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/ --------------------------------------------------------------------------- New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED] Website: http://www.delphi.org.nz To UnSub, send email to: [EMAIL PROTECTED] with body of "unsubscribe delphi" Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/