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
(an answered question)
(zero-based)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
There are two kinds of arrays in [[Gambas]]. One is well-documented, the other barely-documented.
+
There are two kinds of arrays in [[Gambas]]. One is well-documented, the other barely-documented. Both are zero-based by default.
 
==Native Arrays==
 
==Native Arrays==
 
A "native array" is an array of other variable types; see [http://gambasdoc.org/help/cat/arraydecl Array Declaration] for documentation and examples.
 
A "native array" is an array of other variable types; see [http://gambasdoc.org/help/cat/arraydecl Array Declaration] for documentation and examples.

Latest revision as of 22:52, 9 May 2015

There are two kinds of arrays in Gambas. One is well-documented, the other barely-documented. Both are zero-based by default.

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>