[Gambas-user] basics of dynamic arrays of structures

Tobias Boege taboege at ...626...
Sat Dec 21 10:57:28 CET 2013


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.

Regards,
Tobi




More information about the User mailing list