?
Solved

Visual Basic Resource Files

Posted on 2014-10-13
10
Medium Priority
?
198 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
10 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

765 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