Solution Items in Visual Studio 2005/2008 - c#

Is it possible to add a class as a solution item and use it as a linked item in all the projects in the solution?
Basically I was thinking of creating a class (which will inherit ConfigurationSection) and keeping it as the Solution Item. I wanted to add it as a linked item in all the projects in the solution, so that everyone can use it to access the configuration properties.
(Refer to this tutorial for more details)
Now the issue I am facing is that when I create a class in the solution item, it doesn't have any namespace. And it shows up in intellisense, inside the projects but once I create an object of the solution item class, the object doesn't show up in intellisense.
Any ideas why?

You would be better off putting your class into a library and then people can reference your library and use your class. If you use the linking methodology, you will run into problems when one project wants to reference another as you will have the same class in both projects in the same namepsace - it will not compile.
Update
I can see from your other questions that you want to share configuration settings through a config file. Sharing the config file by linking is fine, it is just a plain text file so does not have any compilation issues. However, I would advise against sharing the class file using this method as discussed above. Put your configuration class in a class library project which other people can reference as a project reference - they can then use your class. Add the actual config file (with the settings) using linking. In this way your settings file will get copied across projects nicely, and people can use your configuration settings class by referencing it.

Yes.
Right-click each project, click Add, Add Existing File, navigate to the file, then click the down arrow next to the Add button and click Add As Link.
However, it would be better to put the class in a DLL project that is referenced by all of the projects.

Related

How to use a C# class in many projects inside the same solution

I'm learning C# and I don't know exactly how to make a class visible to all projects inside the Solution. Basically I have many projects, all Windows Forms, all of them have some similarity, so they can share a lot of classes. I think a good approach is put all the shared classes in the Solution folder and make all the apps get the classes from there. Is this a good approach? How to do it? Another way to do it, I think, is to put all the shared classes in one app and the others get that references.
If that is not a good approach, which one is? How to do it?
I'm using Visual Studio 2019. This is how my Solution looks like, but I can't share the classes between the projects:
Create a Class Library project within your solution and put your shared classes in there (right click on your solution in the Solution Explorer, Add, New Project). Then for each of the projects that you want to access the classes from, right click on Dependencies, Add Project Reference and tick the Class Library project you created.
You create a class library project and in there you would want to add folders where you can name them according to what the classes would do so it is easier to maintain. Then you would add your classes in the corresponding folder and in the project that you want to use the class you can add a reference to the project and the class would be accessible as long as it was a public class.

File type required to add a reference to a C# project in Visual Studio (2015)

