how to create folders in a C# class library - c#

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'.

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?

Using Namespaces in Other Projects

As the screenshot demonstrates, I have a project SampleApp that uses the namespaces EDAM and Thrift. I want to replicate the functionalities of SampleApp to another project.
Do I have to include the two projects (EDAM and Thrift) in my other project? Can I just copy the folders instead of including the actual project files? Can I just convert them to some sort of DLL or something?
You definitely should add references to the EDAM and Thrift projects from the SampleApp project.
To do so, follow these steps:
Right click the 'References' folder in the SampleApp project.
Select 'Add Reference...'
On the popup, go to the 'Solution' tab.
Select the EDAM and Thrift projects.
Under no circumstances should you just copy the files.
Don't copy the folders, just add references to the projects. Even if the projects are class libraries you don't want to copy the DLLs, instead you should add references, just in case your DLLs are updated:
How to: Add or Remove References in Visual Studio
If the "other" project is in the same Solution, you should be able to reference EDAM, Thrift and even SampleApp from that project much in the same way you set the references up for SampleApp.
If this "other" project will be in a new solution...I'd have to do a little research and testing.
Visual Studio project references are equivalent to referencing an assembly directly, but it has a great advantage: when you build a project, Visual Studio take cares of building its dependent projects too.
In addition, Visual Studio will prevent circular references.
There're many other pros, but it's a good summary.
Copy-pasting the code files isn't importing a namespace: this is duplicating code! And referencing the assemblies directly is a waste of time and features!

Convert a completed project to a DLL

How can I convert a completed C# project to a DLL, in order to use it in other projects?
I have Googled but lots of results say to open the Class Library, write your code there, then Build Solution and everything will be ok.
But my question is: how can I convert a completed project to a DLL? The project can include lots of Forms etc.
if your code is complete, you need to create a Class project out of it, if you already have a project then only transfer the useful code to the class project for reuse in other projects
or change the Output Type to class library, you can find that in your project properties under the tab application
If you are using VS2010, go to your solution in Visual Studio,
Click the 'Project' tab
Select 'Project Properties' down at the bottom of the menu
Now in the 'Properties' window click 'Application'. This should show you a menu
On this menu, select the 'Output type' as 'Class Library'
Now when you compile the project you will get your output as a DLL (.dll) in the relevant bin folder.
I hope this helps.

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

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
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?

Categories