C# : Classes into Folders - c#

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

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.

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.

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?

Related

Copy references to class library ASP.NET

I created a new class library in my project, and moved my Models folder to that. Now I need to get all the references like EntityFramework, DataAnnotations etc to my new class library.
Is there an easy way to do that(like copying them), without going into Nuget packages and downloading them one by one?
Yes you can easily copy all references from one project to the other. While in VisualStudio double click the project and it should open the .csproj file. (If you are not in VisualStudio Right click edit the .csproj file.) Copy all lines enclosed in a ItemGroup tag which say 'PackageReference' into the 'new' project you copied the models to.
Make sure you also rename all Namespaces of the files you moved so that they match the new library name. Link: how to change namespace of entire project?

how to create folders in a C# class library

I want to build a class library with folders representing a namespace structure. When I import my class library into a project I can only see classes on the root level. No classes within folders are part of the class library anymore. Did I miss something?
In Solution Explorer, press the "Show All Files" button.
Folders not included in the project will now appear, but be greyed out. Find the folders with the missing classes, right click and choose 'Include In Project'.

open a .csproj with monodevelop

I've downloaded a framework with samples in .csproj project format.
How can I open them in MonoDevelop?
I'm interested in using some classes in that framework.
It has a folder structure like: Accord.Statistics.Models and a main folder Accord with a subfolder Statistics with a subfolder Models with a file ModelFoo.cs
I want to use that file (that begin with
namespace Accord.Statistics.Models
) in a MonoDevelop Solution under Ubuntu.
If I copy the folder or single files inside my new Solution I get the error
Are you missing a using directive or an assembly reference?
How can I do?
Create a new empty solution, copy your projects and their sources into the solution folder, then right click on the solution in the solution explorer
("View" → "Pads" → "Solution") and choose "Add" → "Add Existing Project".
Rather than opening the VS project file, you may be better off making a MonoDevelop project file and adding the code files and references necessary (typically Accord.dll, Accord.Statistics.dll, or something along those lines) to your new project. That is, of course, if MonoDevelop doesn't have an import function.
Generally, the "missing using or reference" error comes when you use a symbol defined outside of the current project. If you're actually using something from another assembly, you need to add it as a reference to the project, so the metadata is imported and used to link. If it's from the same project, you may need to import the namespace with a using Accord.Statistics.Models statement.
First of all, I would use a separate solution file for MonoDevelop because MD sometimes puts slightly different settings in there. The same applies to .csproj files, so if you want your project compilable with both VS and MD, watch out that you don't commit any project file changes that don't work in VS.
Regarding your problem: Remove the references using MD and re-add them. MD adds references in a way that they're compatible with both VS/MD (my experience).

How to compile cs files into separate dll

I have a class library and cs files which are for different objectives. One is extension class, the other is windows form control the other is asp.net control, etc.
I want to compile all these cs files into a different dll.
PS: Some of them will need more than one class files maybe.
You may try command line compilation. (Working with the C# 2.0 Command Line Compiler)
csc /target:library /out:Something.xyz *.cs
I know this sounds too obvious, but if you want to compile them into seperate DLLs, why don't you create a project per assembly? So that's a project for the extension classes, one for the asp.net controls etc...
You have to create different projects in your solution (assuming you work in Visual Studio).
Each project can have multiple (class, resource, form, etc.) files and will be compiled into different assemblies (dll's). For each project you can specify settings (assembly name, target framework, etc.).
Classes from different projects can "use" each other by making references from one project to the other. Also, different projects can specify the same namespaces so that you can structure the aplication to your own wishes.
See Structuring Solutions And Projects
All *.cs in a single class library project will compile into the same DLL, you can not split them into individual dlls. If you want a seperate dlls for each class then each should be in a seperate class library project.

web userControl

I have create a new empty web application in c# Asp.net 4.0 and then added App_Code folder then added 3 classes into that folder. I have added a web user control and set the reference of App_Code class into the control but it throws an error.
The type or namespace name 'App_Code' does not exist in the namespace (are you missing an assembly reference?)
Any ideas?
This point can be confusing.
A "web application project" is not expecting the App_Code folder.
A "website project" would expect that folder and know how to use it.
Whether you realize it or not (and depending upon which version of Visual Studio you use), you will get a WAP or a WSP when you first create your web solution. They are different. It takes some effort to convert from one to the other.
Here are a few articles explaining the differences.
To solve your immediate problem, then, you can just move the 3 class files out of the App_Code folder into the root folder, or better, create a new folder to contain those classes and move them into it. Depending upon whether you give the classes in that folder namespaces, you may have to add a reference to that namespace in your UserControl.
Did you create the App_Code folder manually?
I was the impression the folder was created automaticaly when creating the project.
If not, when you add something to the project you select Add ASP folder, and it would have App_Code option along others there.
If the option isn't there is probably because you created a Web Application.
This might help you more: http://vishaljoshi.blogspot.com/2009/07/appcode-folder-doesnt-work-with-web.html

Categories