Android Question Iterate over Type

joseluis

Active Member
Licensed User
Longtime User
Is there a way of iterating over the Type fields? Maybe with reflection? For example:

B4X:
    Type LogTypes(NONE As Int, TEXT As Int, INFO As Int, ERROR As Int, _
WARNING As Int, DEBUG As Int, COMMAND As Int, RESPONSE As Int )

Dim LOGTYPE As LogTypes

' This doesn't work, obviously
Dim n as Int : n = 0
For Each item As Int in LOGTYPE
   n = n +1
   item = n
Next
 

LucaMs

Expert
Licensed User
Longtime User
I think not.

But I have a strange idea.

You could create a class with properties corresponding to those "fields" and expose a Map that contains the values of the member variables of the properties.

For example:

Class name: clsLogType

Private mNone, mText, mInfo, ... As Int
Private (or Public) mapFields As Map

Public Sub setNone(None As Int)
mNone = None
mapFields.Put(0, mNone)
End Sub
Public Sub getNone as Int
Return mNone
End sub
...

Public Sub getFields As Map
Return mapFields
End sub



(Written on the fly here, I hope it is clear).
 
Upvote 0

joseluis

Active Member
Licensed User
Longtime User
Thank you for the example Lucas. It's appretiated

Yes, that could work. I'll use it in case I really need the ability to enumerate the childs of a structure, since I prefer not to clutter the modules panel with cosmetic classes, which needs more writing to do than to fill a Type structure.

What I trying to do is to replicate an Enum structure in b4a. It's possible to do it Type but I must fill manually all the values. That's not very efficient. But at least is shorter and doesn't occupy a module slot.

I thought that if I can iterate a map with Value method, since it can hold any kind of object... maybe it's possible to do it with Type structures too.
 
Upvote 0
Top