Brackets for arrays


#1

In my Basic code sometimes when I use round brackets (parentheses) for array access (eg. MyArray(2)) I will get an error saying MyArray is not a function. The only work around I’ve found is to use the Java format square brackets (eg. MyArray[2]) - is this normal?


#2

JavaScript uses square brackets for arrays. BASIC can use either round or square.

The BASIC to JavaScript Translator will translate BASIC’s round brackets to square ones if it can figure out that you mean an array and not a function call. It does that by looking for a Dim myVar[] statement which declares the variable to be an array.

You’ll need to have one in the code module itself. If it is in a different code module or form, the Translator will not know that you mean it to be an array. It’s OK to have multiple DIM statements for the same array in different modules - nothing will get messed up.


#3

I have the following code where GameBetsArray is defined in the same function it is used, but I still get an error saying GameBetsArray is not a function - if I use square brackets (GameBetsArray[i][0] and GameBetsArray[i][1]) I don’t get the error. The UBound(GameBetsArray) never seems to have a problem.

  Dim GameBetsArray = Array()
  For i = 0 To results.rows.length-1
    Push(GameBetsArray,Array(results.rows.item(i)["game"],results.rows.item(i)["defaultbet"]))
  Next
  If GameSelect.ListCount > 1 Then
    Game1Value.value = 0
    For i = 0 To UBound(GameBetsArray)
      If GameBetsArray(i,0) = GameSelect.List(1) Then
        Game1Value.value = GameBetsArray(i,1)
        Exit For
      End If
    Next
 End If

#4

What happens if you do this:

Dim GameBetsArray()

George Henne
NS BASIC Corporation
http://www.nsbasic.com


#5

That solves the bracket problem, but then the first Push into the array puts the elements in (1,0) and (1,1) and not (0,0) and (0,1) (GameBetsArray(0,0) is undefined)


#6

What is your push statement exactly?

(Tip: when entering code using the web interface of this board, use the </> icon to format the code nicely)


#7

It’s the Push statement shown in the code at the top of this discussion


#8

I see what you mean now - if I use both of the following statements I get the results I want:

Dim GameBetsArray()
GameBetsArray = Array()