This is just a short series to help VB users code in REALbasic more
effectively. Small lessons, tidbits, and caveats.
Today's topic: Arrays
* VB 6 allows users to dimension an array with negative lower bounds
(LBound(array) < 0). Neither RB, nor VB .Net allow this. In addition,
all arrays start with a lower bound of 0; you can only change the upper
bound(s) with the "ReDim" keyword
* Also, when using 'Redim', RB does not have the 'Preserve' keyword
that VB 6 has, nor does it need it; RB arrays are 'ReDim Preserve' by
default. If you wish to erase the contents of a REALbasic array, Redim
it for -1 elements, then Redim to the desired size.
* To create an array with no initial size, Dim it for a size of -1.
This works for local variables, as well as properties (of classes,
windows, and modules.)
* Watch out for arrays of object references! Just as with a local
variable typed as 'reference-to-some-class', the individual elements of
an array also need to be 'New'ed into existence before use! Likewise,
if the array goes out of scope, it's good practice to 'nil the elements
before removal, especially if there's any risk they create a circular
reference!
* As with VB, RB arrays can be passed into functions and subs. RB
(5.0+) also allows you to return an array from a function call; I don't
believe this is permitted in VB 6.
* Arrays passed into methods (a fancy word for a function or sub) are
ByRef by default; any contents of the array can be modified by the
called method, so watch out!
* RB arrays support useful methods, like "Insert()", "Append()",
"Remove()", and others that are better described in the RB Language
Reference (online, or print version). As of RB 5 (I forget which
version), arrays also support a "Sort()" method and - I believe - a
"Shuffle()" method. They also have a "Pop()" method which allows you to
use an RB array to implement a stack or queue data structure.
* Whenever possible (and reasonable), wrap an array - and the
operations you plan to perform on it - in a class. This makes it easier
to change the underlying implementation details without a major rewrite
of the code that used the array originally. This also makes it easier
to 'control' the contents of the array when passing them into methods;
the mechanism of this being the wrapper class' public API.
Watch for more exciting (?) and informative tidbits here one week
from now. Stay tuned.
William H Squires Jr
wsquires at satx dot rr dot com dot nospam <- remove the .nospam
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>
|