简体   繁体   中英

Use an array in a user-defined TYPE in QBasic

I'm trying to learn QBasic to program on an Amstrad Alt-286. In one of my program, I use several user-defined types, sometimes TYPE arrays. In some of them, I want to declare an array like this :

TYPE TestType
    dataArray AS STRING * 4 'Since "dataArray AS _BYTE * 4" doesn't work (wrong syntax compiler says).
END TYPE

I then declare my type like this :

DIM customType(2) AS TestType

And as soon as I want to write in my type's dataArray like this :

customType(1).dataArray(2) = 3

The compiler tells me it is an invalid syntax.

Then, how to store an array in a defined TYPE? And how to use it?

There are two issues here. In QB64 you simply can't put arrays inside of user defined types. According to the QB64 Wiki's article on TYPE definitions :

TYPE definitions cannot contain Array variables! Arrays can be DIMensioned as a TYPE definition.

Besides that, your dataArray (declared dataArray AS STRING * 4 ) does not declare an array at all, but rather, declares a 4 character string. That's why you get a syntax error when you try to access elements of dataArray using array syntax. You can declare an array consisting of a custom type, like so:

TYPE TestType
    dataElement AS _BYTE
END TYPE

DIM CustomType(4) AS TestType

CustomType(1).dataElement = 3

This declares a 4 element array of TYPE TestType, each element containing a variable of TYPE _BYTE. That's about as close as you can get to what you're trying to do. Good luck!

The code you want is something like this:

Although you CANNOT do this in QB1.1, QB4.5, or QB64, you CAN do this in supersets of the BASIC dialect known as QB7.1(BC7/PDS), and VBDOS(v1.00):

TYPE testtype
    dataArray(4) AS INTEGER
END TYPE
DIM customtype(10) AS testtype
customtype(1).dataArray(2) = 3

Otherwise you could compress the variables as such:

TYPE testtype
    dataArray AS STRING * 8
END TYPE
DIM customtype(10) AS testtype
A = 10: B = 12: C = 14: D = 16
' compress variables into structure
element1$ = MKI$(A) + MKI$(B) + MKI$(C) + MKI$(D)
customtype(1).dataArray = element1$ ' store
' extract variables from structure
element2$ = customtype(1).dataArray ' get
E = CVI(MID$(element2$, 1, 2))
F = CVI(MID$(element2$, 3, 2))
G = CVI(MID$(element2$, 5, 2))
H = CVI(MID$(element2$, 7, 2))
PRINT E, F, G, H

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM