Android Question Pass string array as object

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Is it possible to pass a (1D) string array as an object to a Sub and make that Sub handle the object as an array?
Tried all sort, but can't make this to work and I am guessing it is best to avoid the string array and pass a list instead.
With a list there is no problem passing it as an object and handle it in the receiving Sub as a list.

RBS
 

epiCode

Active Member
Licensed User
newsub( receiveString() as string) as returnString()
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
newsub( receiveString() as string) as returnString()
Yes, thanks, that seems to work.
I take it your function looks like this:

B4X:
Sub GetArrayFromObject(arrString() As String) As String()
    Return arrString
End Sub

Needs some further work as my objects can also be a ResultSet or a List or a Map or B4XOrderedMap.

RBS
 
Upvote 0

epiCode

Active Member
Licensed User
yes sorry I wrote returnstring instead of string() but glad that it worked for you!
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
newsub( receiveString() as string) as returnString()
How would I do similar with a 2D string array?

I tried with:

B4X:
Sub Get2DStringArrayFromObject(arr2DString(,) As String) As String(,)
    Return arr2DString
End Sub

But that gives me this error:

Cannot cast type: {Type=String,Rank=2, RemoteObject=True} to: {Type=String,Rank=1, RemoteObject=True}

RBS
 
Upvote 0

emexes

Expert
Licensed User
Well, I'm having trouble seeing what the compiler is complaining about with yours, but this seemingly-equivalent declaration appears to work:

B4X:
Sub Quotem2D(NumX As Int, NumY As Int, SA2D(,) As String) As String(,)

    Dim Quoted(NumX, NumY) As String
  
    For X = 0 To NumX - 1
        For Y = 0 To NumY - 1
            Quoted(X, Y) = """" & SA2D(X, Y) & """"
        Next
    Next
  
    Return Quoted

End Sub

Sub AppStart (Args() As String)
  
    Dim RandomStuff(6, 10) As String
  
    For I = 0 To 6 - 1
        For J = 0 To 10 - 1
            RandomStuff(I, J) = "Map " & Rnd(1, 201) & " " & Chr(Rnd(65, 75)) & Rnd(1,13)
        Next
    Next

    For X = 0 To 6 - 1 Step 3
        For Y = 0 To 10 - 1 Step 3
            Log(X & TAB & Y & TAB & RandomStuff(X, Y) & tab & Quotem2D(6, 10, RandomStuff)(X, Y))
        Next
    Next
  
End Sub

Log output:
Waiting for debugger to connect...
Program started.
0    0    Map 188 G2     "Map 188 G2"
0    3    Map 80 C6      "Map 80 C6"
0    6    Map 183 J2     "Map 183 J2"
0    9    Map 54 C11     "Map 54 C11"
3    0    Map 71 C1      "Map 71 C1"
3    3    Map 90 J5      "Map 90 J5"
3    6    Map 153 A8     "Map 153 A8"
3    9    Map 100 D3     "Map 100 D3"
Program terminated (StartMessageLoop was not called).
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
Well, I'm having trouble seeing what the compiler is complaining about with yours, but this seemingly-equivalent declaration appears to work:

Actually, when I plug in your Sub instead of mine into:

B4X:
Sub Get2DStringArrayFromObject(arr2DString(,) As String) As String(,)
    Return arr2DString
End Sub

Sub AppStart (Args() As String)
  
    Dim RandomStuff(6, 10) As String
  
    For I = 0 To 6 - 1
        For J = 0 To 10 - 1
            RandomStuff(I, J) = "Map " & Rnd(1, 201) & " " & Chr(Rnd(65, 75)) & Rnd(1,13)
        Next
    Next

    For X = 0 To 6 - 1 Step 3
        For Y = 0 To 10 - 1 Step 3
            '''Log(X & TAB & Y & TAB & RandomStuff(X, Y) & TAB & Quotem2D(6, 10, RandomStuff)(X, Y))
            Log(X & TAB & Y & TAB & RandomStuff(X, Y) & TAB & Get2DStringArrayFromObject(RandomStuff)(X, Y))
          
        Next
    Next
  
End Sub

it works as expected ie:

Log output:
Waiting for debugger to connect...
Program started.
0    0    Map 187 F11    Map 187 F11
0    3    Map 138 B8     Map 138 B8
0    6    Map 120 A1     Map 120 A1
0    9    Map 7 J1       Map 7 J1
3    0    Map 90 A8      Map 90 A8
3    3    Map 122 F8     Map 122 F8
3    6    Map 90 E11     Map 90 E11
3    9    Map 165 J5     Map 165 J5
Program terminated (StartMessageLoop was not called).

so the error must be coming from the calling code (which I can't find above).
 
Upvote 0

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Yes, you are right the error come indeed from the calling code and it works all fine as expected.
Thanks again.

RBS
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…