B4J Question [BANano] Is it possible to concat String Arrays as in Javascript?

Hi,
Is it possible to make this in BANano B4j:

JavaScript:
const arr1 = ["Cecilie", "Lone"];
const arr2 = [1, 2, 3];
const arr3 = arr1.concat(arr2);

I tried something like this:
B4X:
Private hidden() As String = Array As String("one", "two", "three")
Private shown() As String = Array As String("four", "five", "six")
Private all() As String = Array As String(hidden(), shown())

But the above does not work.
Or is the only option to merge them in a function at init?
 

angel_

Well-Known Member
Licensed User
Longtime User
Try this:

B4X:
 Private all() As String = BANano.Concat(arr1, arr2)
 
Upvote 0
Solution
Try this:

B4X:
 Private all() As String = BANano.Concat(arr1, arr2)
Ok thanks, I guess this should work but for me the transpiled version looks like this:
JavaScript:
this.all = this.arr1.concat(_B.arr2);

And _B is not defined until initialize where _B = this;
So I get an error that arr2 is undefined.

I changed _B. by hand to this. after transpiled then it works so I guess it might be a bug in the transpiler.
 
Last edited:
Upvote 0
@alwaysbusy can yo take a look at this?
Kind of not so much fun to change it manually every time I build.
The transpiler should make this code to make it work:
JavaScript:
this.all = this.arr1.concat(this.arr2);
Instead of the wrong one I mention in previous reply with _B.arr2.
 
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
I need a runnable example to find out what goes wrong in your code. Works fine here:

B4X:
Sub BANano_Ready()
     Dim arr1() As String = Array(1,2,3)
     Dim arr2() As String = Array(4,5,6)
     Dim all() As String = BANano.Concat(arr1, arr2)
     Log(all) ' [1,2,3,4,5,6]
End Sub

The only way I can reproduce something like yours is because you would put all of it in the Globals method, which is just for declaration in B4J.
B4X:
Sub Process_Globals
    Dim arr1() As String = Array(1,2,3)
    Dim arr2() As String = Array(4,5,6)        
    Dim all() As String = BANano.Concat(arr1, arr2)
End Sub

But this is would be wrong as the IDE even shows in the logs:
Main - 14: This sub should only be used for variables declaration or assignments of primitive values. (warning #29)

Alwaysbusy
 
Upvote 0
Ok, then it is my fault. I missed that warning.
I have it in the Process_Globals but it works if it has "this" instead of _B and if I look in Javascript descriptions then I do not find that limitation.
But what I want to do is have two separate arrays concatenated to one, see first question in this thread.
I need to use the two arrays separately and them both together and concat works as I want but maybe you have a better solution?

Best regards
Christian
 
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
if I look in Javascript descriptions then I do not find that limitation
True, but it is a B4J one. I honestly don't see the problem why you can not just concat them in the initialize of the class (or write one if it is a module and run it in BANano_Ready). Beats having to that manual change every time you do a build to me. As in B4J, BANano uses Global only for declarations and primitive initializations. There are many things that can be done in JavaScript and not in B4J, but we've chosen to get as close as possible to B4X behavior as possible.
 
Upvote 0
Ok, thank you for the help!
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…