ComponentOne WebReports for ASP.NET: ComponentOne WebReports for ASP.NET Overview > Namespaces

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 namespace for the C1WebReport control is C1.Web.C1WebReport. The following code fragment shows how to declare a C1WebReport control using the fully qualified name for this class:

      Visual Basic

Dim webreport As C1.Web.C1WebReport.C1WebReport

      C#

C1.Web.C1WebReport.C1WebReport webreport

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.

For example, if you create a new class named NavigationBar, you can use it inside your project without qualification. However, the C1.Web.C1WebReport.dll assembly also implements a class called NavigationBar. So, if you want to use the C1WebReport control in the same project, you must use a fully qualified reference to make the reference unique. If the reference is not unique, Visual Studio .NET produces an error stating that the name is ambiguous. The following code snippet demonstrates how to declare these objects:

      Visual Basic

' Define a new NavigationBar object

Dim MyNavBar as NavigationBar

 

' Define a new C1WebReport.NavigationBar object.

Dim NewNavBar as C1.Web.C1WebReport.NavigationBar

      C#

// Define a new NavigationBar object

 NavigationBar MyNavBar;

 

// Define a new C1WebReport.NavigationBar object

C1.Web.C1WebReport.NavigationBar NewNavBar;

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:

      Visual Basic

Imports NavigationBar = C1.Web.C1WebReport.NavigationBar

Imports MyNavBar = MyProject.Objects.NavigationBar

 

Dim C1 As NavigationBar

Dim C2 As MyNavBar

      C#

using NavigationBar = C1.Web.C1WebReport.NavigationBar;

using MyNavBar = MyProject.Objects.NavigationBar;

 

NavigationBar C1;

MyNavBar C2;

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.

Note that as a warning about the code examples of this documentation, it is taken for granted that the

      Visual Basic

Imports C1.Web.C1WebReport

      C#

using C1.Web.C1WebReport;

statement has been specified. This applies only to small code samples. Tutorials and other longer samples will specify complete qualifiers.


Send comments about this topic to ComponentOne.
Copyright © 1987-2010 ComponentOne LLC. All rights reserved.