GetGlobalResourceObject or Resources.Resource - which is better?

I have a multilingual application. I use ready-made .NET functions for this. Each language has its own file in App_GlobalResources (see Iemge below)

In code, which is better?

  • GetGlobalResourceObject ("LocalizedText", "ErrorOccured")
  • Resources.LocalizedText.ErrorOccured

The second uses less code and is safe, it will return an error at compile time and not run time.

alt text http://img340.imageshack.us/img340/5562/langl.gif

+4
source share
2 answers

These are the benefits of each approach:

Benefits of GetGlobalResourceObject (and GetLocalResourceObject ):

  • You can specify a specific culture instead of using CurrentCulture.
  • You can use a late constraint expression (i.e. a string) to decide which resource to load. This is useful if you cannot know in advance which resource you need to download.
  • It works with any type of resource provider. For example, it works not only with an embedded provider based on the RESX standard, but it will work the same way with a database-based provider.

Benefits of strongly typed RESX types:

  • You get compile-time errors if you access a resource that does not exist.
  • You get Intellisense while working on a project.

So, as with many of the β€œbest” questions, the answer is: it depends! Choose the one that has the most benefits for your specific scenarios.

+8
source

So, use the second if you know what a resource file and key are.

The GetGlobalResourceObject() method is useful if you do not know which resource file or (more likely) key will be at compile time.

+2
source

All Articles