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:

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.

Share

About the Author

Gregory Gadow lives in Seattle, Washington and has been writing code for almost 25 years in more than a dozen programming languages. He works for a mid-size brokerage firm and holds the Series 7 and Series 66 brokerage licenses, but much prefers working as the company's programming department doing VB6, VB.Net, ASP, HTML, XML and SQL.

Nice to know that VS 2008 exposes resources as public classes making it available across multiple projects, thanks for the info. But, I think its very much possible to expose a resource files by creating public wrappers for the default properties. Just have the resource files in a separate project, create public wrappers and reference the project in other projects.

If that's what you were trying to convey, then I'm afraid that the message has not come out clearly.

Perhaps you only want to expose some of the resources, or to alias the same resource with different names. The other ways I've seen to do this were all-or-none propositions, and none of them make it easy to alias a resource under several different names.

To create an example from the sample apps: I have the same image published as Page and Report. I use Page to mean a sheet of paper and Report to indicate a report. For now, they show the same image. Latter on, I add a separate image to indicate a report. All I have to do is change the image exported by the property Report, and I'm done. Without this aliasing ability, I would have to dig through every project that references the resource library for all references to Page, decide if it is actually a report, and change the image reference.

I think this is a good technique if you need simplicity and flexibility. Just don't pay any attention to the misspellings that somehow appeared after I had proofed the article.

I understood then intent and purpose of the article and I appreciate the fact that you have put it up. Its just that I didn't make right sense of it. My bad! I got the introduction wrong and posted the message. I read thro the article again and understood my mistake, but cudn't edit/update my message as I had to head to a meeting right then. Please discard my note, I'll remove it once and for all after sometime.

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.