We help IT Professionals succeed at work.

About namespace

william007
william007 asked
on
Hi, I am new to C#.

To create a new namespace, do we need to click any thing on the visual studio 2005 toolbar, or just simple rename the existing namespace to
namespace newname{


}

this is due to when I simple put a new name, it shows me "Options to update references to rename object".
What happen? What is the correct procedure to put a new namespace?
Comment
Watch Question

just rename it :)

Author

Commented:
May I know rename what? I wish to create many namespaces...how to do that?
yeah.... bcoz the name space you are going to change is being used in other socurce code files... so it need to update all the reference in other code like
namespace myspace
{
 class  A  
  {
  public int a ;
  // some code here
  }
}
in other code file
{
myspace.A obj ;
A.i = 3 ;
}
some thing like dat
so it needs to update namespace references. as well.

Author

Commented:
I see, so when I add a new namespace, in the solution explorer, will it show the namespace information? If no, where can we find it?
(Actually I just want to have a high level view of each file contain what namespace, instead of one my one open them)
well... i dont have VS now .. but i guess...

i think Class view explorer ll show you namespaces..

if the changed namespace is being used by any other sourcecode file then it ll show update option,else you can simply change..no update required.
Commented:
When you type "using System.Drawing" etc. System.Drawing is a namespace. When you write you're own namespace it is possible (and if you are working with a form, almost certain behind the scenes) that another file has got "using YourNamespaceHere" in it (or in the case of WinForms, the generated file needs to be in the same namespace) so it gives you the option to change those accordingly.

If you are changing the namespace of the main file you probably want to go into the project's Properties and change the default namespace accordingly, though I think the "Options to update references to rename object" might do this for you, not sure.

Other than that you can rename the namespace to what you want and just select to update other references usually.

As for information about a namespace, you want the Object Browser (it's under View->Other Windows in VS2005/8 Express and View in Standard and above)
Top Expert 2007
Commented:
Usually, the namespace corresponds to the assembly name. So if you add a new project to your solution, you are creating a new assembly, and with it a new namespace.

Jim

Commented:
For readibility of your namespaces I usually use the dot notation.  So in my solution I have say 4 assemblies with namespaces like this

Solution is called MobileCentaur

Then namespaces in assemblies

MobileCentaur.BusinessObjects                                
MobileCentaur.CentaurApp
MobileCentaur.DAL
MobileCentaur.CentaurSvc

Etc  That way your object relationships become clearer

Author

Commented:
Thanks:)

Explore More ContentExplore courses, solutions, and other research materials related to this topic.