Alan schrieb:
> Dennis-Bendert Schramm wrote:
>
>> I guess, you will understand what I want to do: I'd like to use
>> a value stored in word_itemNumber for defining the size of an
>> array within the very same Record.
>>
>> But it won't compile
>
> You cannot use a variable for an array range; it must be a
> constant. If you must use a variable, you could akways use a
> Dynamic Array:
>
>
> type
> MenuRec = RECORD
> word_menuItem : word;
> word_itemNumber : word;
> p_itemArray : array of pchar; { declares a dynamic array }
> end;
>
>
> Somewhere in your code you would setup the dynamic array like so:
>
> SetLength(recvar.p_itemArray,word_itemNumber);
>
> From here you can use the array however you wish, but remember that
> a dynamic array is zero-based (i.e. the first element of the array
> is p_itemArray[0]).
But Dennis, please remember that every time you change word_itemNumber,
you must also set the length of the array because otherwise the array
and word_itemNumber become inconsistent.
That is why my advice is to use a class instead of a record, make
word_itemNumber private and implement getter and setter functions which
also modify the dynamic array.
Anyway, maybe you do not want to reinvent the wheel :-)
As you are using FPC, there is TList (in unit cl*****) which probably is
what you're looking for.
Wolf


|