Namespaces
Namespaces organize the objects defined in an assembly. Assemblies can contain multiple namespaces, which can in turn contain other namespaces. Namespaces prevent ambiguity and simplify references when using large groups of objects such as class libraries.
The general namespace for ComponentOne Web products is C1.Web. The following code fragment shows how to declare a C1ReportViewer using the fully qualified name for this class:
Dim ReportViewer As C1.Web.UI.Controls.C1ReportViewer
• C#
C1.Web.UI.Controls.C1ReportViewer ReportViewer;
Namespaces address a problem sometimes known as namespace pollution, in which the developer of a class library is hampered by the use of similar names in another library. These conflicts with existing components are sometimes called name collisions.
Fully qualified names are object references that are prefixed with the name of the namespace where the object is defined. You can use objects defined in other projects if you create a reference to the class (by choosing Add Reference from the Project menu) and then use the fully qualified name for the object in your code.
Fully qualified names prevent naming conflicts because the compiler can always determine which object is being used. However, the names themselves can get long and cumbersome. To get around this, you can use the Imports statement (using in C#) to define an alias — an abbreviated name you can use in place of a fully qualified name. For example, the following code snippet creates aliases for two fully qualified names, and uses these aliases to define two objects:
Imports C1ReportViewer = C1.Web.UI.Controls.C1ReportViewer
Imports MyReportViewer = MyProject.Objects.C1ReportViewer
Dim rv1 As C1ReportViewer
Dim rv2 As MyReportViewer
• C#
using C1ReportViewer = C1.Web.UI.Controls.C1ReportViewer;
using MyReportViewer = MyProject.Objects.C1ReportViewer;
C1ReportViewer rv1;
MyReportViewer rv2;
If you use the Imports statement without an alias, you can use all the names in that namespace without qualification provided they are unique to the project.
|