Android Question How do I create an IterableList

Alessandro71

Well-Known Member
Licensed User
Longtime User
I'm building a class which would benefit from being used in a For Each statement, but how do I create an IterableList of its content?
Looks like the IterableList object is not even declarable in a Dim statement.
 

MikeSW17

Active Member
Licensed User
 
Upvote 0

Albert Kallal

Active Member
Licensed User
sadly the search function didn't return the above link...

Ok, well you can make a "list", but you have to roll your own. If this is a small class, and you don't do a lot of looping etc. on that class?

Then you can "maintain + hand code" a "list" in your class, and then return that.
So, for each new memeber of the class, then you have to add that our method - lets use "items" for that method.

And like in .net, I often don't bother to write get-ers and set-ers but just expose the base variables as public.

So, a simple class like this:

B4X:
---- clsPerson
Sub Class_Globals
    Public FirstName As String
    Public LastName As String
    Private m_Map As Map
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    m_Map.Initialize
    
End Sub

Sub FullName As String
    
    Return FirstName & " " & LastName
    
End Sub

Sub Items As Map
    
    m_Map.Put("FirstName",FirstName)
    m_Map.Put("LastName",LastName)
    m_Map.Put("FullName",FullName)
    
    Return m_Map
    
End Sub

So in above, just remember while coding that you tie a little string around your finger, and for EACH property/method of the class, make sure you add it to the "items" method in above.

Now, in code we can do this:

B4X:
    Dim OnePerson As clsPerson
    OnePerson.Initialize
    
    OnePerson.FirstName = "Albert"
    OnePerson.LastName = "Kallal"
    
    For Each MyKey As String In OnePerson.Items.Keys
        Log("Class item = " & MyKey & " Value of item = " & OnePerson.Items.Get(MyKey))
    Next
    
    Dim strMyProperty As String = "FullName"
    
    Log(OnePerson.Items.Get(strMyProperty))

Note VERY interesting that LAST line of code. By adding that "map" to the class, then we can get/grab/retrive ANY member by a key value (a string value in a variable).

Output:
B4X:
Class item = FirstName Value of item = Albert
Class item = LastName Value of item = Kallal
Class item = FullName Value of item = Albert Kallal

Albert Kallal

So there is no "attribute" say like in .net (or even VBA/VB6) in which you can mark each method/property the class as to be "interable" for that class.

I will note that because that I re-fill the map each time, then that "items" code is re-run for "each" for-each or any time you referernce "map". If this was to be a processing intensive loop, or performance was going to matter, then I would move the public FirstName/LastName to get/setters, and for all methods I would "maintain" the map always - and then just expose the map as public. That way you could use a for/each and not re-trigger the full re-load of the map each time.

However, given we have 1+ million instructions per second, then the above simple approach for "most" cases should suffice without performance considerations.

So, you the Mark II Human carbon based computer have to maintain and update that "items" method for each new property/method. Given that most classes we hope are not all that complex, then I find this approach is rather worth the extra coding time to maintain that "items" method.

The end result is not only do we get for/each iteration of the values (and returned as a key), we also gain the ability to get/grab ANY property by using a string variable. (as per last line of code in above).

And I also think that perhaps a "key pair" value could be exposed, and that could used for the for each

So you could have For each OneV as OneKeySet in OnePersions.items
OneV.Key, and OneV.Value could be returned.

Regardless, you can (have to) roll your own exposed list or I think "map" in this case. I think map is somewhat better here, but one can re-write above using a "list" if you wish. I would prefer a key name + value pair returned, or as per above just get/grab that value.

So, we could ouput say
Type MyKey(Key As String,Value As Object)

To a list, and then your for each would interate on above type returned as a list.

So, you have thus a few ideas here.

Regards,
Albert D. Kallal
Edmonton, Alberta Canada
 
Upvote 0
Top