Dim MyCustomClassList As List
Dim MyCustomClass2List as List
Type tPerson(Name As String, Age As Int)
Dim lstPersons As List ' ... As List Of(tPerson)
' init & add here
If lstPersons.Get(Index).Name = "Erel" Then
Log("Lucky man")
End If
Dim myList as List of MyClass
'some languages have you do something like
Dim myList as List<MyClass>
Dim myList as List = LoadMyList
Dim mc as MyClass = myList.Get(0)
...
Aside from the fact that that wouldn't be very practical, the point is that with a typed list (if you're talking about these) it can be used like (like Arrays):
B4X:Type tPerson(Name As String, Age As Int) Dim lstPersons As List ' ... As List Of(tPerson) ' init & add here If lstPersons.Get(Index).Name = "Erel" Then Log("Lucky man") End If
?
In case you hadn't thought of it, why not just name the lists appropriately? You'll soon find out if the list contains an element of the wrong type when you try to access it if you don't check it when reading.
B4X:Dim MyCustomClassList As List Dim MyCustomClass2List as List
I've wanted to do this several times:
B4X:Dim myList as List of MyClass 'some languages have you do something like Dim myList as List<MyClass>
Sub Class_Globals
Private mList as List
End Sub
Sub Initialize
mList.Initialize
End Sub
Sub Add(element as MyObjectClass) '<----
mList.Add(element)
End Sub
Sub Get(index as Int) as MyObjectClass '<----
return mList.Get(index)
End Sub
Sub Size As int
return mList.Size
End Sub
Sub GetList as List
return mList ' In case the primary list is needed ;-)
End Sub
' ...the rest of needed methods
Dim myListOfMyObjectType as CL_ListOfMyObjectType
myListOfMyObjectType.Initialize
Dim myObjectTypeInstance as MyObjectType
' ...
myListOfMyObjectType.Add(myObjectTypeInstance)
' ...
A tricky workaround to achieve the same goal (detecting errors at compileTime) is to define small classes that do the trick to extend List for each type
The only disadvantage is that a class must be defined for each type (so it is not general) but can use them as a template for the next one just changing the object type.
Class CL_ListOfMyObjectType
B4X:Sub Class_Globals Private mList as List End Sub Sub Initialize mList.Initialize End Sub Sub Add(element as MyObjectClass) '<---- mList.Add(element) End Sub Sub Get(index as Int) as MyObjectClass '<---- return mList.Get(index) End Sub Sub Size As int return mList.Size End Sub Sub GetList as List return mList ' In case the primary list is needed ;-) End Sub ' ...the rest of needed methods
main code
B4X:Dim myListOfMyObjectType as CL_ListOfMyObjectType myListOfMyObjectType.Initialize Dim myObjectTypeInstance as MyObjectType ' ... myListOfMyObjectType.Add(myObjectTypeInstance) ' ...
Yes this is what I do sometimes too. But it's a lot of overhead code ofcourse.