I would like to have a multi-level type structure similar to:
B4X:
Sub Process_Globals
Type type1 (name As String, number As Int)
Type type2 (org As String, assoc As Int, person(100) As type1)
Type type3 (country As String, zip As Int, assoc(100) As type2)
Dim individual As type1
Dim company As type2
Dim nationality As type3
End Sub
Then the following works:
B4X:
Public Sub Show
If frm.IsInitialized = False Then
frm.Initialize("frm", 440, 520)
frm.RootPane.LoadLayout("Login")
company.Initialize
company.person(0).name = "ted"
company.person(0).number = 5
Log (company.person(0).name)
End If
frm.Show
End Sub
However the following compiles but results in a NullPointerException when run:
B4X:
Public Sub Show
If frm.IsInitialized = False Then
frm.Initialize("frm", 440, 520)
frm.RootPane.LoadLayout("Login")
nationality.Initialize
nationality.assoc(0).person(0).name = "ted"
nationality.assoc(0).person(0).number = 5
Log (nationality.assoc(0).person(0).name)
End If
frm.Show
End Sub
You might be best served by creating a factory method like this:
B4X:
Sub NewType3 As Type3
Dim t3 As Type3
t3.Initialize
For Each t2 As Type2 in t3.assoc
t2.Initialize
For Each t1 As Type1 in t2.person
t1.Initialize
Next
Next
Return t3
End Sub
Then you would use it like so:
B4X:
Dim nationality As Type3 = NewType3
nationality.assoc(0).person(0).name = "ted"
nationality.assoc(0).person(0).number = 5
Log(nationality.assoc(0).person(0).name)
Beware, none of this is tested. Also, do you really need an array of 100 elements in Type2 and Type3? Initializing all of that will require 10,000 iterations. You might think about using a List instead and only adding elements as necessary.
You might be best served by creating a factory method like this:
...
Beware, none of this is tested.
Also, do you really need an array of 100 elements in Type2 and Type3?
Initializing all of that will require 10,000 iterations.
You might think about using a List instead and only adding elements as necessary.