B4J Question Initialising Type Variables

Chris2

Active Member
Licensed User
This is pretty basic I think but I'm just after some clarification on initialising Type variables please.
Given....
B4X:
Sub Process_Globals
Type myType (str as String, b as Boolean, i as Int)
End Sub
Is the following OK
B4X:
Dim t as myType = mapMyTypes.Get("type1")    'where the values in mapMyTypes are initialized myTypes
Or do I need to explicitly initialize 't' first with...
B4X:
Dim t as myType
t.initialize
t= mapMyTypes.Get("type1")   

'OR

Dim t as myType=CreatemyType(...
t= mapMyTypes.Get("type1") 

Public Sub CreatemyType (str As String, b As Boolean, i As Int) As myType
    Dim t1 As myType
    t1.Initialize
    t1.str = str
    t1.b = b
    t1.i = i
    Return t1
End Sub
?

Thanks.
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
It is not only OK. But the correct way, initialize is a burdensome process (to any b4x object), initializing and later assigning an object is creating 2 times in RAM the object.

The GC will eventually delete the first object, but HEAP will grow larger.
 
Upvote 0

Chris2

Active Member
Licensed User
Thanks everyone.
I was aware that the first option was correct for lists & maps, but for some reason thought that it might be different for custom type variables.
I now know that it is not!
 
Upvote 0
Top