Page 2 of 2
Re: PureBasic Modules: A Quick Tutorial
Posted: Sat Jun 02, 2018 4:07 pm
by DontTalkToMe
Code: Select all
DeclareModule myModule
EndDeclareModule
Module myModule
Define myVar = 123
EndModule
Debug myModule::myVar ; == 0 ?
However, you'd notice that the resulting output is still zero. Why is this?
The output can't be be zero since the code is not valid.
myVar is local to the module and it's not accessible from the external debug statement.
Less important, but I would add a .f to
since it's a float in the example after that which is a variation of this one.
Re: PureBasic Modules: A Quick Tutorial
Posted: Sun Aug 26, 2018 12:23 pm
by minimy
Wauuuu master TI, thanks for this very comprensive tutorial!!!
As allways PBforum is the best!!
Great community with a lot of gray matter!!
I now can put, halt and catch fire like a module in my HAL9000

HAHAHA
THANKS AGAIN TI!!! +1
Re: PureBasic Modules: A Quick Tutorial
Posted: Sun Oct 03, 2021 6:02 am
by chikega
Very nice tutorial on Modules! It's been added to my PureBasic Evernote Notebook

Re: PureBasic Modules: A Quick Tutorial
Posted: Mon Mar 20, 2023 11:33 am
by salutcava
Hello,
Tanks for your quick & simple tutorial on the use of modules. I had read it mostly for the interoperability section (would like to call a procedure in the parent/main code from code in a module) for my needs. Since the use of a common/dummy module wasnt helping me (or couldnt figure out how and don't want my whole code to be contained in modules). I post here a "workaround" to call a procedure which is in the main code from a line of code contained inside a module :
Code: Select all
Procedure MainProcedure(arg.s)
debug arg
EndProcedure
DeclareModule MyModule
Prototype proto(argument.s)
Global MainProc.proto=0
;.
;.
;.
EndDeclareModule
Module MyModule
Procedure ModuleProcedure(...)
;.
;.
;.
MainProc(text$)
;.
;.
;.
EndProcedure
EndModule
;.
;.
;.
MyModule::MainProc = @MainProcedure()
;.
;.
;.
End
Hope it will help. Bye !
Re: PureBasic Modules: A Quick Tutorial
Posted: Sat Apr 01, 2023 1:55 pm
by lgb-this
salutcava wrote: Mon Mar 20, 2023 11:33 am
Hope it will help. Bye !
Brilliant ! I was looking for this topic: "call an external procedure in a module" and your proposal worked !
Re: PureBasic Modules: A Quick Tutorial
Posted: Wed Jan 24, 2024 1:57 am
by KarLKoX
Very nice tutorial !
There is a tiny typo error :
"The variable myPrivateVariable and the function myPrivateFunction() are both inaccessible to the parent program"
To be changed to :
The variable myPrivateVariable and the function myPrivateProcedure() are both inaccessible to the parent program
Re: PureBasic Modules: A Quick Tutorial
Posted: Wed Jul 03, 2024 12:38 pm
by sst
In addition to the examples given before, here an example of a very common use case, namely, a structure defined in a module and called in other modules.
Code: Select all
DeclareModule module1
Structure Writer
name.s
surname.s
EndStructure
;.....
EndDeclareModule
Module module1
;here can be empty, but it must exist
;......
EndModule
DeclareModule module2
;......
EndDeclareModule
Module module2
Define w1.module1::Writer
w1\name="Isaac"
w1\surname="Asimov"
Debug w1\name ;=Isaac ;it works
;Define w2.Writer ; ERROR!
EndModule
Above, it must be noticed
module1:: before structure name.
"
module1::Writer" is so called fully-qualified name for structure
Writer
By inserting line of code:
UseModule module1, in declaration of
module3, then
"
module1::" before structure name
Writer may be left out, meaning that fully-qualified name for structure
Writer is not required in this case
See below:
Code: Select all
DeclareModule module3
UseModule module1
;......
EndDeclareModule
Module module3
Define w1.Writer
;it works because line of code: "UseModule module1 " (where structure Writer is defined)
;is inserted in module3 declaration
;As shown in previous posts,there is no confict between name of the variables w1 from module2 and w1 from module3,
;because they are used inside different modules
w1\name="Mark"
w1\surname="Twain"
Debug w1\name ;=Mark, it works
EndModule