index > XNA Framework > Building a Class repository but I have Namespace issues.

Building a Class repository but I have Namespace issues.

You'll have to forgive the no doubt "simplicity" of this question but please bear with me.

I am currently writing lots of small projects to learn different aspects of how C# and XNA both work. This is pretty much the first major involvement in a class based language that I have really dug into and it is certainly one where I have encountered namespaces and had to take notice of them.

My plan is to build a set of classes that any new project can have access to (gameObjects, cameras etc...), ideally in a central location to all projects. Every class I create has a Namespace associated with it which means every time I try and use it from a different project, I have to add the class to the project and then change the Namespace.

Whilst that works, I am sure many of you are saying "you do what??? are you mad???" and the answer is yes, probably.

So, my question is, how simple is it to set up this central repository of classes and make them accessible to every project, without adding them and renaming the Namespace every time? Can I directly specify a secondary Namespace within a project so that I can access these classes?

 




Worms Forts 10/10, MotoGP3 10/10, Worms 10/10, Rogue Trooper 10/10 - Thank you Total Mobile :D
LeeC22
Sounds like you want to create a class library. Add a reference to the library DLL to every project and import the library namespace. Don't use a separate namespace for each class. Done.
Jim Perry

Thanks Jim,

Just before coming back on here I was having another browse through the C# help and managed to find a bit on Namespaces. Adding a using myClasses to the top of the project solves the problem... I can now maintain a set of generic classes with a myClasses Namespace which is what I was trying to do.

I know even less about library DLL's, perhaps that's something I can look at as I get more used to this system.

Thanks for your help though, like I said, it was probably too simple for some people to answer... we've all got to start somewhere though.




Worms Forts 10/10, MotoGP3 10/10, Worms 10/10, Rogue Trooper 10/10 - Thank you Total Mobile :D
LeeC22
The only problem with doing this is if you make a change to one of the classes you have to recompile every game that uses it. If the classes are in a class library, you just have to recompile the library. You might want to look into it sooner rather than later.
Jim Perry
The fact that the compiled library classes end up in DLL's is just a detail. From your point of view, all you have to do as add the library project (let's call it "myLibrary") to the solution of whatever you're creating (let's call it "myGame"), and then add a reference in the "myGame" project to the "myLibrary" project. You add a reference by right clicking on the "References" icon for the "myGame" project in the Solution Explorer view. Once you do that, the output for "myLibrary" (which is a DLL), is copied into the "bin" directory for "myGame" when you compile.
Hopefully this all made sense. Once you try it once you'll see how easy it is.



www.threesixbox.com - Share your Xbox 360 and XNA Projects
Derek Nedelman

Jim: Yeah, I discovered that problem last night as I went through some of my projects... I knew there had to be a catch.

Derek: Let me see if understood that....

I create a new 'Class Library' project called myLibrary.

I add my generic classes (gameObject, Camera etc...) to that project (with the myLibrary Namespace in each class file?) and then compile the project which will output a DLL.

In my other projects, I create a reference to the myLibrary Project (not the DLL?).

Is that right?

If it is, do I still need to add a 'using myLibrary' statement to the project or does this technique make the Namespace side of it redundant?

Thanks for your help both of you, I always find myself bogged down with this kind of stuff and I usually end up that frustrated that I give up. I do tend to end up with numerous projects so this will save loads of time and messing about, thank you.




Worms Forts 10/10, MotoGP3 10/10, Worms 10/10, Rogue Trooper 10/10 - Thank you Total Mobile :D
LeeC22
 LeeC22 wrote:

I create a new 'Class Library' project called myLibrary.

Yes

 LeeC22 wrote:

I add my generic classes (gameObject, Camera etc...) to that project (with the myLibrary Namespace in each class file?) and then compile the project which will output a DLL.

Correct, though if you add the myLibrary project to your game's solution, you don't even have to build the library separately. When you compile, Visual C# internally builds a list of dependencies and will compile whatever projects need compiling, and it will do so in the proper order.

 LeeC22 wrote:

In my other projects, I create a reference to the myLibrary Project (not the DLL?).

Since you wrote the library and have the source code and project file, then yes, you would want to add a reference to myLibrary. Before doing this you must add the myLibrary project to your game's solution (the overall "thing" that contains your game project), otherwise there will be no way to add the reference. In cases where the DLL is from a 3rd party and you don't have the source code, then you would add a reference to the DLL directly.

On a side note, we keep saying "DLL". Just so we're entirely clear, we're really talking about .NET assemblies, which happen to be stored in DLL's.

 LeeC22 wrote:

...do I still need to add a 'using myLibrary' statement to the project or does this technique make the Namespace side of it redundant?

You still need to add "using myLibrary".

Good luck.



www.threesixbox.com - Share your Xbox 360 and XNA Projects
Derek Nedelman

Derek: Thank you, that's fantastic help.

Hopefully one day I can repay the assistance in kind... probably not code wise but perhaps my other game development knowledge may provide a useful answer some day.

Cheers
Lee

P.s. I did have to do a couple of minor tweaks, a Class Library doesn't contain references to any Microsoft.XNA stuff so I had to add that to the project and then I just had to make my classes public. Everything now works fine so again, thank you for your help.




Worms Forts 10/10, MotoGP3 10/10, Worms 10/10, Rogue Trooper 10/10 - Thank you Total Mobile :D
LeeC22
reply 8

You can use google to search for other answers

 

More Articles

• Bad Implementation of Texture.FromFile
• BUG: Device Created/Reset etc not called back
• Speech / Voice API?
• A couple of stupid questions (about Timer's etc)
• Spritebatch.Draw() with an origin
• Sharing projects that use 3rd-Party or Community-Shared component...
• Where to Find XNA Documentation, Including Tutorials and Samples!
• Video/Animation Playback
• Mesh Classes?
• Custom Vertex and Transformed vertices.
Bookmark and Share
Welcome to Bokebb   New Update  
 

New Articles

• Compiling from command line.
• XInput Common controller 32 bit wont ins
• Bad Implementation of Texture.FromFile
• WOW!! Difference between running the EXE
• Next Beta (SKD)
• How do I next GameComponents?
• Video Playback
• Normals
• how do i create/edit .fx files?
• GenerateMipMaps
• How do I change the opacity of a sprite?
• Post 2D whole screen 'filters'?
• PDC 2006 Xna framework ?
• Control as RenderTarget
• XNA Framework Licenses

Hot Articles

• Microsoft.Xna.Framework.Storage Namespac
• Can I load *-x files with UV textures?
• Reference Rasterizer
• Components w/ different timings?
• Content Access
• XML, XNA & Dll's Oh My...
• Game Components, Game.GameServices and G
• Feedback for Mitch W
• When is the content pipeline coming?
• Are these videos still usefull?
• Big Help and FAQ Post, also contains cod
• Min. Requirements to PLAY
• Game Programming is very Hard
• Proper use of GameComponent? (yet anothe
• Game Component Designer Problems

Recommend Articles

• Is it possible to improve the BoundingSO
• Generics.
• MouseCursor Component
• In the final release of XNA i would like
• texture quality
• Ray Ecuation? (collision-response)
• Draw() called before Update()
• Saving screenshots
• Problem with adding color value to custo
• Talk about being behind the curve
• Custom Vertex and Transformed vertices.
• Transparent Texture2D
• Xna Pro, C++/CLI, and trusted developers
• Keyboard Text Input
• Relative costs of state changes