[C#] Namespaces

Associate
Joined
1 Feb 2006
Posts
1,868
Location
Reading
I need some help understanding how to utilise namespaces.

I'm doing a project to make a poker AI.

I have a solution called Poker.

I have a project called Engine. This is the game exe

I then have multiple projects for each type of AI I make which compile to assemblies.


Ideally I want them all to be under the same namespace "Poker" so they can all use the same types etc. E.G. Poker.Engine for all types and classes to do with the game engine and Poker.AI for all AI classes.

Can namespaces span multiple projects?

Is it possible to set it up how I want? I initially tried setting the namespace for each project to "Poker" which didn't work!

Any help appreciated
 
Last edited:
Yup, just name them how you want. They dont have to follow the structure of the site in your development IDE or file structure but it makes sense to do so for your own sanity and in case anyone else needs to look at it.

Name the main exe as you say something like Poker.Engine and then create a class library with the namespace Poker, and create folders in that project as required, eg. a folder called AI would be Poker.AI then just save your classes under there. Make sense?

What IDE are you using?
 
You certainly can use the same namespace across multiple projects, it's effectively a unique identifier for your class.

Best practice would be to start your namespace with something specific to you, i.e. your name as an individual or a company name if you're a business.
This is just so you don't get naming conflicts, i.e. imagine if you used a third party DLL in your Poker application which also used the namespace Poker. You could run into some problems.
So, I'd set your namespace as YourName.Poker.whatever

What exactly didn't work when you set the namespace of all to Poker?
That should be fine as long as you don't have conflicting class names within the namespace.
 
Thanks gents.

Had a little play around and basically I was confusing namespaces and references. I thought If I kept everything under one solution and namespace all projects could share their classes/types. But of course you have to reference the projects with each other but that caused circular references.

So just had to rejig my class structure to get around the circular references.

:)
 
Back
Top Bottom