Delphi:TAssArr
A rather neat feature of programming languages such as PHP and JavaScript is the ability to define array variables whose contents are indexed using descriptive text labels. For instance the $_SERVER superglobal variable in PHP is an associative array whose contents can be accessed using descriptive labels such as REQUEST_URI, HTTP_REFERER etc. Here we provide a Delphi implementation. The principal properties of TAssArr are listed below
- KeyCount:Integer - This readonly property indicates the number of entries in the array.
- Keys[Index:Integer]:String - Provides access to the names of the indices, keys, associated with each entry in the array.
- Integers[const Index:String]:Integer - Used to access integer contents of the array.
- Reals[const Index:String]:Double - Use this property to read/write floating point values to the array.
- Strings[const Index:String]:Strings - This property should be used to read text element values from the array and to add new text entries to the array.
- Pointers[const Index:String]:Pointer - To store Delphi objects as an associative array use this property. A two dimensional associative array is easily created by storing sequences of appropriately created TAssArr objects as pointers in another instance of TAssArr.
Inappropriate use of these properties is liable to throw the following exceptions
EBadIndex- if Keys or any of the subsequent properties are accessed for reading with a nonexistentIndexvalue. WritingIntegers or any of the other array element access properties with a nonexistentIndexvalue will simply result in a new entry being to the array.EBadType- The array element access property used should correspond to the type of array that was created. For example, if you create an instance of TAssArr to store strings you should only use the Strings property to access array contents. If, for instance, Integers is used this exception will be thrown.
The principal methods of TAssArr are listed below
- constructor CreateEx(AType:TArrayTypes) - This is the object constructor. AType is determined by the nature of the data to be stored in the array. It takes one of the following values - atInt, atReal, atString or atPointer
- procedure Compact - Repeated calls to the Discard method, see below, can result in "holes" in the array that waste memory and have an adverse impact on performance. Call Compact to redress matters.
- procedure Discard(const Index:String) - Use this method to remove the entry bearing the association label Index from the array. Nothing is done if there is no corresponding entry.
Usage:Create an instance of TAssArr whenever you need to store a sequence of values, or objects, that are associated with a unique descriptive key. To create a two dimensional array simply store individual instances of TAssArr in another instance of TAssArr configured to store pointers. Use TAssArr methods and properties to manage your entries. Remember to free the instance once you are done. If you create a two dimensional association remember to free every instance of TAssArr!
Download