Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / Visual Basic
Article

Share Resources Across Multiple Projects

Rate me:
Please Sign up or sign in to vote.
4.33/5 (9 votes)
6 Oct 2008CPOL2 min read 44.4K   804   19   13
Centralize your resources into a single module and export them in an organized, controlled way.

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:

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#:

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)


Written By
United States United States
Gregory Gadow recently graduated from Central Washington University with a B.S. that combined economics and statistical analysis, and currently works for the Washington Department of Fish & Wildlife as an IT developer. He has been writing code for 30 years in more than a dozen programming languages, including Visual Basic, VB.Net, C++, C#, ASP, HTML, XML, SQL, and R.

Comments and Discussions

 
QuestionImage Not Available At Design time Pin
bir yaz31-May-21 4:27
bir yaz31-May-21 4:27 
QuestionI'm looking for more :'( Pin
Frederic GIRARDIN11-Jan-17 5:21
Frederic GIRARDIN11-Jan-17 5:21 
GeneralMy vote of 5 Pin
FernandoUY8-Jun-12 19:45
professionalFernandoUY8-Jun-12 19:45 
GeneralMy vote of 5 Pin
hoernchenmeister7-Nov-10 21:25
hoernchenmeister7-Nov-10 21:25 
GeneralMy vote of 5 Pin
hoernchenmeister7-Nov-10 21:25
hoernchenmeister7-Nov-10 21:25 
GeneralThis really helped Pin
JM Pironneau17-Feb-10 19:24
professionalJM Pironneau17-Feb-10 19:24 
GeneralPerfect Pin
bar49_9#11-Aug-09 8:36
bar49_9#11-Aug-09 8:36 
Generalcongratulations, the simply normally is the best option Pin
juliotrujilloleon13-Oct-08 22:59
juliotrujilloleon13-Oct-08 22:59 
QuestionI wonder why this is not possible in 2005? Pin
Dinesh Mani7-Oct-08 3:24
Dinesh Mani7-Oct-08 3:24 
AnswerThis technique allows you to control which resources are exposed, and to alias your resources Pin
Gregory Gadow7-Oct-08 3:58
Gregory Gadow7-Oct-08 3:58 
GeneralRe: This technique allows you to control which resources are exposed, and to alias your resources Pin
Dinesh Mani7-Oct-08 5:14
Dinesh Mani7-Oct-08 5:14 
GeneralCreating strongly typed resource wrappers Pin
mav.northwind6-Oct-08 19:15
mav.northwind6-Oct-08 19:15 
Hi!
Instead of creating the wrapper manually and having to add a new wrapper property every time you add a new resource, the smarter solution (IMO) is to use an appropriate code generator.
Take a look at this article[^] for such a generator - it can automatically create public wrappers, performs resourcemanager initialization in a thread-safe manner and even creates methods with the correct signature for string resources containing parameters.

Regards,
mav

--
Black holes are the places where God divided by 0...

GeneralShare Resources Across Multiple Projects Pin
Member 41287956-Oct-08 11:33
Member 41287956-Oct-08 11:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.