Shared source folders as an alternative to class libraries in C# - c#

I'm wondering if its possible to set up and share an entire folder of source code between multiple projects in Visual Studio 2012 like how eclipse lets you use multiple source folders. I'm currently developing an entity framework model for a game I'm making with split server and client code, said framework currently sits in a class library referenced by both projects.
The reason I'd like to use a shared code folder instead is to add code to each entity component type by declaring the classes in the shared folder as partial and having another partial class in the client/server projects that adds sided functionality. I thought that achieving this would be more useful than extending each component on both sides, and it would also eliminate the need to have a DLL included with the product.

you can add a project to your solution (right click Solution -> Add -> Existing Project) and reference it (right click on your project -> Add Reference -> Solution -> Projects -> "Project you want to reference")

You can right click on a project and select - Add - Existing item.... Within the open file dialog select the desired file, click on the arrow next to the Add button and afterwards on Add As Link
(source: modbusdriver.com)

Related

How to use a class of one project in another project under the same solution?

I have two projects of Web API and Windows Forms App under one solution.
The names are:
Solution - CliendAddress
Web API - ClientAddress
WFA - ClientAddressWFA
In the ClientAddress project there is a class called ServiceResponse. How can I use this ServiceResponse class in my project ClientAddressWFA?
From my ClientAddressWFA, I've already added the reference to ClientAddress ๐Ÿ‘‡
However, when I am trying to add using ClientAddress.Models; (<---- This is where the ServiceResponse class is) in my ClientAddress.WFA project, I'm getting an error๐Ÿ‘‡
Recording: https://screenrec.com/share/XTp0dwbI42
If you need just this one source file then you can just add it to the other project "as link". Quick and dirty, but who cares. However, if you need more classes, then make a shared library project that would then be referenced by both projects. That's what shared libraries are for.
So, in project ClientAddressWFA try to add this one .cs file but instead of clicking "Add" click on small triangle next to "Add", and select "Add As Link".
Sometimes just restarting the visual studio and compiling again will work (Given that class you are referencing from another project is public and you have added project reference)

Call Forms from Two C# projects in a third Project

This is the first time I start with StackOverflow...
I think my question is a silly question!!
I need to call the forms and classes from two C# projects in a third project
I follow these steps:
From Solution Explorer in a third project, I have clicked the Add Reference
I click the Project Tab
I didn't find any add browsing to add these two projects. see the image
Create your multiple project in a single solution. then add project1 and project2 in your project3 reference using >> Add Reference >> Projects >> Solution.
here's how: https://msdn.microsoft.com/en-us/library/f3st0d45.aspx
Your Projects should be under same solution.
If in case you are building two projects differently not under same solution follow these steps
1.Create a winform project let say "Project1" go to project settings change the output type to class library and build it.
2.Create another winform project lets say "Project2" go to project setting change the output type to class library and build it
3.Create third project from where you want to call it. Go to Add reference. Browse for folders as below
debug folder of project1 select project1.dll and add reference.
debug folder of project2 select project2.dll and add reference.
4.Now create objects of Forms and call.
Alright. Visual Studio offers you the ability to add references you just created yourself. Those references could be (dynamic-link-)libraries DLL or executables EXE. So go ahead and create a project and add a new Form. This custom Form needs to be public so the other project is able to extract the code.
Now you are able to add and use a reference. Just be sure to include the namespace, if it doesn't match the one, in which you're going to use it.
The thing is called Project-to-project Reference

Visual Studio 2013 C#: one solution consume code of another solution

