structure array within a structure

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
mikefromca
Coder
Posts: 41
Joined: Wed Oct 16, 2019 11:28 am

structure array within a structure

Post by mikefromca »

Lately, I've been using the TYPE command to make a structure for use with my assembly code.

I'm making a routine in assembly that will extract multiple name-value pairs back to Qbasic but I'd like to receive it in a special type structure but I don't know if it can be doable in Qbasic. I'll explain in Qbasic code what I'm trying to do:

Code: Select all

Type employee
    FirstName as string * 30
    LastName as string * 30
    Age as integer
End Type

Type product
    ProductName as string * 30
    ProductPrice as Double
End Type

Type maindata
    StoreName as string * 30
    employees(10) as employee
    products(100) as product
End Type

Dim datatoasm as maindata

'setup name
datatoasm.StoreName="Valu Mart"

'setup first employee
datatoasm.employees(1).FirstName="John"
datatoasm.employees(1).LastName="Smith"
datatoasm.employees(1).Age=66

'setup 2nd employee
datatoasm.employees(2).FirstName="Jack"
datatoasm.employees(2).LastName="Daniels"
datatoasm.employees(2).Age=77
I hope my code explains it better. But When I try to execute it in Qbasic, it complains at the code here:

Code: Select all

    employees(10) as employee
    products(100) as product
and highlights the word "as" and shows as an error "Expected end-of-statement".

Is there a way around this without me having to do this...

Code: Select all

    employee1 as employee
    employee2 as employee
    ...
    employee10 as employee
    product1 as product
    product2 as product
    product3 as product
    ...
    product99 as product    
    product100 as product    
MikeHawk
Veteran
Posts: 61
Joined: Sun Jul 08, 2018 11:23 am

Re: structure array within a structure

Post by MikeHawk »

QBasic doesn't support namespaces, which is what I think you're trying to do with "maindata." I'm afraid you'll have to create a set of shared variables instead (see this topic for shared variables.) It's dirty but there's no other way that I'm aware of:

Code: Select all

DIM SHARED mdStoreName AS STRING * 30
DIM SHARED mdEmployees(10) AS employee
DIM SHARED mdProducts(100) AS product
If you need to have variable-length content or arrays in a user-defined type for some other purpose, I'd recommend an indexing system for the type and a shared array for its content, something like:

Code: Select all

TYPE vec2
	x AS INTEGER
	y AS INTEGER
END TYPE

TYPE shape
	numPts AS INTEGER
	firstPt AS INTEGER
END TYPE

DIM SHARED ptList(31) AS vec2
DIM shp AS shape

shp.numPts = 2
shp.firstPt = 0

ptList(0).x = 10  ' 1st point, index 0
ptList(0).y = 15
ptList(1).x = 20  ' 2nd point, index 1
ptList(1).y = 100
Or you could tell QB to free some memory, reserve it again via a DOS interrupt, and craft a custom routine to write and read data to/from memory (since pointers are not exactly available in QB;) this way you could reserve as much or as little memory as you need for your types and keep track of it via its segment and offset (would be static since QB cannot mess with it.) The obvious downside is that you'll have to use your custom routines to access that data and will have to create temporary structures to display or manipulate its content (fill structure from memory, manipulate the data, write the structure back to memory.) For simplicity's sake I'd rather go for the indexed system...
mikefromca
Coder
Posts: 41
Joined: Wed Oct 16, 2019 11:28 am

Re: structure array within a structure

Post by mikefromca »

ok thanks
Post Reply