ComponentOne True DBGrid for .NET (2.0) Search HelpCentral 

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 Windows products is C1.Win. The namespace for the True DBGrid control is C1.Win.C1TrueDBGrid. The following code fragment shows how to declare a C1FlexGrid control using the fully qualified name for this class:

·      Visual Basic

Dim tdbgrid As C1.Win.C1TrueDBGrid.C1TrueDBGrid

·      C#

C1.Win.C1TrueDBGrid.C1TrueDBGrid tdbgrid;

·      Delphi

var tdbgrid: C1.Win.C1TrueDBGrid.C1TrueDBGrid;

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 Split, you can use it inside the project without qualification. However, the C1TrueDBGrid assembly also implements a class called Split. So, to use the C1TrueDBGrid class in the same project, a fully qualified reference must be used to make the reference unique. If the reference is not unique, Visual Studio .NET produces an error stating that the name is ambiguous.

Fully qualified names are object references that are prefixed with the name of the namespace where the object is defined. Objects defined in other projects can be used if a reference to the class is created (by choosing Add Reference from the Project menu) and then the fully qualified name for the object can be used in the 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, use the Imports statement (using in C#, uses in Delphi) to define an alias — an abbreviated name that can be used 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 C1Split = C1.Win.C1TrueDBGrid.Split

Imports MySplit = MyProject.Split

 

Dim c1 As C1Split

Dim c2 As MySplit

·      C#

using C1Split = C1.Win.C1TrueDBGrid.Split;

using MySplit = MyProject.Split;

 

C1Split c1;

MySplit c2;

·      Delphi

uses C1.Win.C1TrueDBGrid, MyProject;

 

type C1Split = C1.WinC1TrueDBGrid.Split;

type TMySplit = MyProject.Split;

 

var

  c1: C1Split;

  c2: TMySplit;

If the Imports statement is used without an alias, all the names in that namespace can be used without qualification provided they are unique to the project.

As a warning, unless specified explicitly in the code, it is taken for granted that the Imports statement has been specified. 

·      Visual Basic

imports C1.Win.C1TrueDBGrid

·      C#

using C1.Win.C1TrueDBGrid;

·      Delphi

uses C1.Win.C1TrueDBGrid;

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


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