Link to existing source file

Got a nice tip from PDSA.NET

Reuse Class Files in Different Projects (Add as Link)

All of us have class libraries that we developed for use in our projects. When you create a .NET Class Library project with many classes, you can use that DLL in ASP.NET, Windows Forms and WPF applications. However, for Silverlight and Windows Phone, these .NET Class Libraries cannot be used. The reason is Silverlight and Windows Phone both use a scaled down version of .NET and thus do not have access to the full .NET framework class library. However, there are many classes and functionality that will work in the full .NET and in the scaled down versions that Silverlight and Windows Phone use.

Let’s take an example of a class that you might want to use in all of the above mentioned projects. The code listing shown below might be something that you have in a Windows Form or an ASP.NET application.

public class StringCommon
{
public static bool IsAllLowerCase(string value)
{
return new Regex(@”^([^A-Z])+$”).IsMatch(value);
}

public static bool IsAllUpperCase(string value)
{
return new Regex(@”^([^a-z])+$”).IsMatch(value);
}
}

The StringCommon class is very simple with just two methods, but you know that the System.Text.RegularExpressions namespace is available in Silverlight and Windows Phone. Thus, you know that you may reuse this class in your Silverlight and Windows Phone projects. Here is the problem: if you create a Silverlight Class Library project and you right-click on that project in Solution Explorer and choose Add | Add Existing Item… from the menu, the class file StringCommon.cs will be copied from the original location and placed into the Silverlight Class Library project. You now have two files with the same code. If you want to change the code you will now need to change it in two places! This is a maintenance nightmare that you have just created. If you then add this to a Windows Phone Class Library project, you now have three places you need to modify the code!

Add as Link

Instead of creating three separate copies of the same class file, you want to leave the original class file in its original location and just create a link to that file from the Silverlight and Windows Phone class libraries. Visual Studio will allow you to do this, but you need to do one additional step in the Add Existing Item dialog. You will still right mouse click on the project and choose Add | Add Existing Item… from the menu. You will still highlight the file you want to add to your project, but DO NOT click on the Add button. Instead click on the drop down portion of the Add button and choose the “Add As Link” menu item. This will now create a link to the file on disk and will not copy the file into your new project.

When this linked file is added to your project, there will be a different icon next to that file in the Solution Explorer window. This icon signifies that this is a link to a file in another folder on your hard drive.

Of course, if you have code that will not work in Silverlight or Windows Phone — because the code has dependencies on features of .NET that are not supported on those platforms – you can always wrap conditional compilation code around the offending code so it will be removed when compiled in those class libraries.

Download Other Tips and Tricks

I hope you found this short tip useful. You can get many other tips, tricks and articles at my website. http://www.pdsa.com/downloads.

Changing the Projects Version

The following code may be used in the Macro Studio. It will update the AssemblyInfo files to reflect them with a given version. When the files are required to be chacked out, it will happen automatically.


''' <summary>
''' For all projects in the solution,
''' change the version to a given version
''' </summary>
''' <remarks>some items in the solution might produce an exception
''' for istance if they are not loaded, or they have no vesion
''' </remarks>
Sub ChangeVersionProperties()
Dim sol = DTE.Solution
Dim projectT As EnvDTE.Project = sol.Projects.Item(1)
Dim newVersion As String = "1.200.0.9"

For Each project As EnvDTE.Project In sol.Projects
Try
Debug.Write(project.Name)
Debug.Write(
" changes from ")
Debug.Write(project.Properties.Item(
"AssemblyFileVersion").Value)
Debug.WriteLine(
" to " + newVersion)
project.Properties.Item(
"AssemblyFileVersion").let_Value(newVersion)
Catch ex As Exception
System.Windows.Forms.MessageBox.Show(project.Name +
" kon niet gezet worden: " + ex.Message)
End Try
Next
End Sub

Why Should You Move to Visual Studio 2010

This is the first thing I’ve got from the early pages of the book Moving to Visual Studio 2010 which looks very interesting to read:

There are numerous reasons to move to Visual Studio 2010 Professional,
but it would be goot do list a few from a high level perspective:

  • Built-in tools for Windows 7, includign multi-touch and “ribbon” UI components.
  • Rich new editor built in WPF that you can highly customize to suit how you work.
  • Multi-monitor support
  • New Quick Search helping to find relevant results just by quickly typing the first few letters of any method, class, or property.
  • Great support for developing and deploying Microsoft Office 2010, SharePoint 2010 and Windows Azure application.
  • Multicore development support allows you to parallelize your applications, and a mew specialized debugger to help you track the tasks and threads.
  • Improvements to the ASP.NET AJAX framework, core JavaScript IntelliSence support, and the inclusion in Visual Studio 2010 of jQuery.
  • Multi-targetting/multi-framework support

Modify Visual Studio 2005 templates

Whenever you create a new file in the visual studio, you are asking it to put a file as it is specified in its templates. The templates are located in
C:Program FilesMicrosoft Visual Studio 8Common7IDEItemTemplatesCache and for web development you need to look into the Web folder in there.
There are some handy parameters like $time$ and $user$ that you can see in the MSDN at Visual Studio Template Reference: http://msdn.microsoft.com/en-us/library/ms247064(VS.80).aspx