Call Function from outside of a module

Just starting out? Need help? Post your questions and find answers here.
User avatar
jacdelad
Addict
Addict
Posts: 1432
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Call Function from outside of a module

Post by jacdelad »

Hello,
the help tells me that I cannot call a function from outside a module if I'm within a module. Ok. I have a module and want to call the errorhandler of the program, if an error occurs. I currently have an own errorhandler, which I want to get rid of, within the module. Is there a way to circumvent this restriction?
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Call Function from outside of a module

Post by NicTheQuick »

You can only create an other Module "Common" which contains your Errorhandler and then call it from your Module. But you can not call anything in the main scope without working with pointers and Prototypes and such things.

Small Example:

Code: Select all

DeclareModule Common
	Declare.i globalFunction()
EndDeclareModule
Module Common
	Procedure.i globalFunction()
		Debug "Error!"
	EndProcedure
EndModule

DeclareModule myModule
	Declare myFunction()
EndDeclareModule
Module myModule
	Procedure myFunction()
		Common::globalFunction()
	EndProcedure
EndModule


myModule::myFunction()
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Call Function from outside of a module

Post by netmaestro »

The way you have it set up now seems correct. If workarounds were possible for crossing the module/caller boundary the result would be convoluted logic and you'd lose the purpose of using modules.
I currently have an own errorhandler, which I want to get rid of, within the module.
My advice would be, stop wanting that.
BERESHEIT
User avatar
jacdelad
Addict
Addict
Posts: 1432
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Call Function from outside of a module

Post by jacdelad »

Well, ok. Just to clear it up: My program writes a log onto a MySQL-Server and I wanted to include my Modules too (in case they encounter an error). That's why I need a more complex and universal ErrorHandler.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Post Reply