Android Question [RESOLVED] Coercing a List to a typed array

Lee Gillie CCP

Active Member
Licensed User
Longtime User
My eventual usage calls for an array of class instances, so this function returns an array...

B4X:
Sub DeliveredOrders As SerializableOpenOrder()

In the mechanics of it the orders are first created with a List because it is dynamic, so I can add items to the List as they are discovered. Once the List is built I coerce the List to an array (because that's what is needed downstream)...

B4X:
    Dim orders(reclsth.Size) As SerializableOpenOrder
    For i = 0 To reclsth.Size-1
        orders(i) = reclsth.Get(i)
    Next

    Return orders

I then eventually pass it to a routine that will utilize the array....

B4X:
Public Sub DeleteOrders( orders() As SerializableOpenOrder )

    Dim soo As SerializableOpenOrder
    Dim ooh As OpenOrderHeader
    Dim ool As OpenOrderLine   
   
    For i = 0 To orders.Length - 1

Where it says "orders.Length" I get a runtime error: java.lang.RuntimeException: Field; length not found in java.util.ArrayList. So I tried changing the reference to orders.Size at the FOR loop, but now it won't compile:
B4X:
Error compiling program.
Error description: Only 'Length' is supported by arrays.

I seem to have put myself into a pickle, and it's not really clear to me why this approach does not work.
 

Lee Gillie CCP

Active Member
Licensed User
Longtime User
Thanks for your reply Roycefer!

No I do not. I tried to make a tiny test project to illustrate the issue as I think I am observing it, but it does not reproduce the behavior of the large production project. There must be an intermediary involved I can not yet see. So for the moment I should call this closed so no one spins wheels on it, and I will trace it more deeply in the debugger. I'm obviously doing something wrong. I will report back here with my findings.
 
Upvote 0

Lee Gillie CCP

Active Member
Licensed User
Longtime User
In fact all with B4A works as expected, and I found my cockpit error down in the bowels of an intermediary routine.
 
Upvote 0
Top