Solved

Visual Basic Resource Files

Posted on 2014-10-13
10
191 Views
Last Modified: 2014-10-13
I want to move all of my English strings into a Resource file (an resx).

I've been able to create the resx and add it into my Project, but I am struggling about the code as to how to refer to it. I believe I should use GetString to get the strings, but it's the code getting to the resource file in the first place that's the problem.

Looking online, people have been talking about Assembly and ResourceManager etc., but I can't get any of it to work. Maybe I am missing some "Imports" at the beginning.

So, is anyone able to give some code for accessing a single string in a resource file (i.e. with all of the necessary Imports that I need).

I am using MS Visual Studio 2010.

Many thanks,
0
Comment
Question by:Phillip Burton
  • 7
  • 3
10 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 40377034
The simplest way would be to use the My namespace.

e.g.

Dim aValue As String = My.Resources.KeyName

Open in new window


The "longer" way would be to create a ResourceManager:

Imports System.Reflection
Imports System.Resources

...

Dim manager As New ResourceManager("YourAssemblyName.Resources", Assembly.GetExecutingAssembly())

Dim aValue As String = manager.GetString("KeyName")

Open in new window

0
 
LVL 24

Author Comment

by:Phillip Burton
ID: 40377057
I'm looking at the ResourceManager solution, because I want to use localised strings (which I don't think you can do with the My.Resources - though if I am wrong, please let me know).

I have got a file called FP.resx with a Key called TestString (see attached).

I am using this code:

        Dim manager As New ResourceManager("FP.resx", Assembly.GetExecutingAssembly())
        MsgBox(manager.GetString("TestString"))

Open in new window


It fails on the second line.

If it helps, I've got two projects, FP1 and FP2. The code starts in FP1, goes to FP2 (a .vb file), and I am calling it from FP2. Does the Assembly bit need changing?
Resource.png
0
 
LVL 24

Author Comment

by:Phillip Burton
ID: 40377060
I note that your call says "YourAssemblyName.resources" - I am using a resx file.

Does that make a difference? If it does, how do I use a .resources file?
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40377108
Honestly, it's been a while since I've looked at resources, but from what I recall, there is nothing you should have to do in terms of selecting a specific file. .NET/Windows takes care of selecting the appropriate file/library based on what the end-user's system is set up as. If the end user is set up for German, then you would have a resource file that is named "SomeName.de.resx". I think it may be a bit more involved in ASP.NET, but not much.
0
 
LVL 24

Author Comment

by:Phillip Burton
ID: 40377118
Thankfully, I'm using Visual Studio 2010 instead of ASP.NET, but still don't get why the above code isn't working.
0
 
LVL 24

Author Comment

by:Phillip Burton
ID: 40377134
Manager.ResourceSets = 0.
0
 
LVL 24

Author Comment

by:Phillip Burton
ID: 40377169
If it helps any:

MsgBox(My.Resources.FP.TestString)

does work.

However, I also have two resource files called "FP.resx" and "FP.fr-FR.resx", and the following still gives me the English version:

        My.Application.ChangeCulture("fr-FR")
        MsgBox(My.Resources.FP.TestString)
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40377199
I think I might have to defer this to someone with more experience with multi-culture applications. If you don't get a decent response before tonight, then I'll take another stab at it when I get home. Sorry for not being more helpful at this time.
0
 
LVL 24

Author Comment

by:Phillip Burton
ID: 40377263
You were very helpful.

I think I've found the solution re the simplest way. Instead of:

 My.Application.ChangeCulture("fr-FR")

I have to use

 My.Application.ChangeUICulture("fr-FR")

So, problem solved. Thank you very much.
0
 
LVL 24

Author Closing Comment

by:Phillip Burton
ID: 40377265
Thank you very much - very helpful.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question