Re: Migrating from VB6
Posted: Mon Jan 25, 2021 3:35 pm
Modules have the advantage that the internal structure does not have to be global and the procedure names do not have to be globally unique.
For your own safety, however, it is not bad to check the validity of object data, even if it requires more programming time. This can be realised via maps.
Small example ...
To make it a class, only one small step is required 
For your own safety, however, it is not bad to check the validity of object data, even if it requires more programming time. This can be realised via maps.
Small example ...
Code: Select all
;-TOP
DeclareModule MyModule
Declare Create(Name.s = "", Value = 0)
Declare Free(*this)
Declare SetName(*this, Name.s)
Declare.s GetName(*this)
EndDeclareModule
Module MyModule
Global NewMap ValidData()
Procedure IsData(*this)
If FindMapElement(ValidData(), Str(*this))
ProcedureReturn #True
Else
Debug "Error Module " + #PB_Compiler_Module + ": Object data not exixts! [" + Hex(*this) + "]"
ProcedureReturn #False
EndIf
EndProcedure
Structure sMyModule
iValue.i
sName.s
EndStructure
Procedure Create(Name.s = "", Value = 0)
Protected *this.sMyModule
*this = AllocateStructure(sMyModule)
If *this
AddMapElement(ValidData(), Str(*this))
With *this
\sName = Name
\iValue = Value
EndWith
EndIf
ProcedureReturn *this
EndProcedure
Procedure Free(*this.sMyModule)
If IsData(*this)
DeleteMapElement(ValidData())
FreeStructure(*this)
ProcedureReturn #True
Else
ProcedureReturn #False
EndIf
EndProcedure
Procedure SetName(*this.sMyModule, Name.s)
If IsData(*this)
*this\sName = Name
EndIf
EndProcedure
Procedure.s GetName(*this.sMyModule)
If IsData(*this)
ProcedureReturn *this\sName
EndIf
EndProcedure
EndModule
;-
Global NewList Objects()
Debug "Fill ..."
For i = 1 To 10
AddElement(Objects())
Objects() = MyModule::Create("MyName " + i, i + 100)
Next
Debug "Output ..."
ForEach Objects()
Debug MyModule::GetName(Objects())
Next
Debug "Free ..."
ForEach Objects()
Debug MyModule::Free(Objects())
Next
ClearList(Objects())
Debug "Double free object ..."
obj = MyModule::Create()
Debug MyModule::Free(obj)
Debug MyModule::Free(obj)
