How to share code and resources between projects - c#

I have two projects named Project 1 and Project 2. I am porting my application from iOS to WP8 and I have ported my iOS_Project 1 to WP8_Project 1. In iOS I have the flexibility to add the files as references and thereby I can achieve code reuse. I would like to perform the same process in WP8, but I couldn't find proper help and here's the question. (Here I want to refer all the classes, images and xaml files like MainPage.xaml)
I know pretty sure about one thing that wpf/forms/.NET projects differ with WP8 structure. Please do provide some help.
Thanks.

When you have add your item to one project, select the option to Add Existing Item to the other project. Navigate to the item you want to add and then you should find a drop-down arrow on the appropriate button to allow you to add the item as a link. That means that there will only ever be one copy of that item but it will be used in both projects.That said, if possible, I would suggest that you break the common functionality out into a library project that you then reference in both the other projects. If you create a portable library then you could use it in a Windows 8 app as well.

You can use references in Visual Studio 2012 - there is no problem. However, PCL (Portable Class Library) is a better solution. Here is example how to add files as link in Visual Studio 2012.

Related

Self-built C# Library not found

I have an issue including a self-built library to a C#-project. I have created an own class library called ClassLibrary1 just to learn how to add libraries on Visual Studio 2019.
So I have written some simple code in a newly created .NET-class library project and have clicked on "create new solution" (directly translated from my german IDE-language. Maybe it's called slightly different) after writing the code. Back in the C#-project, I have selected the dll-file from bin/Debug/ of the class library's project folder.
After I have set the checkmark, the dll-file was shown in the solution-explorer under Assemblys like expected. But the issue I now have is that I still cannot use the ClassLibrary1.dll-file in the cs-file in this very project as I expected via the command "using ClassLibrary1;". It only shows me the error message "type- or namespacename "ClassLibrary1" not found" when trying to compile the C#-project and I don't get, why this is the case.
It seems like it has to be a very obvious problem but after some research on the internet and trying some things by myself still nothing has changed.
Thanks in advance for helpful replies.
The by far easiest way to manage a library is to use project references. Ensure that your library and the project that uses the library is in the same solution. Then right click the "references" and select "add Reference", go to the project tab and add a checkbox for the library. Read more about managing references.
You might also need to add namespaces for the classes you wish to use in the source files.
I would not recommend managing using file-references to lose dll-files, since it can easily become a hassle to manage. I.e. if you create a new version of the library you would need to build, and explicitly replace this file in all other projects and update all the references.
If you want to share libraries between multiple solutions the more popular solution would be to setup a nuget server. This solves some of the updating problems by maintaining multiple versions of the same library, and provides a nice interface to update references in all projects. But this is a somewhat more complicated solution, so I would not recommend this for new developers.

Trying to better understand Shared Projects and using them in different solutions

Even though Shared Projects have been around since Visual Studio 2015 (maybe as early as VS 2013 update2), I've only recently learned about them. Today I spent time trying to learn how to use them following a tutorial I found Shared Project: An Impressive Feature of Visual Studio 2015 Preview. However, the one thing the author did in that tutorial, which won't work for us, is he created the Shared Project and 3 other projects, all within the same solution. Of course, you can do that, but in practice we're likely to want to create a Shared Project in some solution, and then as time goes by, include that Shared Project in other solutions.
So what I did is instead of putting the Windows Forms application into the same solution as the author of that C# Corner post did, I created a new solution with a Windows Forms project in it, then I tried to add the Shared Project from the first solution. First, I tried adding the .sln file. That failed miserably. Then I tried adding the .shproj file to the second solution. That failed miserably as well.
Next I shared here on SO for ways of addressing this. I found 2 posts: Adding references in a shared (.shproj) project and How do I add a reference to a Shared Code project (.shproj) from another project. The second one gave me an idea. I decided I would simply add the Shared Project, from the first solution, to the second solution by clicking on the second solution within Solution Explorer, then doing a "Add Existing Project". That worked.
But I wonder, is that the way you're supposed to use Shared Projects? If so, it seems to me as though I could just as well created a simple class library in the first solution and then added that class library project to the second solution. Is there something about Shared Projects that make them inherently better to use, if you add the Shared Project to a different solution, instead of just adding a regular class library project to a solution?
A class library compiles into its own DLL and your original project references that DLL, whereas a project using a Shared Project will compile into a single assembly. One scenario I could think of with shared projects is that you can have single code base but has platform specific code sections marked by directives.
There is a good video on this subject even though it's being explained in the context of xamarin they do a good job i think.
https://www.youtube.com/watch?v=G5ov0gLZWgQ
Personally I I would always go with PCL (portable class lib) rather than SAP (shared project). I use shared code projects as documentation container in my projects. The project green icon stands out really well. I keep everything there from markeddown doc files to stored procedures and etc.

C# include (type behaviour)

I have a set of applications that will pass a particular structure between each other.
I would like to have just one definition of this structure, and have each of the applications reference it.
In C / C++ I would do this by having an #include reference to the .h file containing the definition of the structure. I understand that this isn't possible in C#, but I would like to know the best way to acheive a similar thing.
I am using Visual Studio 2012, each application is its own project. I have one Solution which contains the various projects. If I try to drag and drop the TagData.cs file (which contains the structure definition) into any projects then the file itself is copied into the Project directory (which is not what I want, since this results in multiple copies of the definition).
I'm sure this is a simple question, and there must be an easy solution, but I haven't been able to find anything.
Thanks
What you'd want to do here is move those pieces that are shared into a third project and reference that new project from the other two.
If this is not possible (different platforms between projects where the types you're using wouldn't be supported by a PCL profile), you can right click on the project you're looking to include the file in, select Add Existing Item... browse to the file in question, and, instead of just clicking Add, select the down arrow just to the right of it and click the Add as Link option.

C#: Making a file editable from one project, but including it in two

I've currently got a solution where a certain class is used from several C# projects, some Silverlight, some regular. For compatibility reasons I cannot create a single project for these utility classes. At first I solved the problem by making the class library project a silverlight one, so that it could be referenced by all other projects.
However, I ran into another problem that made it impossible to use a Silverlight project as the class library. So I resorted to making two separate class library projects: one Silverlight, one regular. In C++ it's possible to add a project file to two projects, while only having one physical file on disk. I noticed that in C#, it automatically makes a copy when adding the file to a second project. I solved this with a build event that copies the file from the regular library to the Silverlight one when the Silverlight project is built.
This all works, so technically the problem is solved. However, it's gotten too convoluted for my taste. Also, other programmers who are unaware of my solution might start to edit the dependent (Silverlight) file, and get unexpected results.
So my question is, do you know of any good reasons to solve these two issues properly? How would you solve it?
If you need 1 file on disk do not use the standard "Add" when adding the file to your project but click on the arrow next to it in the dialog and select "Add as link", however this does not prevent it from being edited (you'll have to look into other options there, I'm not aware of any)
I think you are talking about linking. Have a look here:
http://support.microsoft.com/kb/306234
Hope this helps.
have the file in one project then in the other projects where you want to have it as well do Add Existing Item, select the file and see the Add button in the dialog has a small arrow, select Add as Link from there and you are set. :)

