ScriptBasic does not have real name spaces to separate modules from each other. There are no such things as public and private variables of modules or classes. Whenever you develop a module you have to trust the user of the module that he or she will use the module in the way it is intended and the way you hopefully documented. A programmer should not use the internal variables of a module not because he can not but because he is supposed not to. ScriptBasic name spaces only help the programmers to avoid accidental name collisions. When you develop a module you start it saying
After this line until the end of the module which is noted by the line
all lines will belong to the specific module and all global variables will belong to that module.
However all these module and name space handling is done on low level altering the names of the variables and functions. When you start to write a program and you write:
A = 3
you actually use the variable main::A. This is because the default name space is main, and if there is no name space defined in a variable its name is altered so that the variable will belong to the current name space. Try the following:
Example mod1.bas :
A = 3
Result executing mod1.bas :
Surprising? Try the following:
Example mod2.bas :
Result executing mod2.bas :
This is because main::A is 5, boo::A is 3 and the variable name main::A is not converted to boo::main::A, because it is explicitly denoted to belong to the name space main. Name spaces can be nested. You can write:
Example mod3.bas :
Result executing mod3.bas :
To ease the nesting of modules you can write the same code as
Example mod4.bas :
When the module name in the module declaration starts with double colon ScriptBasic knows that the module is to be nested into the current module. The variable name _::A means: the variable A of the surrounding name space. This is the same as the operating system directories. You can think of name spaces as directories and variables as files. Whenever you write
a similar construct may say
When the parser sees the end module statement it always returns to the previous name space. You can start and close modules many times, ScriptBasic will not complain that the module was already defined. You can even totally neglect the module statement and you can write the above program as
This is a bit less readable. Name spaces can serve another purpose. See the following code:
string = "AF"
hex = 0
while string <> ""
chr = mid(string,1,1)
string = mid(string,2)
hex = hex*16 + asc(chr) - asc("A")
when you try to compile you will probably get many errors. Why? Because string, chr and hex are predefined functions and can not be used as variable names. What to do then? You can use them as variable names when you specify that they are variables:
::string = "AF"
::hex = 0
while ::string <> ""
::chr = mid(::string,1,1)
::string = mid(::string,2)
::hex = ::hex*16 + asc(::chr) - asc("A")
When you write ::string, ::chr or ::hex ScriptBasic will know that they are variables belonging to the current name space. This is a bit weird, so you better avoid using function names and predefined function names as variable names.