Android Question Need of Iniatialize for library

demasi

Active Member
Licensed User
Longtime User
Hello,
I created a class and compiled as a library to use in my apps. This class have only a few methods / subs for strings manipulations and calculations.
Maybe this is a silly question, but why I need to initialize my class to use its methods? I have nothing to put in the initialize sub, but if I dont call it, I receive an error.
I want my library to function as StringUtils, that we just Dim and then use its methods, without Iniatilize.
Thank you!
 

demasi

Active Member
Licensed User
Longtime User
Thank you, JordiCP. In fact, I already tried this way, using class module. It works ok. The only problem is I´m not so creative with variable names and sub names, so sometimes I have a naming conflict with the code modules. So I converted to class modules and compile to library. It works ok, no problems.
I´m only curious about the need of the initialize, as in my initialize sub I have nothing to do.
I know it´s possible looking for example to StringUtils, that works the way I think my class should too, no need to Initialize. How they did this?
Thank you.
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
I think you did not understand correctly (or I expressed badly ;))

Put your Subs in a code module and compile it to a library. Name the code module "stringutils2" if you want, you'll get what you are asking for.
 
Upvote 0

demasi

Active Member
Licensed User
Longtime User
Thank you for your answer.
When creating an example project to show what I´m saying, I´ve found the answer.
I have some files manipulation in my class. Without this, it does not need the Initialize.
 
Upvote 0

demasi

Active Member
Licensed User
Longtime User
Just to leave here the correct answer, I think I spotted the problem. I was using class global variables. When I changed that to local variables in the subs scope I don´t need to initialize my class anymore.
But I´ve noted a strange behavior (at least for me) in the B4A IDE. When I compile in release mode, without class global variables and without initialize, it runs ok. But this same code crashes in debug mode, due to the lack of the Initialize class.
test1 runs ok. test2 crashes after the dialog.

This is the code I used:

Main Activity
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim myClass As mClass
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    myClass.test1
    Msgbox("Continue","")
    myClass.test2
End Sub

mClass
B4X:
Sub Class_Globals
    Dim gFile As String = "testfile.dat"
    Dim gDir As String  = File.DirDefaultExternal  
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize

End Sub

Sub test1 As Boolean
    File.WriteString(File.DirDefaultExternal,"testfile.dat","testdata")
    Log(File.Exists(File.DirAssets,"testfile.dat"))
    Return
End Sub

Sub test2 As Boolean
    File.WriteString(gDir,gFile,"testdata")
    Log(File.Exists(gDir,gFile))
    Return
End Sub
 
Upvote 0
Top