|
-
Oct 7th, 2005, 01:34 AM
#1
Thread Starter
Frenzied Member
Namespaces
Stupid as it sound, I so far did not figure out how to use Namespaces properly.
OK, I do understand the benefits, and so on, but when on start out a new project, how do you decide how to use them when you also keep in mind that new classes, migth be used in other projects as well?
Guess I'm kind of slow on "concept" kind of stuff. Took me months after I moved from VB to C#, to truely understand OOP (One thing knowing what it is, another thing to be able to visualize the whole thing). Dig me?
Last edited by StrangerInBeijing; Oct 7th, 2005 at 11:17 AM.
Reason: Resolved
-
Oct 7th, 2005, 01:53 AM
#2
Re: Namespaces
A namespace is simply a conceptual area where each type name is unique. They are generally used to group types that have certain similarities, like Data, IO, etc. When you create a new project it is its own namespace. There is usually no need to use additional namespaces within small projects. If you have additional projects that compile to libraries, you would usually have seperated that functionality because it all shares some similarity, so those projects will all also be there own namespace. Multiple projects within a solution will generally all have the same root namespace, which would represent the solution itself. You would normally only create additional namespaces within an assembly if you wanted to subdivide certain groups, like the System.Data.dll assembly contains the System.Data namespace, but also the System.Data.OleDb, System.Data.SqlClient, etc. namespaces.
-
Oct 7th, 2005, 02:33 AM
#3
Thread Starter
Frenzied Member
Re: Namespaces
So, one can decide for yourself how to name your namespaces?
Say I want to arrange my classes in directories on my disk, starting with a top level folder called CodeLib, subdirectories as Database, etc, I can for instance name a class's namespace in Database CodeLib.Database.TestClass.cs?
-
Oct 7th, 2005, 02:56 AM
#4
Re: Namespaces
Absolutely. In the Property Pages for a project, I would normally set the Root Namespace to <SolutionName>.<ProjectName>. Others may choose to omit the <SolutionName>, particularly if there is only one project in the solution. Every class, and indeed other type as well, that you create within your project is then a member of that namespace by default. If you then want to make a type a member of a child namespace you simply put it inside:
VB Code:
Namespace <NameSpaceName>
End Namespace
and it is then a member of <SolutionName>.<ProjectName>.<NameSpaceName>. How you choose to make those subdivisions is completely up to you. Like I said though, you should be using namespaces to group only types that share some sort of similarity.
-
Oct 7th, 2005, 05:46 AM
#5
Thread Starter
Frenzied Member
Re: Namespaces
 Originally Posted by jmcilhinney
you should be using namespaces to group only types that share some sort of similarity.
...that was my only initial motivation to care about namespaces in this particular case.
Thanks for the help. What I did was to create a sample app and play around with it. Should have done it ages ago.
-
Oct 7th, 2005, 06:20 AM
#6
Re: Namespaces
I generally create namespaces with a starting name as the company name, for 'belongingness'. It's also common practice.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|