473,473 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Type safe access to resources

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

Sep 14 '06 #1
3 1917

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/Re...sGenerator.asp

HTH,

Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.


On 14 Sep 2006 16:24:36 -0700, "Big Daddy" <jo********@yahoo.com>
wrote:
>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
Sep 14 '06 #2
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.

"Big Daddy" <jo********@yahoo.comwrote in message
news:11**********************@d34g2000cwd.googlegr oups.com...
>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

Sep 15 '06 #3
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.

Sep 15 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Tongu? Yumruk | last post by:
I have a little proposal about type checking in python. I'll be glad if you read and comment on it. Sorry for my bad english (I'm not a native English speaker) A Little Stricter Typing in Python...
31
by: Jamie Burns | last post by:
Hello, I am writing a client / server application. There is 1 server, and many clients. The server processes requests from each client, and typically creates and manipulates C++ objects on their...
4
by: AdamM | last post by:
How can I change an object's type at runtime? For example, here's what I want to do in psedocode: object animal; if (dog) { animal=(dog)animal;
2
by: SpotNet | last post by:
Hi Newsgroup, General C# questions. If I have a class(es), of API functions just as they are. Would that be considered Unmanaged or unsafe (not meant as the c# keyword) code? If I implement...
3
by: karthick.ramachandran | last post by:
Hi, I was just reading this article http://www.netobjectives.com/resources/downloads/Best_Practices_CSharp_Delegates.pdf In which the author had mentioned <quote> Delegates would appear...
2
by: Jeff Chan | last post by:
I have read the documentation from msdn to try to understanding the concept of "Type safe". Would someone give me an example of code segment illustrating what is *Non* type safe? Many Thanks,...
4
by: Zen | last post by:
Hi, Is my finalizer safe? Thanks!! public class MyClass { private string m_str; public MyClass( string s ) {
7
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}"...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.