realbasic-nug
[Top] [All Lists]

Re: Lessons for VB users

To: REALbasic NUG <realbasic-nug at lists dot realsoftware dot com>
Subject: Re: Lessons for VB users
From: "Joseph J. Strout" <joe at realsoftware dot com>
Date: Thu, 31 Mar 2005 08:48:39 -0600
Delivered-to: realbasic-nug at lists dot realsoftware dot com
References: <ee222e3e0051b41e915448c9dbd993ba at satx dot rr dot com>
At 9:18 PM -0600 3/30/05, William Squires wrote:

* 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!

This is true...

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!

...but this is not. Elements of an array are automatically set to nil (or the equivalent) when an array goes out of scope or is otherwise destroyed. Doing this manually beforehand has no benefit whatsoever.

* 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!

This is also not true. Just as with object references (or any other type), array references are passed ByVal by default. You could pass one ByRef, which would allow you to assign a different array to the variable passed in by the caller, but this is rarely needed.

However, arrays (like most objects) are *mutable*, which means that even when the reference is passed in ByVal, you can mutate the array so referred to, and this of course is the very same array that was passed in.

* 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.

Yep, all true.

* Whenever possible (and reasonable), wrap an array - and the operations you plan to perform on it - in a class.

"Whenever possible" might be a bit strong -- I've only needed to do this a couple of times in all the years I've used RB. And it's much more rarely needed now that you can return arrays from function calls. But it's certainly a valid technique to use when you need it.

Best,
- Joe

--
,------------------------------------------------------------------.
|    Joseph J. Strout           REAL Software, Inc.                |
|    joe at realsoftware dot com       http://www.realsoftware.com        |
`------------------------------------------------------------------'
_______________________________________________
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>

<Prev in Thread] Current Thread [Next in Thread>