Solved

Visual Basic Resource Files

Posted on 2014-10-13
10
192 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 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