Creating a function alias for included modules

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
SHughes_Fusion
Posts: 219
Joined: Wed Sep 11, 2013 1:27 pm
Location: Chesterfield

Creating a function alias for included modules

Post by SHughes_Fusion » Wed Mar 19, 2014 2:40 pm

I'm writing a module where I want to be able to alter the means by which data is output. For example, to a screen, over the UART etc.

I have tried the following but get an error - can I do what I'm trying to do and if so where have I gone wrong?

The main program includes:

[code]
#include "BTComms.bas"
Public Dim XMLWrite as BTComms.BTWrite
#include "XML.bas"
[/code]

The BTComms.BTWrite is a compound sub which outputs whatever is thrown at it over a Bluetooth link.

My XML.bas module includes functions such as:
[code]Sub XML_Root(open_close As Boolean = true)
XMLWrite("<")
If open_close = false Then XMLWrite("/") EndIf // If called with false then create a close tag
XMLWrite(XML_ID_Out)
XMLWrite(">")
End Sub [/code]

However, when I try and compile my code (the main code, not the modules) I get an error 'Identifier Not Declared : XMLWrite'. I would guess I could set up a number of #if isoption tests within the xml.bas module but I'd prefer to be able to define my output routine separately from the module.

SHughes_Fusion
Posts: 219
Joined: Wed Sep 11, 2013 1:27 pm
Location: Chesterfield

Re: Creating a function alias for included modules

Post by SHughes_Fusion » Wed Mar 19, 2014 2:54 pm

Actually, another, related question. Can I include a file in my main program which is a series of Public Dims which is then passed over to another module?

Also for the XML module I want to define various tags but these will vary based on the application. I want the actual XML creating and parsing module to be generic if possible so I want to define these in a separate file.

However, this seems to have the same problem - despite being declared as public they don't seem to be shared with other modules. Should public declarations share across modules, or will a public declaration only be visible to a module if it is made in the program which includes the module?

I guess one solution would be to have a specific file for the tags (XMLTags.bas say) which you have to have a customised copy for each program which uses this module. Not ideal but unless I'm just doing the above wrong it would seem the only solution?

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: Creating a function alias for included modules

Post by Jerry Messina » Wed Mar 19, 2014 5:27 pm

Should public declarations share across modules, or will a public declaration only be visible to a module if it is made in the program which includes the module?
They are not shared. Declaring something public doesn't give it global visability all by itself... the item has to be "seen" by the module accessing it.

Looking at your first example (BTW - it's "include" not "#include")

Code: Select all

include "BTComms.bas"
Public Dim XMLWrite as BTComms.BTWrite
include "XML.bas"
XMLWrite can't be seen by XML.bas. For that to happen, you'd have to make a new module declaring XMLWrite and include that...

XMLutils.bas:

Code: Select all

module XMLutils

include "BTComms.bas"
Public Dim XMLWrite as BTComms.BTWrite

end module
XML.bas:

Code: Select all

module XML
include "XMLutils.bas"

Sub XML_Root(open_close As Boolean = true)
	XMLWrite("<")
	If open_close = false Then 
		XMLWrite("/") 
	EndIf
	XMLWrite(XML_ID_Out)
	XMLWrite(">")
End Sub

end module
main.bas:

Code: Select all

include "XMLutils.bas"		// if you want to call XMLWrite from main
include "XML.bas"
Because of this, I usually end up with very little declared in main since no one can "include main.bas"

SHughes_Fusion
Posts: 219
Joined: Wed Sep 11, 2013 1:27 pm
Location: Chesterfield

Re: Creating a function alias for included modules

Post by SHughes_Fusion » Thu Mar 20, 2014 8:38 am

Yes, sorry, was typing it from memory not copying and pasting. Not sure why my code tags didn't work though, unless it was that?

I'd sort of assumed that if you declare something as public before you include a module then it would be visible to that module...

So the solution is basically what I proposed, you need to create a custom 'configuration' file for each application which is included in the XML module? I am planning to have 'default' tags declared outside the module anyway so I can just add the necessary code here.

Thanks, Jerry!

Post Reply