using the class in the project group in all projects - c#

I have small projects that are alike, using the same class,
I keep the same file in every project, But when I want to make a change, I have to visit all my projects and update them.
While developing a project in Delphi, I would put my related class in just one directory , I would just update in that class and build all of them, all projects would be updated at the same time
how do i do this in visual studio , I made a project group, I added a class to the group, but the projects in the group cannot access this class. How should I use a method?
common class I will use in all projects , 2nd and 3rd other projects, there is more

You should start with a class library (dll) project with your shared code, then add reference to that project/library on the projects you'll need to consume it.
Here's a Microsoft tutorial about this:
https://learn.microsoft.com/en-us/dotnet/core/tutorials/library-with-visual-studio

You can create new project of dll library. This library can be referenced by all other projects. This is way how you can use the same interface in all other projects.

Related

WPF Application = Exe + DLL + Custom Visualizer ? Can I do all 3 of them?

As the title says, I have a WPF form that I want to build in 3 separate projects:
A Windows application, used standalone by clicking the .exe file
A GUI control that can be imported and used in some other projects (dll)
A Custom Visualizer, that can also be used to view a collection of some specific types (dll)
The interface and all the functionality will be the same(identical) between the 3 projects
My questions are the following:
Can this be achieved by making only one project? Or do I have to make 3 separate projects?
If there is not other way but to make 3 separate projects, how can I have the logic for the controls in a separate project common for all of them?
All the logic is now in the controls events (MouseWheel, button click etc)
I need to use net framework 4.7.2 (or 4.8). Can I make all the projects with this? Or can I also use .net 6.0 on some of them?
Thank you
In such case, you can use a Shared Project. Add a Shared Project (case-sensitive) to your solution and reference that project from other pojects. Then you can access the code inside the Shared Project from other projects as if the code belongs to each of other projects.
Shared Project is often explained in the context of sharing the codes among different platforms but it could be useful for other purpose as well.

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.

How to reference and use same class library in two WebApps in single solution file?

I have a single solution file that has two asp.netmvcWebs called AspnetMvcWebApp1 and AspnetMvcWebAp2.
After I created new class library called BuinessFunctionalityClassLibrary in the same solution file, the solution includes AspnetMvcWebApp1, AspnetMvcWebAp2 and BuinessFunctionalityClassLibrary.
I want to reference and use BuinessFunctionalityClassLibrary in both my WebApps, AspnetMvcWebApp1 and AspnetMvcWebAp2. The idea is: If I want to add any functionality to my program I need just modify BuinessFunctionalityClassLibrary.
Is it doable? If so how can this be achieved?
My environment: Visual Studio 2017 and C#, Asp.NEN MVC5 and .NET Framework 4.7.

Where do shared class files belong in Visual Studio?

I'm writing a program in C# that has two classes, Foo and Bar. I have two executables that will use them, GenerateFooBar and SearchFooBar. My question is how should my solution be partitioned? From what I can gather, everything can live in one solution and each executable should have it's own project. Should Foo and Bar
share a project?
each have their own projects?
go with one of the other projects?
something else?
Thanks for the help! I'm coming from Java if that's helpful.
In your solution, create project called for example Common, and put all your classes and businbes logics there. Set project type as Class Library
Next add your other projects and in them Add Reference to Solution Common project to use it.
If multiple users are meant to work with the shared library, the best solution will be to create the local nugget
Create a shared project and put Foo and Bar into it. Reference that project from both executable projects.
If you later have many developers depending on the shared library you might make it into a Nuget package that you publish and have them depend on that.
From what I can gather, everything can live in one solution and each
executable should have it's own project.
Yes, you are right.
how should my solution be partitioned?
Totally depends on you and how you would like to manage it.
You could create one project, multiple projects, each separated projects, etc.
But, to reuse code as much as we can and to give a more structured way. Here is what you can do.
Make one Project which will hold the ApplicationServices, Helpers, etc eg: Foo and Bar.
Make Another Project which will hold the application itself (Could be two projects one for search and one for generate). You can add reference of service project to your foo and bar projects and to any other project you create in future.
So in the end you will have 2 or 3 projects (depending on you)
1- ApplicationServices
2- Generators/GeneratorServices/GenerateApplications/anyname
3- Search/SearchServices/SearchApplications/anyname

Is there a way to create a class thats can be called accross more than one project in C#

I'm trying to do this so that I can access a certain part of the other linked project in all other projects. right now when I switch between projects I can only land on their entry points.
I have added a reference from my main project to two other dependent ones
Put the common parts in a separate DLL (aka "Class Library" project in Visual Studio). Then add references to that project to the other projects where you need to use them. Don't forget to make the common classes public.
In such case mostly people add one common project in there application and do stuff there which can be used in any other project to avoid the circular reference.
and of your public class will be available to any other project.

Categories