Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Variable length type declarations
#1
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.
Reply
#2
(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 !     
Reply
#3
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.
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#4
(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 !.
Reply
#5
Here's an example where I did this very type thing ages ago -- https://qb64phoenix.com/forum/showthread.php?tid=142
Reply
#6
(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 !!!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  QB64PE Excel-type spreadsheet supporting formulas and QB64PE macros? madscijr 33 1,347 01-30-2026, 12:28 AM
Last Post: madscijr
  Determining if variable or static string is passed to a Sub dano 9 1,168 06-20-2025, 06:31 PM
Last Post: CookieOscar
  Most efficient way to build a big variable length string? mdijkens 9 1,841 01-17-2025, 11:36 PM
Last Post: ahenry3068
  Huge array of variable length strings mdijkens 9 1,787 10-17-2024, 02:01 PM
Last Post: mdijkens
  TYPE and CONST within SUB/FUNCTION TerryRitchie 9 1,904 07-11-2023, 01:22 AM
Last Post: bplus

Forum Jump:


Users browsing this thread: 1 Guest(s)