If it ain’t broke…fix it Part 1 of many

Since this is my first post, I wonder if I should take a moment and introduce myself. But, then, it might be easier to let the author profile and my posts themselves do the talking…

One of the clichés that has guided developers through the decades has been “if it ain’t broke don’t fix it.” But when a system is based on good development methodologies, preemptive changes can have significant benefits. This post discusses one such preemptive measure VB(A) developers should consider. Many, if not most, VB(A) developers use 1-based arrays or even arrays declared with bounds that are highly relevant to their application. For example, someone who has to create an array for the years 2001 to 2010 might use

.Net, on the other hand, requires all arrays to be zero-based. Further, since the lower bound must be zero, the array declaration does not allow the specification of a lower bound. Essentially, the syntax

declares an array of 10 elements from a lower bound of zero to an upper bound of 9. Clearly, when it comes to declaring arrays .Net supports only one of the many flavors, if you will, that VB(A) supports. To the extent that one plans to migrate to VB.Net or develop in both VB(A) and VB.Net, adopting .Net protocols now will help minimize transition difficulties. In VBA, one can use

to accomplish essentially what .Net requires.

Is that all there is to making a preemptive change to zero-based arrays? No, not really. One of the first things to consider is that one needs to think differently. Most of us grew up learning to count starting from 1. Now, of course, we need to start counting from zero. The first element of an array is at index zero and not at index 1.

In addition, when it comes to changing existing code, there are other issues to consider. If the developer has code like

it will now fail.

On the other hand, if the developer had practiced safe programming all along, the code would have used references to the array’s bounds in the first place and would require no change!

In the next post, we will look at the handling the interactions between VB(A)/.Net zero-based arrays and Excel’s 1-based collections.