View Single Post
  #20   Report Post  
Posted to microsoft.public.excel.programming
Chip Pearson Chip Pearson is offline
external usenet poster
 
Posts: 7,247
Default "Listing Contents of Sub-Folders"

In either of your examples, it seems to me that you have not omitted
the lower bound in the array declaration, have you?


Each pair of array declarations was supposed to illustrate the difference
between omitting and including the lower bound of the array declaration. The
first Dim statement in each pair omitted the lower bound and the second Dim
statement in each pair showed how the array would need to be declared to
match a Option Base statement. For example, the in pair of declarations

Dim Arr(5)
Dim Arr(1 To 5) ' five elements in the array

the first illustrates not using a lower bound and the second shows how VBA
actually allocates the array if Option Base 1 is in effect. The second pair
of declarations,

Dim Arr(5)
Dim Arr(0 To 5) ' six elements in the array

illustrates again an array without a specified lower bound and how VBA will
allocated the array if Option Base is 0. The point I was trying to make was
that the declaration Dim Arr(5) will allocate two differently sized arrays
depending on the Option Base statement,.

Dim Arr(5) declares an array without a specified lower bound. If you have
Option Base 1, this array declaration is the same as if you used Dim Arr(1
To 5). This array has five elements. If you have Option Base 0 or no Option
Base statement at all, that same array declaration, Dim Arr(5), is the same
as the declaration Dim Arr(0 To 5). This array has six elements.

A potential problem arises when you copy a procedure from one module to
another. If the code is copied from a module using Option Base 1 to a module
using Option Base 0 or no Option Base statement at all, that array
declaration, which originally had 5 element now declares an array with 6
elements. The presence of the extra element may cause the logic of the
procedure to behave in an unintended way.

As I wrote before, I think it is very bad programming practice to omit the
lower bound. In my opinion, all arrays that have bounds specified in the Dim
statement should have both the lower and upper bound specified. I would
never use Dim Arr(5). Instead, I would always use Dim Arr(0 to 5) or Dim
Arr(1 To 5). I never use Option Base at all. It is worth noting that not all
arrays adhere the the Option Base statement. For example, the array created
by the Split function always has a lower bound of 0, regardless of the
Option Base statement.

So, I don't see the usefulness of the Option Base 1 or Option Base 0.


Not only is it not useful in any meaningful way, it is an invitation to bugs
if you are a proponent of code reuse.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)


"dan dungan" wrote in message
oups.com...
Hi Chip,

Option Base 1 sets the default lower bound of arrays. With Option Base 1,
the following array declarations are the same.
Dim Arr(5)
Dim Arr(1 To 5) ' five elements in the array

With Option Base 0, the following array declarations are the same.
Dim Arr(5)
Dim Arr(0 To 5) ' six elements in the array

Personally, I think it is bad programming practice to omit the lower
bound
in an array declaration.


In either of your examples, it seems to me that you have not omitted
the lower bound in the array declaration, have you?

So, I don't see the usefulness of the Option Base 1 or Option Base 0.

Thanks for your comments and help.

Dan