forums

TNT Basic Forums > Programming Q&A
Can I resize an Array without it being emptied?
< Last Thread     Next Thread >
Author
Thread        Post A Reply

03-14-2006 08:48

Posted by:
Jason Anderson

Location:
Doylestown, PA

Click Here to Email Jason Anderson   Find more posts by Jason Anderson

It seems if I Dim an array that's already full of data the entire array gets emptied.

How can I resize an array (Make it bigger or smaller) without losing all my data??

Is there a way? And if not, why not? I need a Redim that can expand an array without destroying the data.

Dumping the data into a text file is not an option as it would slow things down a lot at the pace my program runs.

03-14-2006 23:10

Posted by:
someone

Location:
Quebec ( Canada )

Click Here to Email someone   Find more posts by someone

Unless this is implemented in TNT, you could make a second array, then copy all your data to the second array, then resize the first array and copy back things into it. It's a bit long, i know! but usually arrays are not resized.

Though implementing Vectors/Lists/etc. to TNT could be great (a vector is like an array without any defined length. You can add things into it and remove them whenever you wish)

03-15-2006 01:39

Posted by:
Jason Anderson

Location:
Doylestown, PA

Click Here to Email Jason Anderson   Find more posts by Jason Anderson

I would do that, but I have a LOT of arrays. What I really need is Classes. Why doesn't TNT have Classes?

03-15-2006 07:37

Posted by:
allnodcoms

Location:
hertfordshire (England)

Click Here to Email allnodcoms   Find more posts by allnodcoms

BASIC tends not to have 'Objects'...

TNT is an 'Old Skool' BASIC inspired by AMOS on the AMIGA, RB has classes as its primary function is GUI elements, and it's much more dependant on an event driven model. If you want to look in the feature request list on Sourceforge you'll see I've listed memory management functions, which would solve your array headaches and allow for the creation of 'pseudo classes' or 'structures'. You could allocate a 'memory block' of the correct size for your object, you could access it at the short, word or long (1, 2 or 4 byte) level and treat it as a C style 'struct'. You would access this memory via a 'pointer' which you could either hold in an array, or (for a more dynamic structure) you could make the last member of your object a pointer to the next item. This is a basic 'linked list'.
The reason why your arrays are cleared when 'resizing' is that it is not always possible to dynamically change the allocation size. A 'redim' command works in the way suggested by 'someone' (Jae?), it allocates a new block and sets the pointer (or address) of the old variable to this new space, then it copies the old data to the new array and zaps the original. It's the only way to be sure to get the correct size in a continuous block.

Hope this helps...

Danny (nods)

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.