Bug(?): Dim array

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
In Global:

Dim Cards(52) As Int

In Activity_Create:

Cards(52) = someNumber

Error when app is compiled-run:

java.lang.ArrayIndexOutOf BoundsException

How can 52 be an out-of-bounds index when I Dimmed it to 52?
 

stevel05

Expert
Licensed User
Longtime User
As TOB suggested when you dimension an array of size 52, it actually has 52 elements. It's just that the first one is element 0. So 0 to 51 = 52 elements.

Steve
 

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
As TOB suggested when you dimension an array of size 52, it actually has 52 elements. It's just that the first one is element 0. So 0 to 51 = 52 elements.

In any version of BASIC I've ever used, 52 is the last index number, not the number of elements, so Dim A(52) is 0-52 in other BASICs. In fact, I am in the process of converting a Visual Basic program and its got a ton of arrays dimensioned, all to the last index number.

Anyway, it just means that I'll have to change all those Dim's by upping their values by one.
 

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
Well, in almost any other languages an array is zero-based and the dimension indicates number of elements not he last one. I'm also using PowerBasic for Windows and it's just the same thing there.

Well, I guess that on the B4A home page, they should change "Basic4android language is similar to Visual Basic" to "...PowerBasic". <grin> It seems like 99% of B4A's syntax and way of doing things is different from VB.
 

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
It may be different to VB6 but it is similar in many ways (including array declarations!) to VB.NET which has been around for nine years now.

VB.NET hardly qualifies as the same language as the original Visual Basic 1-6, which is why a huge number of VB6 programmers (including me) never made the change. Maybe the B4A home page should change from "similar to Visual Basic" to "similar to VB.NET" (seriously).
 
Top