Migrating from VB6

Just starting out? Need help? Post your questions and find answers here.
User avatar
mk-soft
Always Here
Always Here
Posts: 6204
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Migrating from VB6

Post 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:
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
kpeters58
Enthusiast
Enthusiast
Posts: 341
Joined: Tue Nov 22, 2011 5:11 pm
Location: Kelowna, BC, Canada

Re: Migrating from VB6

Post 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.
PB 5.73 on Windows 10 & OS X High Sierra
Bitblazer
Enthusiast
Enthusiast
Posts: 761
Joined: Mon Apr 10, 2017 6:17 pm
Location: Germany
Contact:

Re: Migrating from VB6

Post 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...
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Migrating from VB6

Post by skywalk »

The closest to VB6 is by far, Gambas.
But, I found PB more powerful and a less terse syntax.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply