简体   繁体   English

在QBasic中的用户定义的TYPE中使用数组

[英]Use an array in a user-defined TYPE in QBasic

I'm trying to learn QBasic to program on an Amstrad Alt-286. 我正在尝试学习QBasic,以便在Amstrad Alt-286上进行编程。 In one of my program, I use several user-defined types, sometimes TYPE arrays. 在我的程序之一中,我使用了几种用户定义的类型,有时是TYPE数组。 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 : 而且,我想这样写我的类型的dataArray时:

customType(1).dataArray(2) = 3

The compiler tells me it is an invalid syntax. 编译器告诉我这是无效的语法。

Then, how to store an array in a defined TYPE? 然后,如何在定义的TYPE中存储数组? And how to use it? 以及如何使用它?

There are two issues here. 这里有两个问题。 In QB64 you simply can't put arrays inside of user defined types. 在QB64中,您根本无法将数组放在用户定义的类型内。 According to the QB64 Wiki's article on TYPE definitions : 根据QB64 Wiki 关于TYPE定义文章

TYPE definitions cannot contain Array variables! TYPE定义不能包含Array变量! Arrays can be DIMensioned as a TYPE definition. 可以将数组定义为TYPE定义。

Besides that, your dataArray (declared dataArray AS STRING * 4 ) does not declare an array at all, but rather, declares a 4 character string. 除此之外,您的dataArray(声明为dataArray AS STRING * 4 )根本不声明数组,而是声明4字符串。 That's why you get a syntax error when you try to access elements of dataArray using array syntax. 这就是为什么当您尝试使用数组语法访问dataArray的元素时出现语法错误的原因。 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. 这声明了一个4个元素的TYPE TestType数组,每个元素包含一个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): 尽管您不能在QB1.1,QB4.5或QB64中执行此操作,但是您可以在称为QB7.1(BC7 / PDS)和VBDOS(v1.00)的BASIC方言的超集中执行此操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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