[Gambas-user] basics of dynamic arrays of structures

Tobias Boege taboege at ...626...
Sun Dec 29 15:17:29 CET 2013


On Mon, 23 Dec 2013, Kevin Fishburne wrote:
> 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.
> 

I personally don't use this syntax:

  Plan = New PlanStructure[0]

but looking at the docs, this seems to be valid and equivalent to (which is
the syntax I am fond of):

  Plan = New PlanStructure[](0)
  ' Or just:
  Plan = New PlanStructure[]

You can give an initial size (as shown above) with

  Plan = New PlanStructure[](iInitialSize)

instead of creating an empty array and resizing it immediately afterwards.

The rest seems pretty good. One thing came to my mind as I was looking at
this, tough: if you need to initialise e.g. Plan.Wall and other things after
creating Plan, you could also make at least PlanStructure a class and do all
the initialisations nicely encapsulated in PlanStructure._new(). But that's
really your business.

> 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?
> 

Hmm, seems that I have grown up with these dynamic array syntaxes (so I
don't see any difficulty in learning them)... But we have a tutorial
section; maybe we (whoever this will be :-)) can add a document somewhen but
I can't promise anything. (I just saw that the ArrayOfControls example also
teaches the static way... Maybe I should continue updating the examples as I
wanted to do like a year ago :-S)

Regards,
Tobi

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk




More information about the User mailing list