3

I know that I can set a Label's text by using the following syntax.

lblMessage.Text = (string)GetGlobalResourceObject("resxFile", "message");

What are the benefits and drawbacks associated with using the below syntax?

lblMessage.Text = Resources.resxFile.message;

The second method will not work for local resource files. Is there a different syntax for local resource files?

Ryan Gates
  • 4,501
  • 6
  • 50
  • 90
400_the_cat
  • 873
  • 3
  • 16
  • 29
  • Notice that the second way of accessing a resource file only works if you don't plan to localize and thus name it "Resources.TextResources.resx" instead of "Resources.TextResources.en.resx" – Christian Feb 14 '11 at 07:33

1 Answers1

2

The second way looks better because it is strongly-typed. If you changed the resource file name or the resource value name then you would get a compile error. If you needed to dynamically get a resource, then you would have to do it the first way, else use a switch statement or something similar.

If you are using asp.net 2.0 or higher there is actually a 3rd way to set a label by using markup only:

<asp:Label ID="Label1" runat="server" Text="<%$ Resources:resxFile,message %>" />

Kinda related to localization: http://quickstarts.asp.net/QuickStartv20/aspnet/doc/localization/localization.aspx

Hugh Jeffner
  • 2,936
  • 4
  • 32
  • 31