Declares a namespace block
Syntax
Namespace identifier [ Alias "aliasname" ]
statements
End Namespace
Parameters
identifier
The name of the namespace (including nested names specifier).
aliasname
An alternate external name for the namespace.
Description
Namespaces allow to group entities like objects (predefined data-types and UDTs including Union and Enum) and procedures (including their declarations) under a name. This way the global scope can be divided in "sub-scopes", each one with its own name.
Whether or not explicitly declared a namespace in a source file, the compiler adds a default namespace. This unnamed namespace, called the global namespace, is present in every file.
Any identifier in the global namespace is available for use in a named namespace (even global symbols with the same name as keywords may be declared inside a namespace).
Namespaces implicitly have public access and this is not modifiable.
A variable declared inside a namespace is always implicitly static and visible throughout the entire program even if the declaration modifier
Shared is not specified (static and shared are optional, but this may improve code readability).
Namespaces do not have any effect on the visibility of a define.
It is possible to define a namespace in two or more declarations.
Namespaces are commonly used in libraries where you don't want all the symbols from that library to crowd the user's space (called the global namespace).
For example, if you used the "Forms" library, it might define the Point type for describing an X and Y coordinate, and you might also define it for another purpose. This can be resolved by creating the namespace Forms for the library, and then referring to its Point type as Forms.Point, and yours as just Point.
To access duplicated symbols defined in the global namespace use:
.SomeSymbol (or
..SomeSymbol if inside a
With..End With block).
Example
Namespace Forms
Type Point '' A 2D point
As Integer x
As Integer y
End Type
'' Since we are inside of the namespace, Point resolves to Forms.Point.
Sub AdjustPoint( ByRef pt As Point, ByVal newx As Integer, ByVal newy As Integer )
pt.x = newx
pt.y = newy
End Sub
End Namespace
Type Point '' A 3D point
As Integer x
As Integer y
As Integer z
End Type
Sub AdjustPoint( ByRef pt As Point, ByVal newx As Integer, ByVal newy As Integer, ByVal newz As Integer )
pt.x = newx
pt.y = newy
pt.z = newz
End Sub
Dim pt1 As Point
AdjustPoint( pt1, 1, 1, 1 )
Dim pt2 As Forms.Point
Forms.AdjustPoint( pt2, 1, 1 )
Namespaces are GCC C++ compatible, the following code aims to test that.
(cpp)
// mylib.cpp
// To compile:
// g++ -c mylib.cpp -o mylib.o
// ar rcs libmylib.a mylib.o
#include
#include
namespace mylib
{
int test()
{
return 123;
}
}
'' test.bas
Extern "c++" Lib "mylib"
Namespace mylib Alias "mylib"
Declare Function test() As Integer
End Namespace
End Extern
Print mylib.test()
Dialect Differences
- Namespaces are not supported in the -lang qb dialect.
Differences from QB
See also