Q. dim array(any) vs redim array(1 to 1) (SOLVED!!)

New to FreeBASIC? Post your questions here.
SamL
Posts: 25
Joined: Nov 23, 2019 17:30

Q. dim array(any) vs redim array(1 to 1) (SOLVED!!)

Postby SamL » Jul 11, 2020 17:22

I have a sub i call and set up a string array. then I REDIM the array in a loop based on a file size and load different files using the same array.

is it better to use "dim as string array(any) " or "Redim as string array(1 to 1)" ??
what example is correct? or are both ok to use?


example 1:

Code: Select all

sub check_files
   
    dim as string file_array(any) '<----- USING 'ANY'
    dim as ulong file_entries
   
    'open file 1 and get file entries from 1st line
    redim file_array ( 1 to file_entries )
    for i as ulong = 1 to file_entries
        'load file into file_array
    next i
   
    'do some stuff with file 1

    'open file 2 and get file entries from 1st line
    redim file_array ( 1 to file_entries )
    for i as ulong = 1 to file_entries
        'load file into file_array
    next i

    'do some stuff with file 2
end sub


example 2:

Code: Select all

sub check_files
   
    redim as string file_array(1 to 1) '<----- USING 'REDIM'
    dim as ulong file_entries
   
    'open file 1 and get file entries from 1st line
    redim file_array ( 1 to file_entries )
    for i as ulong = 1 to file_entries
        'load file into file_array
    next i

    'do some stuff with file 1

    'open file 2 and get file entries from 1st line
    redim file_array ( 1 to file_entries )
    for i as ulong = 1 to file_entries
        'load file into file_array
    next i

    'do some stuff with file 2
end sub



Thanks every one!! :D

-SamL
Last edited by SamL on Jul 11, 2020 18:29, edited 1 time in total.
MrSwiss
Posts: 3599
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Q. dim array(any) vs redim array(1 to 1)

Postby MrSwiss » Jul 11, 2020 17:48

You'll have to tackle the problem differently, in order to achieve succcess.
Don't pack 'as much as possible' into any one procedure, rather write it in
a way, to be able to call it multiple times.

Code: Select all

Sub FileContent(a() As String)
   ' resize a() as needed
   ' fill it
End Sub

' main
Dim As String sa(Any)      '

FileContent(sa())
' ... other code
' end-main
SamL wrote:is it better to use "dim as string array(any) " or "Redim as string array(1 to 1)" ??
The first is unsized vs. the second is sized to 'one element'.
Since you are going to resize it within the Sub, both can be used.
IMPORTANT: default array in FB is BASE 0, not BASE 1 (aka: array(0 To n)).
SamL
Posts: 25
Joined: Nov 23, 2019 17:30

Re: Q. dim array(any) vs redim array(1 to 1)

Postby SamL » Jul 11, 2020 18:23

ok, great!

-SamL

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 5 guests