Solved

Visual Basic Resource Files

Posted on 2014-10-13
10
190 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

832 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