Program Structure¶
In this chapter we will learn about using many source code files in the same project.
Source Code File Sections¶
Each source code file may contains the next sections (in the same order).
Source Code File Sections |
---|
Include Files |
Statements and Global Variables |
Functions |
Packages and Classes |
The application maybe one or more of files.
Using Many Source Code Files¶
To include another source file in the project, just use the include command.
Syntax:
include "filename.dgn"
Note
The Include command is executed directly by the compiler in the parsing stage
Tip
if you don’t know the file name until the runtime, or you need to use functions to get the file path, just use eval().
Example:
# File : Start.dgn
include "sub.dgn"
sayhello("World")
# File : sub.dgn
func sayhello(cText){
showln "Hello " + cText
}
Include Package¶
Using the ‘include’ command we can use many dgn source files in the same project
But all of these files will share the same global scope
We have also the “include package” command
Using “include package” we can include a library (*.dgn file) in new global scope
This is very useful to create libraries that avoid conflicts in global variables
Example:
File: includepackage.dgn
x = 100
showln "Hello, World!"
include package "testincludepackage.dgn"
showln x
test()
File: testincludepackage.dgn
showln "Hello from testincludepackage.dgn"
x = 1000
test()
func test()
showln x
end
Output:
Hello, World!
Hello from testloadpackage.dgn
1000
100
1000
Include Again¶
Using this command we can include the Dragon native source file which contains constants more than one time.
This is useful when using Dragon native source files for translations through global constants.
Example:
Loading the same file again using the (Load) command is not possible
Because the (Load) command load the same source file only for the first time and ignore next times.
So we have to use the (Load Again) command.
Where we can use these files again during the runtime as in the next code
func setLang( nLanguage){
if C_ENV_DEFAULT_LANG == nLanguage
return
end
C_ENV_DEFAULT_LANG = nLanguage
# Change the language
match nLanguage
case C_TRANSLATION_ENGLISH
include again "translation/english.dgn"
case C_TRANSLATION_ARABIC
include again "translation/arabic.dgn"
end
}