Search code examples
arraysqbasic

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?


Solution

  • 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!