Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Pascal Misc > Re: Using a var...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 6 Topic 709 of 811
Post > Topic >>

Re: Using a variable within a record

by Wolf Behrenhoff <NoSpamPleaseButThisIsValid3@[EMAIL PROTECTED] > Jan 18, 2007 at 02:21 AM

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
 




 6 Posts in Topic:
Using a variable within a record
Dennis-Bendert Schramm &l  2007-01-18 01:48:14 
Re: Using a variable within a record
Alan <alan@[EMAIL PROT  2007-01-18 02:00:09 
Re: Using a variable within a record
Wolf Behrenhoff <NoSpa  2007-01-18 02:21:49 
Re: Using a variable within a record
Dennis-Bendert Schramm &l  2007-01-18 06:59:10 
Re: Using a variable within a record
Waldek Hebisch <hebisc  2007-01-19 10:39:54 
Re: Using a variable within a record
"JP2R" <jp-d  2007-01-19 04:35:29 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 0:47:04 CDT 2008.