LucaMs Expert Licensed User Longtime User Sep 4, 2015 #1 I tried something like: Code: Copy ' MyClass - Class Module Sub Class_Globals Private mInitialized As Boolean End Sub Public Sub Initialize(...) If mInitialized Then ' ... mInitialized = True End Sub but if I declare a variable of type MyClass and then I initialize it two times, mInitialized is always False. I know I can use IsInitialized but I wish the class itself can deal with this eventuality. Thank you
I tried something like: Code: Copy ' MyClass - Class Module Sub Class_Globals Private mInitialized As Boolean End Sub Public Sub Initialize(...) If mInitialized Then ' ... mInitialized = True End Sub but if I declare a variable of type MyClass and then I initialize it two times, mInitialized is always False. I know I can use IsInitialized but I wish the class itself can deal with this eventuality. Thank you
Erel B4X founder Staff member Licensed User Longtime User Sep 4, 2015 #2 I'm not sure that I understand. Can you create a small program that demonstrates it?
LucaMs Expert Licensed User Longtime User Sep 4, 2015 #3 It is simple but... wrong too. I would like to devise a method to create a Singleton class. This question is wrong, though. I fear that I will have to invent something written on the device. However, I am attaching a micro project. Thanks, Erel. Attachments ClassInitialization.zip ClassInitialization.zip 11.5 KB · Views: 332
It is simple but... wrong too. I would like to devise a method to create a Singleton class. This question is wrong, though. I fear that I will have to invent something written on the device. However, I am attaching a micro project. Thanks, Erel.
HotShoe Well-Known Member Licensed User Longtime User Sep 4, 2015 #4 LucaMs said: Public Sub Initialize(...) If mInitialized Then ' ... mInitialized = True End Sub Click to expand... I think you mean to do somethi8ng like: Code: Copy Public Sub Initialize(...) If not(mInitialized) Then ' ... init code here mInitialized = True End If End Sub --- Jem
LucaMs said: Public Sub Initialize(...) If mInitialized Then ' ... mInitialized = True End Sub Click to expand... I think you mean to do somethi8ng like: Code: Copy Public Sub Initialize(...) If not(mInitialized) Then ' ... init code here mInitialized = True End If End Sub --- Jem
LucaMs Expert Licensed User Longtime User Sep 4, 2015 #5 My concept was wrong. I meant: Code: Copy ' MyClass - Class Module Sub Class_Globals Private mInitialized As Boolean End Sub Public Sub Initialize(...) If mInitialized Then ' Prevent a second initialization Return End If ' ... mInitialized = True End Sub but this can be useful to prevent to "duplicate" the initialization of an object, it is not enough (it is wrong) to ensure the existence of one object only (Singleton class). Last edited: Sep 4, 2015
My concept was wrong. I meant: Code: Copy ' MyClass - Class Module Sub Class_Globals Private mInitialized As Boolean End Sub Public Sub Initialize(...) If mInitialized Then ' Prevent a second initialization Return End If ' ... mInitialized = True End Sub but this can be useful to prevent to "duplicate" the initialization of an object, it is not enough (it is wrong) to ensure the existence of one object only (Singleton class).
JordiCP Expert Licensed User Longtime User Sep 4, 2015 #6 The logic would have to be backed by a process single variable Code: Copy Sub Process_Globals Dim mOnceInitialized as Bool = False '... End Sub And the class declaration... Code: Copy ' MyClass - Class Module Sub Class_Globals Private mInitialized As Boolean End Sub Public Sub Initialize(...) If not(mOnceInitialized) Then ' ...initialize other fields mInitialized=True mOnceInitialized=True Return EndIf End Sub Anyway,it is not possible to prevent the existence of more than one object, but this way will grant you that only the first one will be initialized
The logic would have to be backed by a process single variable Code: Copy Sub Process_Globals Dim mOnceInitialized as Bool = False '... End Sub And the class declaration... Code: Copy ' MyClass - Class Module Sub Class_Globals Private mInitialized As Boolean End Sub Public Sub Initialize(...) If not(mOnceInitialized) Then ' ...initialize other fields mInitialized=True mOnceInitialized=True Return EndIf End Sub Anyway,it is not possible to prevent the existence of more than one object, but this way will grant you that only the first one will be initialized
Erel B4X founder Staff member Licensed User Longtime User Sep 6, 2015 #7 Use a code module if you want a singleton class.