Page 2 of 2

Re: Migrating from VB6

Posted: Mon Jan 25, 2021 3:35 pm
by mk-soft
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 ...

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)

To make it a class, only one small step is required :wink:

Re: Migrating from VB6

Posted: Mon Jan 25, 2021 5:35 pm
by kpeters58
And there is also XOJO - it is object-oriented and cross platform

Just in case you missed it in your decision making process.

Re: Migrating from VB6

Posted: Mon Jan 25, 2021 5:44 pm
by Bitblazer
kpeters58 wrote:And there is also XOJO - it is object-oriented and cross platform

Just in case you missed it in your decision making process.
700 for a one year license...

Re: Migrating from VB6

Posted: Mon Jan 25, 2021 6:43 pm
by skywalk
The closest to VB6 is by far, Gambas.
But, I found PB more powerful and a less terse syntax.