Difference between revisions of "Gambas/syntax/variables/arrays"

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
< Gambas‎ | syntax‎ | variables
Jump to navigation Jump to search
(since I spent some time figuring this out...)
 
(an answered question)
Line 8: Line 8:
  
 
Once declared, your descendant class will have all the methods and functions defined in the [http://gambasdoc.org/help/comp/gb/array?v3 Array] class.
 
Once declared, your descendant class will have all the methods and functions defined in the [http://gambasdoc.org/help/comp/gb/array?v3 Array] class.
 +
==Answers==
 +
* '''Q:''' Why does this code cause an "Out of Bounds" error?
 +
<gambas>
 +
  Private arChain As New ClsFolder[]
 +
  oRoot = new ClsFolder
 +
  arChain[0] = oRoot
 +
</gambas>
 +
:* '''A:''' Dynamic arrays don't expand automatically; you have to use the .Add() method -- which, fortunately, is automatically set to accept a parameter of the correct type:
 +
<gambas>
 +
  arChain.Add(oRoot)
 +
</gambas>

Revision as of 18:44, 24 March 2015

There are two kinds of arrays in Gambas. One is well-documented, the other barely-documented.

Native Arrays

A "native array" is an array of other variable types; see Array Declaration for documentation and examples.

Array Class

The Array class is an abstract class, which is to say that you cannot use it directly; you have to create a descendant class which implements it and has a public _new() procedure. (Classes have a public _new() procedure by default, so you don't actually have to declare this.)

To inherit from the Array class, simply put "inherits Array" in the class before declaring any functions or procedures. (Actually, I'm not sure if it has to be first, but it seems like good form to have this near the top.)

Once declared, your descendant class will have all the methods and functions defined in the Array class.

Answers

  • Q: Why does this code cause an "Out of Bounds" error?

<gambas>

 Private arChain As New ClsFolder[]
 oRoot = new ClsFolder
 arChain[0] = oRoot

</gambas>

  • A: Dynamic arrays don't expand automatically; you have to use the .Add() method -- which, fortunately, is automatically set to accept a parameter of the correct type:

<gambas>

 arChain.Add(oRoot)

</gambas>