QB64 Phoenix Edition
Variable length type declarations - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: Chatting and Socializing (https://qb64phoenix.com/forum/forumdisplay.php?fid=11)
+--- Forum: General Discussion (https://qb64phoenix.com/forum/forumdisplay.php?fid=2)
+--- Thread: Variable length type declarations (/showthread.php?tid=3850)



Variable length type declarations - dano - 08-06-2025

Is there any way that a Type declaration can be variable?

For example, the following code works, but the length is static.

Type ContactRecord
    Name As String * 30 ' Fixed-length string for name
    Phone As String * 15 ' Fixed-length string for phone number
End Type


If I were to enter something like (and yes, I realize this is invalid code...):

name.fieldlen% = 30
phone.fieldlen% = 15
Type ContactRecord
    Name As String * name.fieldlen% ' Fixed-length string for name
    Phone As String * phone.fieldlen% ' Fixed-length string for phone number
End Type

I get an error "Invalid number/constrant after STRING * type"


What I am trying to achieve is the ability to create a random access file, but be able to set the field length dynamically.  This would allow me to create basic databases on the fly without having to specify the records statically.  The database can then be created from information gleaned from outside sources and the database's record sizes are determined from within the running code.

One thought was to set the record length to the max that I would expect to use so that is is a 'max-size fits all', but there will be a lot of records and this will make the file sizes unnecessarily large.


RE: Variable length type declarations - ahenry3068 - 08-06-2025

(08-06-2025, 06:44 PM)dano Wrote: Is there any way that a Type declaration can be variable?

For example, the following code works, but the length is static.

Type ContactRecord
    Name As String * 30 ' Fixed-length string for name
    Phone As String * 15 ' Fixed-length string for phone number
End Type


If I were to enter something like (and yes, I realize this is invalid code...):

name.fieldlen% = 30
phone.fieldlen% = 15
Type ContactRecord
    Name As String * name.fieldlen% ' Fixed-length string for name
    Phone As String * phone.fieldlen% ' Fixed-length string for phone number
End Type

I get an error "Invalid number/constrant after STRING * type"


What I am trying to achieve is the ability to create a random access file, but be able to set the field length dynamically.  This would allow me to create basic databases on the fly without having to specify the records statically.  The database can then be created from information gleaned from outside sources and the database's record sizes are determined from within the running code.

One thought was to set the record length to the max that I would expect to use so that is is a 'max-size fits all', but there will be a lot of records and this will make the file sizes unnecessarily large.


I don't think you'll ever see variable length TYPE's.   One way you could accomplish the same end (with slightly More coding) Is to have a Data Length Field in you type and have that data in a **REDIMMED** array that you PUT to the file immedietly after the TYPE variable.    You can REDIM an Array as many times as you want to and an entire Array can be written to (and read from) with a Single PUT, GET instruction !     



RE: Variable length type declarations - bplus - 08-06-2025

If you set a field to a variable length you will need another file to track the field lengths, seems like allot of mickey mouse.

Typically fields of string type are set to a reasonable maximum.


RE: Variable length type declarations - ahenry3068 - 08-06-2025

(08-06-2025, 08:20 PM)bplus Wrote: If you set a field to a variable length you will need another file to track the field lengths, seems like allot of mickey mouse.

Typically fields of string type are set to a reasonable maximum.

   You wouldn't need a separate  file but you would need a fixed length RECORD HEADER with a field for the stored data size.    A seperate file with Record locations would probably be necessary for decent search times if the file grows large though !.


RE: Variable length type declarations - SMcNeill - 08-06-2025

Here's an example where I did this very type thing ages ago -- https://qb64phoenix.com/forum/showthread.php?tid=142


RE: Variable length type declarations - dano - 08-06-2025

(08-06-2025, 09:20 PM)SMcNeill Wrote: Here's an example where I did this very type thing ages ago -- https://qb64phoenix.com/forum/showthread.php?tid=142

I do believe that will work to achieve what I am looking to do - once again, thank you Steve !!!!