Android Question [SOLVED] Redim Array - and/or - copy List to Array

Lee Gillie CCP

Active Member
Licensed User
Longtime User
I have this class... SerializableOpenOrder...
B4X:
Sub Class_Globals
    Public header As OpenOrderHeader
    Public lines() As OpenOrderLine
End Sub

I have built a List containing some number of OpenOrderLine. I'm trying to get lines of this list into the lines() array of the serializable open order class instance.

I was hoping the list would self cast to an array, but that does not work.
B4X:
Dim soo as SerializableOpenOrder
soo.Initialize
soo.lines = mylist

Next I tried to REDIM the lines array in my SerializableOpenOrder object instance, and then copy all elements from the list to the array (using a loop). I can not seem to get soo.ines redimensioned so that it can accept items from my dynamic list. What is the trick please?
 

Lee Gillie CCP

Active Member
Licensed User
Longtime User
I can not seem to dimension SerialiableOpenOrder.lines from outside the class. It must not be valid to do so?? But I was able to create a "Redimension" method for SerializableOpenOrder which dimensions the array from WITHIN the class. Then I was able to copy line objects from the dynamic array into the array within the class. This works, but if there is a better way please feel free to pipe in.
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
You can create a new array of your preferred size and then point your member array to it:
B4X:
Dim someOtherArray(mylist.Size) as OpenOrderLine
soo.lines = someOtherArray
Now, soo.lines is of size mylist.Size. You can, of course, also use any other number.

Is there any particular reason why you're using an array in your class instead of a List? It seems you're getting Lists from outside your class, it might just be easier to make lines a List. Then you could do stuff like this:
B4X:
lines.AddAll(mylist)   'no looping required, or
lines = mylist    'just point it directly at an external List, or
lines.AddAll(someOtherArray)   'easy to work with Lists and arrays when you have a List, not so easy when you have an array.
 
Upvote 0

Lee Gillie CCP

Active Member
Licensed User
Longtime User
Thanks for taking a moment to comment!

I like your idea. Could not see it before, but I eventually found a post by Agraham suggesting it as well.

I also like lists! Boundless, dynamic, automatic! My choice of using an array has to do with the eventual JSON deserialization when it gets to the other end of the wire. Using Newtonsoft.Json from VB.net to deserialize, so far it seems to like the genuine array of class instances (at time of serialization), rather than the list. I found similar for class instances to use the actual class, when serializing from B4A, rather than a map with all the field values. You get residuals like "MyMap" which seem to cause a snag. But I'm still finding my way with this, so I may be back to trying other ideas.

Best regards - Lee
 
Upvote 0
Top