Delphi Initialize Array
Function CloneArray ( const A: array of Integer ): TIntegerDynArray; overload; var Idx: Integer; begin SetLength ( Result, Length ( A ) ); for Idx:= Low ( A ) to High ( A ) do Result [ Idx - Low ( A ) ]:= A [ Idx ]; end; function CloneArray ( const A: array of string ): TStringDynArray; overload; var Idx: Integer; begin SetLength ( Result, Length ( A ) ); for Idx:= Low ( A ) to High ( A ) do Result [ Idx - Low ( A ) ]:= A [ Idx ]; end; Noticing how similar these functions were - only the first line differs - I decided to try a generic approach. Since I didn't want to parameterise both the type of the array (e.g.
The TIntegerDynArray return value) and the array element (e.g. Integer) I first decided to change the return values of the functions to use the TArray type defined in the System unit. So the function prototypes got changed to. Type TArrayEx = class ( TArray ) public class function CloneArray ( const A: array of T ): TArray; end;.. Class function TArrayEx.

Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have. Discuss Dynamic Array of Objects in the Delphi forum on Tutorialized. Dynamic Array of Objects Delphi forum covering how Delphi was originally derived from Pascal. Delphi adds object-oriented capabilities to and already powerful language intended for use with Microsoft Windows.
CloneArray ( const A: array of T ): TArray; var Idx: Integer; begin SetLength ( Result, Length ( A ) ); for Idx:= Low ( A ) to High ( A ) do Result [ Idx - Low ( A ) ]:= A [ Idx ]; end; Don't confuse Generics.Collections.TArray with System.TArray - the first is a class, the second a generic dynamic array type. CloneArray assumes that straighforward assignment of array elements is what we want. For example if the array elements are objects, just the object pointer is copied. If you need the object's fields to be physically copied this simplistic approach won't work.


Delphi Array Of String
I'll close this post with an example of use. Here's a code fragment that initialises string and integer arrays. Anonymous said. WRT your opening question, you can in fact do this: uses Types; var A: TIntegerDynArray; begin A:= TIntegerDynArray.Create(1,2,3,4); end; There's nothing special to TIntegerDynArray - it's just to use a predefined typedef.
Comments are closed.