=====Arrays of structures (LBB)=====
//by Richard Russell, December 2014//\\ \\ One of the language extensions provided by [[http://www.lbbooster.com/|LB Booster]] is native **arrays of structures**. These can be useful for structured data storage within a program, or when calling a Windows API function (for example [[http://msdn.microsoft.com/en-us/library/windows/desktop/dd162814.aspx|Polygon]]) which requires an array of structures as one of its parameters.\\ \\ The syntax for declaring an array of structures is very similar to that for declaring a single structure; the only difference is the addition of the dimension(s) in parentheses:\\
STRUCT sname(dims), member1 AS type, member2 AS type...
Arrays of structures may have any number of dimensions, limited only by available memory. As with ordinary numeric and string arrays the index value runs from 0 to the specified maximum dimension.\\ \\ Accessing an individual member, in order to change or read its value, again uses a syntax very similar to that used for a single (scalar) structure:\\
sname(index).member.struct = newvalue
PRINT sname(index).member.struct
\\ To pass the entire array of structures as a parameter to an API function use the following syntax:\\
sname() AS struct
Note the use of a pair of parentheses with nothing in between.\\ \\ Here are a couple of examples of the use of arrays of structures. Firstly an adaptation of the program by Dennis McKinney at the [[http://lbpe.wikispaces.com/ArraysAndStructs|Lliberty BASIC Programmer's Encyclopedia]]:\\
'define array of 100 structures
struct test(99),_
a as char[20],_
b as long,_
c as char[20]
'for example purposes, fill the whole array of structures
for i = 0 to 99
'put some data into the struct
test(i).a.struct = "Carol - " + str$( i)
test(i).b.struct = i
test(i).c.struct = "Andy - " + str$( i)
next i
'for example, read all of the structures
for i = 0 to 99
A$ = test(i).a.struct
B = test(i).b.struct
C$ = test(i).c.struct
print A$ + " " + str$(B) + " " + C$
next i
'To retrieve the third element from the 50th structure:
C$ = test(49).c.struct
print C$
'To change the value of the third element of the 50th structure:
test(49).c.struct = "Changed" 'change one or more values
'just for example
C$ = test(49).c.struct
print C$
[quit]
end
\\ Secondly, an example of passing an array of structures to an API function:\\
nomainwin
open "Structure array" for graphics as #w
#w "trapclose [quit]"
hw = hwnd(#w)
calldll #user32, "GetDC", hw as ulong, hdc as ulong
npoints = 3
struct points(npoints-1), x as long, y as long
for i = 0 to npoints-1
points(i).x.struct = 120 + 120 * cos(2*i)
points(i).y.struct = 150 + 120 * sin(2*i)
next
calldll #gdi32, "Polygon", hdc as ulong, _
points() as struct, npoints as long, r as long
wait
[quit]
close #w
end