Page 1 of 1

Is there a way to pass different Array types to the same SUB

Posted: Wed Jan 06, 2010 12:58 am
by burger2227
The SUBs can be used to quickly save and restore array data in QB64.
Is there a way to send different array type parameters for this in QB?
This can be used with Integer, Single, Double, Long, fixed length Strings or TYPE arrays:


Code: Select all

DECLARE SUB LoadArray (Array() AS ANY, filename$)
DECLARE SUB SaveArray (Array() AS ANY, filename$)

CLS
DIM Image(1 to 1000) AS String * 1 ' try fixed strings, single, integer, double, long, TYPE arrays

RANDOMIZE TIMER
FOR i = 1 TO 1000
   Image(i) = CHR$(int(RND * 100 + 33)) ' change this calculation for the type used
NEXT
PRINT Image(10), Image(50), Image(100)

filename$ = "TestBSV.BSV"

SaveArray Image(), filename$

ERASE Image
PRINT Image(10), Image(50), Image(100)

LoadArray Image(), filename$

PRINT Image(10), Image(50), Image(100)

sleep

SUB LoadArray (Array(), filename$)
DEF SEG = VARSEG(Array(0))
   BLOAD filename$, VARPTR(Array(LBOUND(Array)))
DEF SEG
END SUB

SUB SaveArray (Array(), filename$)
LB% = LBOUND(Array)
bytes% = LEN(Array(LB%))
filesize& = (UBOUND(Array) - LB%) + 1) * bytes% 
DEF SEG = VARSEG(Array(0))
   BSAVE filename$, VARPTR(Array(LB)), filesize&
DEF SEG
END SUB
This demo routine works in QB64, but not QB. QB Help does not say much about ANY.

Thanks,

Ted

'DIM SHARED'

Posted: Sat Jan 09, 2010 9:28 pm
by OPRESION
UNTIL TODAY I'AM JUST USING THE QB. BUT WHEN I WANT TO DO SOMETHING LIKE
THAT I DECLARE THE ARRAYS AS DIM SHARED AT THE BEGINNING OF THE SOURCE.

Posted: Sat Jan 09, 2010 9:40 pm
by burger2227
If you pass the Array, you don't need SHARED though. I was hoping ANY would allow multiple array types, but it don't.

Since I posted the code above, I have found out that QB64 does not currently Type check arrays. So the code is reall just exploiting QB64 weaknesses at this point.

But QB64 can already do what I wanted to do without BSAVE or BLOAD!

Code: Select all

DIM myarray(1000) AS INTEGER
OPEN "myfile.ext" FOR BINARY AS #1
PUT #1, ,myarray()  ' PUT array all at once....FAST!
CLOSE #1
It should be noted that only numerical or FIXED LENGTH string arrays will work!

Ted