Class Library Namespaces

andrew1980

New member
Joined
Oct 18, 2007
Messages
2
Programming Experience
3-5
I've got a solution file containing 2 class libraries as shown below.

TestSystem
|-------Business Logic { JobLogic.vb }
|
|-------DataAccess { JobAccess.vb }

I want to be able to reference JobAccess class from the JobLogic class without having to qualify the full namespace. I've added an import statement, but I still need to qualify the class name when creating an object. I want to be able to simply write Dim x as JobAccess instead of just Dim x As DataAccess.JobAccess. Here is the code:

Imports DataAccess.JobAccess

Public Class JobLogic

Public Function GetFullList() As DataSet
Dim ja As JobAccess = New JobAccess
Return ja.GetList()
End Function
End Class


I'd like to put both class libraries in a single namespace, but I don't know how to achieve this in VS.

Any help would be much appreciated.

cheers.
 
Your Import statement is wrong. You've imported the DataAccess.JobAccess class, which means that you don't have to fully qualify and members of that class. Nowhere have you imported the DataAccess namespace, so you still have to fully qualify members of that namespace.

You don't put class libraries in namespaces. A namespace is a logical separation of type names, while an assembly is a physical separation of types. An assembly can contain types that are members of more than one namespace. Members of one namespace can be declared in multiple assemblies. There is no specific relationship between an assembly and a namespace. It is common practice to use the name of the assembly as the root namespace, but that is by no means required.

Here's an example. I create a class library named System.Data.dll. I set the root namespace of that library to System.Data, which means that all types declared in that assembly will be under that namespace. I declare a class named DataTable in that assembly, so its full name is System.Data.DataTable. I also do this:
VB.NET:
Namespace SqlClient

    Public Class SqlConnection

        '...

    End Class

End Namespace
That class's fully qualified name is then System.Data.SqlClient.SqlConnection. Does this sound anything like the actual .NET Framework, which does have these assemblies, namespaces and types?

Now, let's say that you have two assemblies named TestSystem.BusinessLogic.dll and TestSystem.DataAccess.dll. If you want all the types in both of those assemblies to be members of a namespace named TestSystem then you set the root namespace of each project to TestSystem, instead of the defaults of TestSystem.BusinessLogic and TestSystem.DataAccess. Alternatively you could set the root namespace to an empty string and then wrap every type in a Namespace block, but you'd only do that if you wanted more than one top-level namespace in the one assembly.
 
Back
Top