Android Question Dynamic array dimension ?

Genricke

Member
Hi !
Please tell me, is this the correct code in B4A ?
The code works, but I don't understand why ... :(

B4X:
Sub Class_Globals
    Private MyLabel()     As String
    Private MyEntry()    As B4XFloatTextField
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    MyLabel = Array As String ("Prompt1","Prompt2","Prompt3")
    Log(MyEntry.Length)    ' = 0
    Private MyEntry(MyLabel.Length) As B4XFloatTextField
    Log(MyEntry.Length)    ' = 3
End Sub

Private Sub B4XPage_Appear
    MyEntry(0).Text = "One" 'OK ! ???
    MyEntry(1).Text = "Two"    'OK ! ???
    MyEntry(2).Text = "Three"    'OK ! ???
End Sub

Thanks ! :)
 

Genricke

Member
What don't you understand?

If comment row 2 -> Error: Array not defined. OK !

In line 9, the array is declared as Private in inside Sub B4XPage_Created.
Why does the dimension of array in row 2 change ?
Thanks. I'm new to B4A, trying to master it. :)
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
In line 9, the array is declared as Private in inside Sub B4XPage_Created.
Why does the dimension of array in row 2 change
MyEntry is a global empty array until line 9.
Line 9 should be:
B4X:
Dim MyEntry(MyLabel.Length) As B4XFloatTextField
You are "redimming" the array and creating a new array with the same length as MyLabel length.
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
i think i understand his question erel.

first, he creates a new array in Class_Globals that has no length,
then he creates a new array in B4XPage_Created with the same name and gives it a length value.

now he changes the array values in position 0,1 and 2.

the question is if it is a new array that is created in B4XPage_Created then how does the code work in B4XPage_Appear??

he should get an error:

java.lang.ArrayIndexOutOfBoundsException: 0

since the length is 0 and you cannot set the items in the array. OR the array that is "redimmed" in B4XPage_Created is just updating the array in Class_Globals and you still have reference to this array? if yes then is that the right behavior?

i would expect that if i redimm an object it is recreated and if it is recreated why can i access it if it was created inside a sub??

btw. is it possible to create an array without a length and then set the length and add items to it in a later phase?
like you can push items to an array in javascript?

i know that list are here a much better solution but just to understand how b4x arrays work.

thanx
 
Upvote 0

Genricke

Member
Thanks ! :) If the Private attribute inside the Sub is ignored, then everything is correct.
Working with an array is more compact and convenient than a list.
 
Upvote 0

emexes

Expert
Licensed User
the question is if it is a new array that is created in B4XPage_Created then how does the code work in B4XPage_Appear??
I think that the Private/Public attribute refers to visibility between source files, and not between Subs in the one source file.

MyEntry() array is declared in/as global, and thus all of the MyEntry() usages in all of the Subs in this one source file refer to that one array.

The array is allocated memory for three elements in B4XPage_Created(). If B4XPage_Appear() was called before memory was allocated for the array, I suspect it would complain loudly.
 
Upvote 0
Top