Very new to C#/VS and this is vexing me - there are a lot of similar questions on SO, but none seem specific enough to help me!
I'd like to use a class from ProjectA in another class (eg ProjectB). I wish to "maintain" the class in the original project, ProjectA, and simply "use" it in Project B (if that makes sense?!)
From what I have read here and elsewhere, I first need to set up a reference to ProjectA within Project B - but this is the stage I am struggling with.
When I follow what I have read (eg on How to use a Class from one C# project with another C# project ) I get to the stage where I am in the "Add References" dialog, and have selected "Projects".
But VS wants me to reference specific "Component" file types (.dll, .tld, .olb, .ocx, .exe) but I can't seem to find a file that refers to "ProjectA". I browse to the folder where ProjectA sits, but no files of the correct type exist. There are files such as:
ProjectA.csproj, ProjectA.sln, ProjectA.suo
but none of these can be selected in the dialog.
Can anyone provide what I suspect will be a very simple answer? Thanks!
In the dialog box for Add Reference you have the option to reference a project within the solution. This will add all its namespace classes and make them available for your use in the other project. You need to make the two projects part of the same solution to be able to do that.

Updating classes used in multiple projects?

I have a few classes that are abstracted in a way that I can use them in multiple projects. I'm always working on these classes, optimizing, adding, etc. So when I optimize something in one of these classes, I then need to copy that new version into every project I remember using it. This isn't a very good way of doing it, but is there a better way?
Thanks
Put these base classes in a single project and share this project between your different solutions as an referenced class library. This way you will not have to copy / paste anything between projects or solutions and everything should always be up to date.
You could even set-up a local NuGet feed so you can use NuGet to retrieve this shared project as a reference in a well structured and managed way.
Instead of manually copying the updated classes to every project that uses them, create a Class Library project and reference the compiled file in every project that uses the classes. Organizing your classes like that will help you to follow the DRY ("Don't repeat yourself") principle.
If you need to reference files instead of compiled libraries, however, you can reference a file as a link so that multiple projects refer to the same file without copying it to each solution folder. To do that, right-click on your project, choose Add existing item..., browse to the .cs file, and choose Add as Link from the combobox in the right lower corner.
How about if you extract the classes into a separate project, and add a reference to this project in every project you are using?
It is a bad idea to copy paste file throughout the application. To avoid these repetitions you can either:
make a link, if the amount of file is really small . In the Solution browser of Visual Studio, right click, Add Existing file, chose your file and in the split button, choose Add as a link
create a separate project and reference this project wherever is is necessary if the amount of files not tiny.
Create a base-lib and build it to a "shared" location. Add a reference to it in you project. It will keep the other projects smaller and will be faster to build.

Visual Studio: Add Item / Add as link rather than just Add

I'm new to visual studio, coming from Delphi.
I have a directory tree full of .cs files (root is \Common).
I also have a directory tree full of Applications (root is \Applications)
Finally, I've got a tree full of Assemblies (root is \Assemblies)
I'd like to keep my .cs files in the Common tree and all the environment voodoo (solutions, projects, settings, metadata, debug data, bin, etc.) in the Assmblies tree. So, for a simple example, I've got an assembly called PdMagic.Common.Math.dll. The Solution and project is located in \Assemblies\Common\Math. All of its source (.cs) files are in \Common\Math. (matrix.cs, trig.cs, mathtypes.cs, mathfuncs.cs, stats.cs, etc.)
When I use Add Existing Item to add matrix.cs to my project, a copy of it is added to the \Assemblies\Common\Math folder. I just want to reference it. I don't want multiple copies laying around. I've tried Add Existing Item, and used the drop down to "Add link" rather than just "Add", and that seems to do what I want.
Question: What is the "best practice" for this sort of thing? Do most people just put those .cs files all in the same folder as the project? Why isn't "Add link" the default?
Thanks!
You can just use Add As Link by clicking on the little down arrow to the right of the add button from Add-->Existing Item command...
Whilst I realise this is not in an answer to the original question (which regards best practices), I present this answer in order to save the time of others who have been directed here by the misleading title of this question.
The "best practice" in this case, is to not fight the tool. It allows you to do what you want, but you'll get more work done and be able to focus on code if you just let the IDE organize your project for you.
I would create an empty solution project called PdMagic.Common
This will give you a file structure like
PdMagic.Common\
PdMagic.Common\PdMagic.Common.sln
then I generally add a src and libs folder (via the file system, not VS)
inside the libs folder, i would place all my third party dependencies, and the src folder would hold all of my projects
PdMagic.Common\
PdMagic.Common\PdMagic.Common.sln
PdMagic.Common\libs
PdMagic.Common\libs\nunit
PdMagic.Common\src
Next, in Visual Studio, I would right click on the Solution I just created, and click "Add -> New Project", I would specify that I wanted it created in the \src folder and call it PdMagic.Common.Math
Now my folder structure would look like this
PdMagic.Common\
PdMagic.Common\PdMagic.Common.sln
PdMagic.Common\libs
PdMagic.Common\libs\nunit
PdMagic.Common\src
PdMagic.Common\src\PdMagic.Common.Math
PdMagic.Common\src\PdMagic.Common.Math\PdMagic.Common.Math.csproj
PdMagic.Common\src\PdMagic.Common.Math\Class1.cs
Then, as you add classes to your PdMagic.Common.Math project, they will go in the folder with the project file. This is how the IDE has the opinion we should work, and I think most developers go with it because trying to get any other layout on the file system would require too much fighting with the IDE. I know it can be hard to come from a different convention, and you instinctively want the same conventions in the new environment. However, if you stick with the conventions, (right or wrong in your opinion) you'll get more done because you won't be trying to force the IDE to do things the way you think they should be done.
I think that the 'best practice' is to have those 'common' routines into an assembly that you can reference instead of pulling the source files into a bunch of different projects. You could add it as a pre-built assembly with "Add reference..." or by including a project for that assembly and adding a reference to the project (also done inthe "Add reference..." dialog).
This is one of those things that seems like a bit of work to set up initially (and it may be), but it generally pays off in the long run.
If you want to "reference" matrix.cs in your project, don't use Add Existing Item, create a reference ("Add Reference") to the class library or object that matrix.cs is a part of. If you don't want (or can't use) the library or object that matrix.cs is part of, then the Add Link is how you would get to it, but seriously I've never used that particular feature.
None of the developers I have ever worked with in .NET isolate their .cs files in separate folders. The only kind of segregation like that that I have seen as a practice is in MVC, where the models, views and controllers are in their own folders.
Because you want to reference matrix.cs in the project, dont just use AddExistigItem, create a new referene to the class.
VS has some useful extensions for this:
http://vscommands.com
and ProjectsLinker: http://visualstudiogallery.msdn.microsoft.com/5e730577-d11c-4f2e-8e2b-cbb87f76c044?SRC=VSIDE
and SourceShare: http://visualstudiogallery.msdn.microsoft.com/aa5d54dd-0b05-4689-ad2f-634e86de327f?SRC=VSIDE
In Visual Studio 2019 it is:
Right click on a project
Add
Existing item
Select file
Expand menu next to Add
Add As Link

Reusing code in .NET website (app_code folder)

I need to use some classes inside the app_code folder of a website project in visual studio 2008. I need to access this code from a class library project in the same solution. I cannot add a reference to the website, and I'm not sure of the easiest way to use the classes that already exist here. Do I need to move it to a class library?
What other options do I have?
Yes, create a class library and move any types you need into that library. This library can be referenced in as many places as you would like.
The best way to do this is to put those classes in their own library.
However, if you really don't want to do that, you could add a link to the files in the library project. To do that, right-click the Class Library project or a folder within it, Add, Existing Item, navigate to the code files, click the down arrow near the Add button, Add As Link. This will add the same file to both projects. You can even use the #if preprocessor directive to limit portions of the file to specific projects.
However, it is vastly preferable to put the code in a library and reference it in the web project.

Categories