Confusion over namespaces


E

Epetruk

Hello,

I have a solution with two projects.



One of the projects is called MyProj with a root namespace called
MyProj.Obj.

The single source (vb) file for MyProj has a class called Obj. There is no
enclosing namespace, i.e. the class declaration is at the top of the file.

The second project is called MyProjUser and has a reference to MyProj.

The single source file for MyProjUser has the following imports statement:

Imports MyProj.Obj



Then I have a statement further down in MyProjUser like this:

Dim o as Obj

But the compiler doesn't like it. It says that Obj isn't defined.



The compiler is happier when I either have this Imports statement:

Imports MyProj.Obj.Obj



or with this statement in code:

Dim o as Obj.Obj.



What's happening here? I thought that once I imported the namespace (in this
case MyObj.Obj), then all classes in that namespace (including Obj) should
be usable without further qualification.

Or is the fact that part of the namespace and the class name are the same
(i.e. Obj) causing confusion?

TIA for any answers.
 
Ad

Advertisements

A

AMDRIT

Think of namespaces and objects as files and directories on your harddrive

your namespace is akin to c:\myproj\obj
and inside that you have created a file called obj.

in order to access your file, you must specify its full path
c:\myproj\obj\obj

a way that I use namespaces is to break the assemblies into thought areas,
i.e

mycompany.myproject.

data libraries would go into mycompany.myproject.datalib
graphic libraries into mycompany.myproject..graphics
UI components into mycompany.myproject and
mycompany.myproject.customcontrols.

Additionally, when I create an assembly with a sepreate namespace it is
generally because the project itself can potentially be reused by other
assemblies without modification, otherwise, the code would all live with the
executing assembly.

for example

Our encryption library is mycompany.encryption
Our xml, adodbo, and csv wrappers live in mycompany.data
Our generic containers such as sorted bindinglist(of T) live in
mycompany.generics
Our login UI components, security model all live in mycompany.security and
mycompany.security.ui

Finally, we then reference these libraries when designing an application

Accounting package base namespace is mycompany.accpac

imports mycompany.accpac
imports gencol = mycompany.generics

UI components import
imports mycompany.accpac.business
imports mycompany.accpac.UI

Business Objects import
imports mycompany.accpac.data
imports db = mycompany.data
imports enc = mycompany.encryption
imports sec = mycompany.security

add system references (i.e. xml, data, text, collections) as appropriate
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top