Array of objects??
RogerInHawaii
Posts: 87
I'm writing some Spin code. What I'd really like is an array of C++ -like objects.
That is I'd like to have each element within the array to consist of two or more items, say a byte value, a long value, and a word value (or even something more complex). But there doesn't seem to be any way to specify such an array in Spin.
The best I've come up with is something like:
[noparse][[/noparse]CODE]
VAR
··BYTE MyByte0
··LONG MyLong0
· WORD MyWord0
· BYTE MyByte1
··LONG MyLong1
··WORD MyWord1
· BYTE MyByte2
··LONG MyLong2
··WORD MyWord2
· LONG MyArray[noparse][[/noparse]3]
PUB Main
· ' Initialize the array elements to point at the "objects"
· MyArray[noparse][[/noparse]0] := @@MyByte0
· MyArray[noparse][[/noparse]1] := @@MyByte1
· MyArray[noparse][[/noparse]0] := @@MyByte2
[noparse][[/noparse]/CODE]
And then I can access MyArray[noparse][[/noparse]i] to get a pointer to the desired "object" and from there access
the individual elements of the "object".
Is that a reasonable way to do it? Is there a better way?
Ideally I'd like to be able to very simply specify at compile time how many·"objects" are in the array without havingto explicity·specify each individual object and explicitly·initialize the pointers. That is, ideally I'd like something more akin to C++ style objects. I know that's asking a lot from Spin. [noparse]:)[/noparse]
PS: What happened to the CODE designators in the New Post window? They're gone, and it doesn't seem to recognize when I excplicitly enter them myself.
That is I'd like to have each element within the array to consist of two or more items, say a byte value, a long value, and a word value (or even something more complex). But there doesn't seem to be any way to specify such an array in Spin.
The best I've come up with is something like:
[noparse][[/noparse]CODE]
VAR
··BYTE MyByte0
··LONG MyLong0
· WORD MyWord0
· BYTE MyByte1
··LONG MyLong1
··WORD MyWord1
· BYTE MyByte2
··LONG MyLong2
··WORD MyWord2
· LONG MyArray[noparse][[/noparse]3]
PUB Main
· ' Initialize the array elements to point at the "objects"
· MyArray[noparse][[/noparse]0] := @@MyByte0
· MyArray[noparse][[/noparse]1] := @@MyByte1
· MyArray[noparse][[/noparse]0] := @@MyByte2
[noparse][[/noparse]/CODE]
And then I can access MyArray[noparse][[/noparse]i] to get a pointer to the desired "object" and from there access
the individual elements of the "object".
Is that a reasonable way to do it? Is there a better way?
Ideally I'd like to be able to very simply specify at compile time how many·"objects" are in the array without havingto explicity·specify each individual object and explicitly·initialize the pointers. That is, ideally I'd like something more akin to C++ style objects. I know that's asking a lot from Spin. [noparse]:)[/noparse]
PS: What happened to the CODE designators in the New Post window? They're gone, and it doesn't seem to recognize when I excplicitly enter them myself.
Comments