[Gambas-user] basics of dynamic arrays of structures

Kevin Fishburne kevinfishburne at ...1887...
Mon Dec 23 07:28:18 CET 2013


On 12/21/2013 04:57 AM, Tobias Boege wrote:
> On Sat, 21 Dec 2013, Kevin Fishburne wrote:
>> On 12/21/2013 12:36 AM, Kevin Fishburne wrote:
>>> I've gotten some flak about how I declare arrays and want to change my
>>> ways. Previously I would do something like:
>>>
>>> Public SomeArray[300,300] as Integer
>>>
>>> I have a case now where I need an array whose number of elements will be
>>> incremented and that will be cleared occasionally. Of course I can't
>>> figure out how to do it. My code looks like this (I stripped it down):
>>>
>>> ---
>>>
>>> Public Struct PlanWallStructure
>>>      Created As Boolean
>>> End Struct
>>>
>>> Public Struct PlanStructure
>>>      Wall As Struct PlanWallStructure
>>> End Struct
>>>
>>> Public Plan[10] As Struct PlanStructure
>>>
>>> ---
>>>
>>> So I need, for example, Plan[0].Wall to be an array where I can do
>>> something like:
>>>
>>> ---
>>>
>>> Plan[0].Wall.Clear
>>> Index = Plan[0].Wall.Count
>>> Plan[0].Wall[Index] = True
>>> Index = Plan[0].Wall.Count
>>> Plan[0].Wall[Plan[0].Wall.Count] = False
>>>
>>> ---
>>>
>>> Is this possible? I've tried every syntax I can think of and looked at
>>> the documentation but get errors every time.
>>>
>> Whoops. Meant for that last bit of code to be:
>>
>> Plan[0].Wall.Clear
>> Index = Plan[0].Wall.Count
>> Plan[0].Wall[Index] = True
>> Index = Plan[0].Wall.Count
>> Plan[0].Wall[Index] = False
>>
>>
>> Same thing basically for the purpose of the example.
>>
> I don't think there is an array in Gambas which lets you use its Count as
> index without raising an "out of bounds" error :-) (and you certainly mean
> Plan[0].Wall[Index]*.Created* = False or else, don't use a Struct.)
>
> Using dynamic arrays, you would do:
>
> ---
> Public Plan As PlanStructure[]
>
> Public Sub _new()
>    Dim iIndex As Integer
>
>    Plan = New PlanStructure[](10) ' Initial size
>    For iIndex = 0 To Plan.Max
>      Plan[iIndex] = New PlanWallStructure
>    Next
> End
> ---
>
> I wrote this out of my head. If it doesn't get you started, just tell me.

I may have figured it out, though please let me know if I'm doing this 
ass-backward (sample project attached). Looks like the Resize array 
method can be used to add elements as needed. I also needed to revise 
how the arrays of structures were declared in the Plan structure as your 
code example suggested.

If my project code is correct, it would be nice to have this somewhere 
in the documentation. I couldn't find a decent explanation of how to 
create and manipulate arrays anywhere in the docs. Maybe there should be 
a tutorial or example wiki along with the regular docs?

-- 
Kevin Fishburne
Eight Virtues
www: http://sales.eightvirtues.com
e-mail: sales at ...1887...
phone: (770) 853-6271

-------------- next part --------------
A non-text attachment was scrubbed...
Name: gb3_test.tar.gz
Type: application/gzip
Size: 6132 bytes
Desc: not available
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20131223/8d3a1034/attachment.gz>


More information about the User mailing list