Biff MaGriff
Biff MaGriff

Reputation: 8241

How do I get resource file values in Visual Basic?

I'm new to Visual Basic, and I'm having issues accessing the resource file for my project.

Dim rm As Resources.ResourceManager = New Resources.ResourceManager("MyProjectName.My.Resources.Resources", [Assembly].GetExecutingAssembly())
Dim myValue = rm.GetString(lookUpKey) 'boom Object reference not set to an instance of an object.

I think the issue is with the string "MyProjectName.My.Resources.Resources".

Would I be better off moving the strings into their own resource file?

Upvotes: 9

Views: 48510

Answers (7)

akuma6099
akuma6099

Reputation: 131

If you load an external .resx file and want it to show up under intellisense My.Resources then you need to do 2 things.

First the file should be in the root of your project. Simply right click the project and do "Add Existing Item", and give it your .resx file. You should notice that there is no chevron to expand the resx file like your built-in resource file.

The last step is to highlight your resx file and go to the properties window. Under Custom Tool put "ResXFileCodeGenerator" and under the Custom Tool NameSpace put "My.Resources". You should now be able to programmatically access this resource under My.Resources.[name of the resx file].resource_item.

Upvotes: 1

Pierre-David Sabourin
Pierre-David Sabourin

Reputation: 91

Simply:

Dim loginMessage As String = Global.Resources.NameOfYourResxFile.NameOFVariable

Upvotes: 0

Biff MaGriff
Biff MaGriff

Reputation: 8241

I was unable to access the resource file until I moved the .resx file into its own project and referenced that project from my main one. I also had to create a dummy class in that project so that it would compile into a DLL file.

The code for accessing the resource file is actually located in the generated Resource.resx.vb file.

I was able to access the resource file using the following code.

'Name of Class Library where I moved the resx file
Dim classLibraryName As String = "ResourceProj"
'Name of Resource File without the .resx suffix
Dim resourceFileName As String = "Mappings"
'Finding the assembly of the resx file, ResourceProjClass is a dummy class I created so that the dll would build.
Dim myAssembly As Assembly = GetType(ResourceProj.ResourceProjClass).Assembly

Dim rm As Resources.ResourceManager = Nothing
rm = New Resources.ResourceManager(classLibraryName & "." & resourceFileName, GetType(myAssembly)
Return rm.GetString(lookUpKey)

Upvotes: 0

Priyank
Priyank

Reputation: 10623

Refer to the MSDN article Retrieving Resources with the ResourceManager Class for naming convetions:

Dim myManager As New _
   System.Resources.ResourceManager("ResourceNamespace.myResources", _
   myAssembly)

Upvotes: 1

Makah
Makah

Reputation: 4523

Try

 Global.<MyNamespace>.My.Resources.<ResourceStringName>

to access Resource Strings

Upvotes: 4

manji
manji

Reputation: 47978

Try ResourceManager("MyProjectName.Resources", ...), otherwise if it's the application resources you can simply use My.Resources.HD (see here:My.Resources Object)

or

Open Reflector, load your assembly there, go to resources, a list of resources appears, search for the one containg 'HD', copy the name (it's like MyProjectName.Resources.resources), remove the last .resources and try with that.

Upvotes: 1

Freesn&#246;w
Freesn&#246;w

Reputation: 32203

I thought it was something similar to:

my.Resource.whateverhere

Is that not the kind of resources you are looking for?

Upvotes: 11

Related Questions