What is the best practice to segment c#.net projects based on a single base project

Honestly, I can't word my question any better without describing it.
I have a base project (with all its glory, dlls, resources etc) which is a CMS.
I need to use this project as a base for othe custom bake projects.
This base project is to be maintained and updated among all custom bake projects.
I use subversion (Collabnet and Tortise SVN)
I have two questions:
1 - Can I use subversion to share the base project among other projects
What I mean here is can I "Checkout" the base project into another "Checked Out" project and have both update and commit seperatley. So, to paint a picture, let's say I am working on a custom project and I modify the core/base prject in some way (which I know will suit the others) can I then commit those changes and upon doing so when I update the base project in the other "Checked out" resources will it pull the changes? In short, I would like not to have to manually deploy updated core files whenever I make changes into each seperate project.
2 - If I create a custom file (let's say an webcontrol or aspx page etc) can I have it compile seperatley from the base project
Another tricky one to explain. When I publish my web application it creates DLLs based on the namespaces of projects attached to it. So I may have a number of DLLs including the "Website's" namespace DLL, which could simply be website. I want to be able to make a seperate, custom, control which does not compile into those DLLs as the custom files should not rely on those DLLS to run. Is it as simple to set a seperate namespace for those files like CustomFiles.ProjectName for example?
Think of the whole idea as adding modules to the .NET project, I don't want the module's code in any of the core DLLs but I do need for module to be able to access the core dlls.
(There is no need for the core project to access the module code as it should be one way only in theory, though I reckon it woould not be possible anyway without using JSON/SOAP or something like that, maybe I am wrong.)
I want to create a pluggable environment much like that of Joomla/Wordpress as since PHP generally doesn't have to be compiled first I see this is the reason why all this is possible/easy. The idea is to allow pluggable themes, modules etc etc.
(I haven't tried simply adding .NET themes after compile/publish but I am assuming this is possible anyway? OR does the compiler need to reference items in the files?)
UPDATE (16/05/2010):
I posted a similar question with a little more detail for question 2 on Experts-Exchange. I don't want to post all that info here as it just will be too messy but it explains question 2 in greater detail.
For your first question, you want to use svn externals. More details can be found here: http://svnbook.red-bean.com/en/1.0/ch07s03.html
For your second question, you need to create a seperate assembly and the easiest way is to create a new project within your solution. You can't have a single project emit 2 dll's (that I know of)
For your first question:
If the base project is a library then there is nothing stopping you from creating the following directory structure on your SVN:
Base project
Cool project nr 1
Cool project nr 2
All projects built on the Base project will include a relative reference and then everybody can checkout his Cool project X and the Base project and work on them. Checking-in changes for Base project will allow everybody else to see them by updating their Base project image. Advantage: only one SVN trunk required.
For your second question :
I tried my best, but I can't understand what you're asking :).

Categories