Android Question Initialize, Initialize2 and IsInitialized

Alessandro71

Well-Known Member
Licensed User
Longtime User
How does B4X tags an object as "initialized"?
I understand that calling "Initialize" does the trick, but I saw some classes also have an "Initialize2" sub that seems to do the trick also.
If my class has 3 different initialize use cases, may I create an Initialize3 and have the IsInitialized return True?
 

Daestrum

Expert
Licensed User
Longtime User
As I understand it ( small example)

main code
B4X:
    Dim c1 As MyClass1
    Log(c1)
    c1.Initialize
    Log(c1)
    c1.Initialize2("fred")
    Log(c1)
    c1.Initialize3("Mike",44)
    Log(c1)

MyClass
B4X:
Sub Class_Globals
    Dim isInitialized As Boolean = False
    Dim name As String
    Dim age As Int
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize ' do nothing just default values
    isInitialized = True
End Sub

public Sub Initialize2(n As String) ' set name ignore age
    name = n
    isInitialized = True
End Sub

public Sub Initialize3(n As String, a As Int) ' set name and age
    name = n
    age = a
    isInitialized = True
End Sub
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
If you create the class in Java you can use Initialize, Initialize2 etc... If you create it from the IDE environment only Initialize is valid
 
Upvote 0

Alessandro71

Well-Known Member
Licensed User
Longtime User
The "original" Sub Initialize must be called anyway.

You could add a parameter to it; a map would be a good choice.

Sub Initialize(Params As Map)
I understand the concept of using a Map to accomodate a variable count of arguments, but I still prefer the multiple Subs approach, since it can enforce, at least, some type checking on the arguments
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
slightly different way - you just call initialize ( this is easier to read)
B4X:
Sub Process_Globals
    Type JustName(n As String)
    Type NameAndAge(n As String, a As Int)
End Sub

Sub AppStart (Args() As String)
    Dim c1,c2,c3 As MyClass1
    Log("C1 initialized = " & c1.isInitialized)

    c1.Initialize(Null)
    Log("C1 initialized = " & c1.isInitialized)
    
    c2.Initialize(CreatejustName("fred"))
    Log("C2 initialized = " & c2.isInitialized)
    
    c3.Initialize(CreatenameAndAge("Mike",44))
    Log("C3 initialized = " & c3.isInitialized)
End Sub


Public Sub CreatejustName (n As String) As JustName
    Dim t1 As JustName
    t1.Initialize
    t1.n = n
    Return t1
End Sub

Public Sub CreatenameAndAge (n As String, a As Int) As NameAndAge
    Dim t1 As NameAndAge
    t1.Initialize
    t1.n = n
    t1.a = a
    Return t1
End Sub

MyClass
B4X:
Sub Class_Globals
    Dim isInitialized As Boolean = False
    Private name As String 'ignore
    Private age As Int 'ignore
End Sub

'
Public Sub Initialize(o As Object)
    isInitialized = True
    If o = Null Then Return ' no params just initialize
    
    If o Is justName Then   ' initialize with name only
        Initialize2(o.As(justName).n)
    else if o Is nameAndAge Then  ' initialize name and age
        Initialize3(o.As(nameAndAge).n,o.As(nameAndAge).a)
    End If
End Sub

Public Sub Initialize2(n As String) ' set name ignore age
    name = n
    isInitialized = True
End Sub

Public Sub Initialize3(n As String, a As Int) ' set name and age
    name = n
    age = a
    isInitialized = True
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…