Share resource files between projects? RRS feed

  • Question

  • Is there a way to share resource files between two projects in VB.NET? I remember in VB6 I could share the same file (through SourceSafe?). Basically, I have a resource file that has localized strings in Project1. I want Project2.ClassA to be able to use Resource.resx in Project1.


    Project2 - references Project1

    But when I reference Project1, intellisense doesn't show me the resource file like it does the other classes that belongs in Project1. Intellisense only shows me Class1 and Class2. In Project1.Class1, I do an "Imports Project1.My.Resources" statement. Is there another call that I can make inside Project2 similar to it? Obviously, I can't include the same Imports statement because it uses "My" in it.

    Any ideas?

    Tuesday, February 20, 2007 11:47 PM


All replies

  • The strong type resource class is generated as a 'Friend' class, not a 'Public' class, so you couldn't access it from another project.


    In the next version of the visual studio, you would be able to make a choice whether to generate a public class for your resource items.


    For your scenario, if you can live with two copies of resources in the memory, you can share one resource file between two projects.  Just create a seperate resource file in one of your project, and add it to another project as an linked item. Make sure you set "Custom Tool" and "Custom Tool Namespace" same for both of them, and it should work.


    If you want to share a same copy of resource class, (generate public class in VS 2005), you can write a build task to use resgen.exe to generate code.  (http://blogs.msdn.com/lifenglu/archive/2006/03/16/553348.aspx)




    Friday, April 6, 2007 9:13 PM
  • Here is an example of a custom generator for strongly typed resources that generates public wrappers, so that you can access the resources from another assembly. The code it generates is for C#. I haven't looked into converting it to VB, but it shouldn't be too hard.




    I have to say that it is beyond me why MS didn't include this as an option. I can understand wanting the default to be internal (Friend), since some resources might be properietary. But how hard is it to include an option that changes the visibilty?

    Tuesday, April 10, 2007 6:32 PM
  • Would it be possible to use the InternalVisibleTo("<namespace>") attribute?

    The InternalVisibleTo attribute allows you to specify a namespace that can see the internal properties, classes, methods etc of an assembly.

    For example if you have a class libray which holds your resx files and you want to use these resx files in an assembly with the namespace ClassLibrary1 you could add the following to you AssemblyInfo.cs:

    [assembly: InternalsVisibleTo("ClassLibrary1")]

    Now in the project for ClassLibrary1 add a reference to the project that holds the resx files, as the internal attributes are visible to this namespace you should be able to use the resources as normal, e.g.:


    All the best.
    Wednesday, April 11, 2007 1:51 PM
  • You don't specify a namespace, you specify an assembly. But yes, I would think that would work.
    Wednesday, April 11, 2007 2:44 PM
  • Note: In the next version of Visual Studio, we do support an option to make these public.  See the comments in http://blogs.msdn.com/lifenglu/archive/2006/03/16/553348.aspx.
    Thursday, June 14, 2007 1:20 AM