I have Java background and am trying to start on C#.
I wanna create a lib in C# that will be used in other solutions. In Eclipse it's just a matter of creating a jar and adding it to classpath. I know each project in VS2013 becomes a dll, but how can I make a solution see these dll?
Also, in Eclipse, we can create a Web Fragment Project. It can have Servlets, jsp and static js and css files, it becomes a war file and can be imported into another project and its files be used in that project.
How can I do that in VS2013? I'd like to create a solution with static files, master page, some aspx stuff, C# dll, and then use them all in other solutions.
Is there any tutorial (I googled it but found nothing) teaching how to do it?
You have a few options depending on your preferences and scope
Option 1 - The Class Library
You can create Class Library, that can be referenced in your website project. The Class library is a library of classes, interfaces, and value types
You can either Add an existing/New Class Library project to your website solution and reference it directly
You can add the project to your solution by right clicking the
solution (inside VS) -> Add -> Existing project -> and navigating to said
project's .csproj file
or
You can use a new/existing Class Library Project - build it and reference the built dll in your website solution.
you can right click your website solution (inside VS) -> Add -> new project -> choose Class Library
After you've done one of the above ->
Right click the project, you want to add the reference to
Click "Add Reference"
navigate to the .dll in question.
If the dll you want to reference is part of your current solution (as in step 1) -> after you've pressed "Add Reference" - press the "Solution" Tab and it should show up
After you've added the dll.
Remember to reference it in your code files with
Using TheReferenceNamespace;
which will allow you to call the functions inside you dll like the following
FunctionInsideDll(param);
or you could fully qualify your calls instead, like the following
TheReferenceNamespace.FunctionInsideDll(param);
Option 2 - Share MasterPages
if you just want "shareable" masterpages
you can do the following - (taken from this -> MSDN article)
(for future reference - web archive link - just in case something gets moved)
Precompile the Code Used in a Master Page
If you are concerned about code in your master pages being visible to others reusing the pages, you can precompile the master pages' code into a library. In this library, you can include code-behind pages as well as user or custom controls. Compiling master pages does not remove the declarative code for the master files or any server controls used, but you can compile the master files to remove the code for controls or code-behind pages used by the master pages.
If you choose to compile the master pages into a library, you must use the "updatable" build option that allows for later modification of the markup. This option is determined by the Allow the precompiled site to be updatable check box in the Publish Web Site dialog box. For more information about precompiling pages into a library that can be reused, see Building Re-Usable ASP.NET User Control and Page Libraries with VS 2005.
Option 3 - The template
Create a template, and use that template for different projects
In Visual Studio - Press "File" -> Export Template -> follow the wizard.
After it has been exported and you've imported it (either through a checkmark in the wizard or double clicking the vsix file) -it will show up under your project templates when you create a new project.
You can include a project from solution A in solution B by right-clicking on solution B and choosing "Add existing project"
Don't be afraid to edit XML .csproj files. For instance, this works ...
<Compile Include="$(Codez)\z.Libraries\diff-match-patch\DiffMatchPatch\**\*.cs"
Exclude="NotThisOne.cs;**\NotThisFolderWith\This*.cs">
<Link>Libs\%(RecursiveDir)%(Filename)%(Extension)</Link>
</Compile>
...and will give you all the C# files from the source folder, and subfolders, as linked files in your destination project under a folder called \Libs\.
$(Codez) is a Windows Environment Variable I use on my PCs.
I also could have used *.* at the end instead of *.cs.
This is one of those things Visual Studio might break on you, adding a file into the folder full of wildcard-linked files may break them out to separate entries. Or not. Depends on the wind.

Adding common files to different projects in C#

I have 2 project in difference paths. I want to use some common files (.cs files) in both of projects.
D:\Tests\WindowsFormsApplication_1\WindowsFormsApplication_1
D:\Tests\WindowsFormsApplication_2\WindowsFormsApplication_2
D:\Tests\Common
How do I add this common files from one path ("Common" folder) for both of the projects?
Yes, You can. Right Click on your project, "Add" -> "Existing Item...", select your *.cs file and click on Down arrow next to the "Add" button and select "Add As Link".
There is no way to use same files in 2 different project without copying them. But you can add the third project in type of class library and put your shared files in it then add references to that project in your previous projects
If you want the common files to be available to edit in your other projects then
follow user4015859's suggestion re setting up a class library project for your common files, but rather than adding a reference to that project in your other solutions, add the actual project to your solution by right clicking on the solution and select Add > Existing Project...
You still need to add a reference but in the reference Manager dialog (right click on references to open it) select Projects on the left hand side and your common project should be listed. Tick it and click OK.
To use the classes etc from the common project just add its namespace in the units where you want to use it.
This way you can edit the common files from either of your other solutions and you don't need to manually build your common solution when any of the files are changed.

Merging Two Projects and accessing files from each other in Visual Studio

I have merged another project in my existing project by right-clicking on the project name and selecting Add->Existing project option. Now i wan't to access the files from my newly added project from the existing one in Visual Studio (Its a C# project) , how can i do this?
If I'm following your question, you had a solution with a project A. You then added an existing project B.
You now want to access a file from A in B. If that's the case, right click on the B -> References and add a reference to project A. Now you can use the files from that project.
Edit: From your comment, I'm not sure if you want to have a separate project / file. If you want them to be only 1, you shouldn't really add a new project, you should add to your project A the files you need from project B. If you want to keep it as a different module that is included, then the above holds, but you need to realize that usually you have one solution, with many projects inside of it.
I struggle with this until I found this the link is below:
If you want all windows application to run as one program rather then calling multiple .exe. Then change 'Out Put Type' of each project to 'Class Library'.
Step 1:
You can do that by right-clicking on each Project in solution -> Go to Properties -> Application -> Out Put Type... set it to Class Library
Once you have done that output of these will be generated as .DLL.
Step2:
Add a new Window Form application project, add reference of exiting projects in it so that they can be executed from here.. you can do that by.. right click on main project-> Add Reference->Projects select all existing projects from here.
Now on the main application, you can create 3 buttons to launch each project...
[Inventory]
[Accounts]
[Payroll]
Now in each button code will be something like that...
Inventory_click()
{
Inventory.MainForm frm=new Inventory.MainForm();
frm.show();
}
Credit goes to forum post

Categories

Resources