Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# : Classes into Folders

I want to organize my solution by adding folders and organize classes into them
how to access these classes as i tried to access them but can't
folders looks like packages in java

like image 613
Ahmed Ghoneim Avatar asked May 28 '11 15:05

Ahmed Ghoneim


3 Answers

When you create a folder in the Visual C# project it normally creates a namespace for items created in that folder. You need to add a using blah.foldername statement to the c# file where you are trying to use the items from the folder, or you can edit the file in the folder to use namespace blah instead of namespace blah.folder.

like image 197
Brian Lyttle Avatar answered Oct 05 '22 16:10

Brian Lyttle


Visual Studio mimics your project's hierarchy on the hard drive. When you add a solution folder within Visual Studio, it creates a real folder under your project directory. Any new projects or source files you add to the solution folder in Visual Studio will default to that directory. Also, and this gets to the heart of your question, when you add a C# file, i.e., class, to the solution folder, Visual Studio places it in a sub-namespace of your project.

For example, if your project is named MyProject, the default namespace will be MyProject. If you add a solution folder to MyProject called MyFolder, any new files, i.e., classes added to that folder from within Visual Studio will have a default namespace of MyProject.MyFolder. Thus, in order for classes in the MyProject namespace to reference classes in the MyProject.MyFolder namespace, you need to either fully qualify the class name, e.g., MyProject.MyFolder.MyClass or include a using MyProject.MyFolder; statement at the top of file where the class is used.

like image 35
Matt Davis Avatar answered Oct 05 '22 14:10

Matt Davis


If you are asking about solution folders, they don't translate to the resulting code. They are merely a way to organize your projects.

If you are creating folders in your project to separate code files, then traditionally we would have the namespace represent the hierarchical structure of the solution/project.

Then you just include the namespaces as you normally would.

Does this answer your question?

like image 32
BAKeele Avatar answered Oct 05 '22 14:10

BAKeele