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

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

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

Post 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
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
OPRESION
Veteran
Posts: 61
Joined: Tue Jan 16, 2007 4:15 am
Location: Mexico

'DIM SHARED'

Post 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.
MY PAGE: http://Qbasic.phatcode.net" target="_blank
(I ONLY USE WINDOWS 98SE YET, BELIEVE IT OR NOT)
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post 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
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Post Reply