views:

168

answers:

2

Hi,

I've two VB 2008 projects - one is class library project and another is winform project. In class library project, I've defined some strings in the project resources (project properties > Resources tab).

I build that class library project and get the dll from debug folder and added up as a reference in my winforms project.

Now, I need to read those strings from the referenced dll. How?

Thanks.

+1  A: 

I think you just use System.Reflection.Assembly.Load to load the other assembly then use the constructor of System.Resources.ResourceManager that takes an assembly.

Here's a link showing a sample for how to do this:

http://msdn.microsoft.com/en-us/library/aa984408%28VS.71%29.aspx

Note, I don't think it needs to a reference for this to work.

ho1
The class name of Resources.resx in Resources.resx.vb is My.Resources. What name should i referenced with?Me.rm = New ResourceManager(("MyClassLibrary.Resources"), assembly)Is this correct?
Ramiz Uddin
Not sure, but seems plausible. Though I'd suggest using Donovan's answer since that sounds like it might be more suitable for you
ho1
+1  A: 

While you can dynamically load the DLL as ho suggests, it's fine to use a reference as you have done. In fact I would recommend using a reference unless you had a particular requirement to dynamically load the resource assembly.

As to accessing the resources there are a few things you need to do.

  • In the resource assembly you will need to ensure the resources are public. By default resources are set to internal which means you will not see the resources in the winforms app. Double click on Properties\Resources.resx to open the resources view. In the top toolbar you will see a label "Access Modifier" next to a combo box drop down. Change the selection to public.

  • You will need to reference the assembly from the forms app. You have stated you have already done this. Just a note that a better way to do this is to create a solution that contains both projects. Then in the forms app choose add reference. Click on the Projects tab up the top. Double click on the resource DLL project name. This works better than referencing the debug DLL directly since it means if you change between a release build and debug build in your forms app, it will automatically build a matching release/debug version of your resource assembly.

  • Once you have added the reference you can simply reference the type out of the resources DLL, e.g.

ResourceDLLNamespace.Properties.Resource.ResourceName

Just a note, you need to be aware of type name clashes if you are using the same namespace for your forms app and resource DLL. In this situation both your forms app will have access to it's own Properties.Resources class as well as that of the resource DLL. You can do two things to avoid this:

  1. Use a different namespace between the two assemblies
  2. In the resource assembly don't include a default Properties\Resources.resx resource dictionary. Delete it and manually add a new resource, i.e. Add New Item and select "Resources File". You should find that you will not be able to add the new resource dictionary to the Properties folder - add it to the root or some other folder as you require. This will automatically give it a different type name by virtue of being in a different folder. You still may want to avoid using the resource file name of "Resources" however, as if you have all the relevant namespaces in scope via using statements you will get the same issue that the compiler won't know which version of Resources to use.

-Donovan

donovan
thanks donovan. this is very descriptive. thanks for the help.
Ramiz Uddin