5,665,355 members and growing! (15,944 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » How To     Intermediate License: The Code Project Open License (CPOL)

Share Resources Across Multiple Projects

By TechBearSeattle

Centralize your resources into a single module and export them in an organized, controlled way.
C#, VB, .NET, Visual Studio, Dev

Posted: 6 Oct 2008
Updated: 6 Oct 2008
Views: 2,291
Bookmarked: 9 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
5 votes for this Article.
Popularity: 2.45 Rating: 3.50 out of 5
1 vote, 20.0%
1
0 votes, 0.0%
2
1 vote, 20.0%
3
0 votes, 0.0%
4
3 votes, 60.0%
5

Introduction

In VS 2005 and earlier, resources were flagged as Friend; this meant that, you could use them globally within the project, but could not make them directly available to outside projects. VS 2008 allows you to make resources Public, but this applies to all resources, and namespace issues can make this a bit tricky. If you use a static class, however, resources can be exported in a controlled, organized, and easy to maintain fashion.

Background

I have been working on a large application organized into several projects. One of these is the "core" project, which holds various toolboxes, custom controls, etc. I wanted to put all of the app's icons and images here, too.

We started out using VS 2005, which does not allow the resources of a project to be shared. The workaround turned out to be pretty simple: export the resources using a static class.

After switching to VS 2008, my team decided to stick with using this export class. The structure allowed to pick which resources would be made public, to organize the resources into logical groupings, and to alias resources to show how they are to be used. The aliasing has proven especially useful, as it has allowed us to change an image in only two places -- the resource file and the exporting class -- and leave the rest of our code alone.

The Theory

In the accompanying solution, there is a code module named Shared. This defines the namespace SharedResources, which contains three classes: Icons, Images, and Strings. These classes export icon, image, and string resources, respectively, by using read-only properties. This is what the Images class looks like in VB:

Public Class Images

    Public Shared ReadOnly Property Leave() As Image
        Get
            Return My.Resources.Leave
        End Get
    End Property

    Public Shared ReadOnly Property Page() As Image
        Get
            Return My.Resources.Page
        End Get
    End Property

    Public Shared ReadOnly Property Report() As Image
        Get
            Return My.Resources.Page
        End Get
    End Property

    Public Shared ReadOnly Property User() As Image
        Get
            Return My.Resources.User
        End Get
    End Property

End Class

In C#:

using SharingResources_CS.Properties;
using System.Drawing;

public class Images
{
    
public static Image Leave

    {
        get { return Resources.Leave; }
    }

    public static Image Page
    {
        get { return Resources.Page; }
    }

    public static Image Report
    {
        get { return Resources.Page; }
    }

    public static Image User
    {
        get { return Resources.User; }
    }
}

You can see that the resource Page is exported under two different aliases, as Page and as Report. If we ever wanted to change the Report image, all we would need to do is add the image to the project, then reference it in the Images class.

We could have further broken this down in to additional classes, say, small images for use in menus and toolbars, and large images for panel backgrounds; the Strings class can be divided up into strings for menus, strings for message boxes, and strings for help text.

The Example Solutions

I have written two example solutions, one in VB and one in C#, using VS 2008. Each solution contains two projects. The first one implements the SharedResources namespace. The second initializes a form's menu and title from the exported resources.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

TechBearSeattle


TechBearSeattle is an alias for Gregory Gadow who, oddly enough, lives in Seattle and, odder still, actually does resemble a bear.

He has been writing code for more than 20 years in more than a dozen programming languages including RPG, Fortran and even COBOL on rare occasion. His current job description includes lots of VB, VB.Net, ASP, HTML, XML and SQL work.
Occupation: Web Developer
Location: United States United States

Other popular .NET Framework articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 6 of 6 (Total in Forum: 6) (Refresh)FirstPrevNext
Generalcongratulations, the simply normally is the best optionmemberjuliotrujilloleon23:59 13 Oct '08  
GeneralI wonder why this is not possible in 2005?memberDinesh Mani4:24 7 Oct '08  
AnswerThis technique allows you to control which resources are exposed, and to alias your resourcesmemberTechBearSeattle4:58 7 Oct '08  
GeneralRe: This technique allows you to control which resources are exposed, and to alias your resourcesmemberDinesh Mani6:14 7 Oct '08  
GeneralCreating strongly typed resource wrappersmembermav.northwind20:15 6 Oct '08  
GeneralShare Resources Across Multiple ProjectsmemberMember 412879512:33 6 Oct '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 6 Oct 2008
Editor: Smitha Vijayan
Copyright 2008 by TechBearSeattle
Everything else Copyright © CodeProject, 1999-2008
Web11 | Advertise on the Code Project