I tweaked fxm's solution to make it more flexible and, hopefully, easier to understand. The crucial statement in fxm's original code, here, is 'ct += 1' and in the tweaked code 'temp += 1'.
For the UDT instances I have used instance0, instance1, and so on but, obviously, they can be any names you like. The instance ordering in the Scope statements needs to be the same as the ordering in the ID() array in Type MyParams. If not, we will be in serious trouble.
Run the following:
Code: Select all
'#include "windows.bi"
Type MyParams
Public:
Declare Constructor()
Declare Destructor()
Private:
As UByte ct, dt
As String ID(3) = { "instance0", "instance1", "instance2", "instance3" }
End Type
Constructor MyParams()
Static As Byte temp = -1
temp += 1
ct = temp
Print "Inside Constructor ";ID(ct)
' if ID(ct) = "instance1" then messagebox(0,"Do you want me to load '" + ID(ct)+"SavedData.dat'", "Constructor "+ID(ct), MB_YESNO)
End Constructor
Destructor MyParams()
Static As Byte temp = -1
temp += 1
dt = temp
Print "Inside Destructor ";ID(dt)
' if ID(dt) = "instance1" then messagebox(0,"Do you want me to create '" + ID(dt) + "SavedData.dat'", "Destructor "+ID(dt), MB_YESNO)
End Destructor
Scope
Dim As MyParams instance0
Dim As MyParams instance1
Dim As MyParams instance2
Dim As MyParams instance3
Print
End Scope
Sleep
Code: Select all
Inside Constructor instance0
Inside Constructor instance1
Inside Constructor instance2
Inside Constructor instance3
Inside Destructor instance0
Inside Destructor instance1
Inside Destructor instance2
Inside Destructor instance3
However, we are not making use of the identification.
If we remove the comment in both the Constructor and Destructor, and the “windows.bi” include, we get some additional work with instance1. For illustration purposes, I have used the Windows API MessageBox and do not respond to the selected choice.
Needless to say, there could be a myriad of types of additional work for the instance1 Constructor/Destructor or any other instance we decide to add more work to.
I am undecided whether to use the above in a particular application because it requires the user to get their hands dirty, and I don't really want to do that.
Anyway, I figured it worth mentioning as it may be food for thought in some of your applications.