Type safe access to resources

  • Thread starter Thread starter Big Daddy
  • Start date Start date
B

Big Daddy

I have added a resource file to my project called
"DemoSite1Resources.resx". I have seen an example in VB where you
could have type-safe access to the resources in your code like this:

btn.Text = My.Resources.DemoSite1Resources.btnCaption

But how can I have similar type-safe access to the resources in C#? I
have been able to do it in an ASP.NET application, but I haven't been
able to do it in a WinForms application.

Thanks in advance,
John
 
In .NET 2.0 a class is created that corresponds to the name of the
resource file.

So if your project's default namespace is DemoSite1 and you put
DemoSite1Resources.resx in the root folder, then you can access them
with:

DemoSite1.DemoSite1Resources.[resource name]

You can also see this in the solution since the resx file will have a
DemoSite1Resources.Designer.cs file associated with it which you can
open and view the namespace, class, and property definitions.

There are 3rd party custom tools to do this in VS.NET 2003 but afaik
only for strings. Here's one example:

http://www.codeproject.com/dotnet/ResourceClassGenerator.asp

HTH,

Sam
 
You shouldn't need to add a resource file to your app. If you're working
with .Net 2.0, and the VS.Net IDE, your project should already have a
resources file under the Properties folder of the project. It is very easy
to work with, and is strongly-typed if you use it as it was designed. See
http://msdn2.microsoft.com/en-us/library/k4s6c3a0.aspx.

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

What You Seek Is What You Get.
 
Thanks. I was able to get it working. I found that if I added a brand
new resource file to the project, it automatically generated the
type-safe DemoSite1Resources.Designer.cs file for me. The problem was
that I had copied some resource files from another project and added
them to this project. It didn't seem to automatically make the cs
files for me. So I went to the properties of the resource file and put
ResXFileCodeGenerator for the "Custom Tool" property and that seemed to
do the trick. When I tried to compile, it gave me a warning, asking me
if I wanted to generate the type-safe cs files, to which I said yes. I
don't know if there's a better way, but it worked for me. Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top