How to get resource file values ​​in Visual Basic?

I am new to Visual Basic and am having problems 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 problem is with the line "MyProjectName.My.Resources.Resources".

Would it be better to move lines to their own resource file?

+6
source share
6 answers

I was unable to access the resource file until I moved the .resx file to my own project and named this project from my main one. I also had to create a dummy class in this project so that it compiles 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) 
+1
source

I thought this was something similar to:

 my.Resource.whateverhere 

Isn't that the resource you are looking for?

+7
source

See the MSDN article Retrieving Resources Using the ResourceManager Class for Naming:

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

Try ResourceManager("MyProjectName.Resources", ...) , otherwise, if these are 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 will appear, find one instance of 'HD' , copy the name (it's like MyProjectName.Resources.resources), delete the last .resources and try it.

+1
source

Try

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

to access resource strings

0
source

Simply:

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

0
source

Source: https://habr.com/ru/post/885749/


All Articles