Android Question [Custom Class] is it possible to set isInitilized to False if a condition is not met?

Cableguy

Expert
Licensed User
Longtime User
Hi guys....

So, the title says it all, but as an example, imagine the my class implements methods that depend on particular hardware, or OS version...
I Test this conditions on the Initialize method, and I return a Boolean, and that works... but, If I test isInitialized, it returns True...
So.... Is it possible to return isInitialized as False or to "Un-Initialize" my class?
 
Solution
Something like (B4J but should work in B4A too)
B4X:
Sub Process_Globals
    Dim k As myClass
End Sub

Sub AppStart (Args() As String)
    k.Initialize(4,"init")
    Log(k.isInitialized)  ' returns true
    k.Initialize(5,"fred")
    Log(k.isinitialized) ' returns false
End Sub
myClass
B4X:
Sub Class_Globals
    Dim isInitialized As Boolean
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize( a As Int, b As String)
    If b <> "init" Then
        isInitialized = False
    Else
        isInitialized = True
    End If
End Sub

Cableguy

Expert
Licensed User
Longtime User
I would implement my own “isInitialized” function.
I tried, but replacing the "native" isInitialized function triggers an error (quite logically)
B4X:
java.lang.RuntimeException: Class instance was not initialized (.....)

while the native function returns False
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Something like (B4J but should work in B4A too)
B4X:
Sub Process_Globals
    Dim k As myClass
End Sub

Sub AppStart (Args() As String)
    k.Initialize(4,"init")
    Log(k.isInitialized)  ' returns true
    k.Initialize(5,"fred")
    Log(k.isinitialized) ' returns false
End Sub
myClass
B4X:
Sub Class_Globals
    Dim isInitialized As Boolean
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize( a As Int, b As String)
    If b <> "init" Then
        isInitialized = False
    Else
        isInitialized = True
    End If
End Sub
 
Upvote 0
Solution

Cableguy

Expert
Licensed User
Longtime User
Something like (B4J but should work in B4A too)
B4X:
Sub Process_Globals
    Dim k As myClass
End Sub

Sub AppStart (Args() As String)
    k.Initialize(4,"init")
    Log(k.isInitialized)  ' returns true
    k.Initialize(5,"fred")
    Log(k.isinitialized) ' returns false
End Sub
myClass
B4X:
Sub Class_Globals
    Dim isInitialized As Boolean
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize( a As Int, b As String)
    If b <> "init" Then
        isInitialized = False
    Else
        isInitialized = True
    End If
End Sub
That Worked!!!
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
The object will still be created - you need to rely on isInitialized to know if its usable
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
The object will still be created - you need to rely on isInitialized to know if its usable
Yes, but since it no longer be "usable" then the Java Garbage Collector will take care of it... AFAIUI (as far as I understand it)
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Only as far as B4J is concerned, for java it will still be tied to the
Dim thing As MyClass

safer to add this line then you know GC will remove from memory
if thing.isInitialized = false then thing=null
 
Upvote 1

Cableguy

Expert
Licensed User
Longtime User
Only as far as B4J is concerned, for java it will still be tied to the
Dim thing As MyClass

safer to add this line then you know GC will remove from memory
if thing.isInitialized = false then thing=null
Nice Tip,

Thanks
 
Upvote 0
Top