Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am developing a Windows application and I want to store all the text for labels, radio buttons, buttons, check boxes and column headers of grids at one place. I have tried using a class file, an xml file, a database table and a resource file.

I found that a class file is the best way to store all these texts, but I want to know is there any alternative way? If I am using a class file then I will have to recompile the project if any text changes.

Will a resource file work properly for large amounts data? What about a dictionary or storing data in variables?

share|improve this question
    
msdn.microsoft.com/en-us/library/… –  Sriram Sakthivel Aug 30 '13 at 11:39
    
@SriramSakthivel: The link shows description about resource. What do you want to suggest, i didn't understood, please give brief description –  sandeep.gosavi Aug 30 '13 at 11:47
1  
@sandeep.gosavi - So give him the choices. Your asking for the "best way" but you already have it. –  Ramhound Aug 30 '13 at 14:21
1  
Related, but not duplicates: Class design for internationalized object, Internationalization : What things to think about?, and Effective Strategies for Localization in .NET. I know the post doesn't explicitly reference I18N, but if it quacks like a duck... –  Dan Pichelman Aug 30 '13 at 14:26
1  
Please don't cross post stackoverflow.com/q/18531904/839601. The Stack Overflow question could have been migrated. You just needed to have flagged it. –  ChrisF Sep 5 '13 at 9:34
show 5 more comments

6 Answers

I think you pretty much mentioned it all.
If you want something flexible, where you don't need to recompile, then the DB might be your best option.
But otherwise I would go with .resx, as this is the standard.
Saving text inside class files is a rather bad idea.

share|improve this answer
add comment

In short: It sounds to me like a localization of resources (different types like static labels, text, etc..). Generally speaking, changes in resource file content should not require a re-build of the application.

The dis-advantage to store resources in classes, is that each change/modification will require a re-build of the win-form application.

Will a resource file work properly for large amounts data?

I think the limitation is the file-size limitation of the operating system and that is 4 GB on 32 Bit systems, if I remember correctly. For 64 bit it should be even bigger. Anyway, for the purpose to store the static text, it should be more than enough.

Thus, in my opinion, this should not be done anywhere else if the text will be static.

An alternative approach would be creating a class to control access to your resource files. This class might be used to store the keys to access your resource files and a have strongly typed way to retrieve available resources of the project.

Some references on SE answers on localization and resources:

References from MSDN :

share|improve this answer
1  
would you mind explaining more on what these resources do and why do you recommend these as answering the question asked? "Link-only answers" are not quite welcome at Stack Exchange –  gnat Aug 30 '13 at 13:33
    
@Yusubov: I have gone through your links, resource is good option, but i am in search for any other option. –  sandeep.gosavi Aug 30 '13 at 13:46
1  
The answer does not have just links, it has statement with proposed opinion of the solution that will work. –  Yusubov Aug 30 '13 at 13:57
    
@sandeep.gosavi: IMHO, choosing resource files as solution will work for your win-forms application. Alternative you may create a class that would control access to your external resources. –  Yusubov Aug 30 '13 at 14:23
    
@Yusubov: what is IMHO? –  sandeep.gosavi Aug 30 '13 at 14:24
show 1 more comment

Besides the .resx format, you can also use .txt or .restext files, which are in a simpler (and easier to read and edit) format.

Creating Resource Files for Desktop Apps (see the "Resources in Text Files" section)

The format is essentially name = value, with the ability to have comments and conditional compilation.

Another option is to use whatever source or text file you want, and build the binary .resources file either manually or as part of a build step. (see "Resources in .resrouces Files" from the link above).

Either way, there is a little more work to get these into your project and compiling, but they both have the benefit of having support for multiple languages, just like the .resx file.

share|improve this answer
add comment

In C#, you're almost certainly better off using a Resource file (which is an XML file). This gives you the following benefits:

  • Removes need to re-compile entire solution for changes of text.
  • Allows management of strings to be done by someone other than the coder (coders aren't necessarily the best people to be writing user error/informational messages).
  • Allows fairly trivial localisation.
share|improve this answer
add comment

I had a similar requirement before and I stored resources in a .resx file but I have decided to use classes for storing the keys.

For example, say I have resources for banking withdrawal screen.

I have created a simple class on the lines of

public class BankingWithdrawalResource
{
   public string WithdrawFunds { get; set; }
}

I then implemented a class for retrieving resources. My aim was to have a strongly typed way of pulling out resources. I also wanted to make resource retrieval unit testable. I've extracted an interface for retrieving resources: IResource.

Each time I wanted to use resources, I would get an implementation through IoC container.

var resource = Dependency.Resolve<IResource>();

Finally, when it came to using the resource, I would have something on the lines of:

resource.For<BankingWithdrawalResource>(p => p.WithdrawFunds)

In above line, I used lambda expression that allowed me to view all available resources through intellisense.

When it came to unit testing, I would mock IResource and set expectations on it.

For example:

resourceMock.Expect(a => a.For<BankingWithdrawalResource>(p => p.WithdrawFunds)).Repeat.Once();

Also notice that we have extracted an interface for resource retrieval. Because of this, we are now able to store resources anywhere we want. For example:

public class DatabaseResource : IResource
{
   // Logic to retrieve resources from database
   // Won't cause recompilation
}

public class XmlResource : IResource
{
   // Logic to retrieve resources from xml files
}

public class DefaultResource : IResource
{
   // Will cause recompilation as resources are stored in .resx
}

I wrote above from top of my head in notepad, so if you have any problems let me know and I'll add implementation later.

share|improve this answer
    
The dis-advantage to store resources in classes, is that each change/modification will require a re-build of the win-form application –  Yusubov Aug 30 '13 at 14:01
    
I am little confused please correct me if i am wrong. What i understood is, you are storing data in resource file and to retrieve it, you are using class. –  sandeep.gosavi Aug 30 '13 at 14:06
1  
You won't change classes as changing classes means changing resource keys. If you are changing resource keys, means that you didn't think it through the first time. What you might be changing is actual entries in the resource file, xml file or database. This will recompile the app. If your requirements don't allow for recompilation, then leave most of my example as it is, and implement a resource provider for the database or other source that won't cause the app to recompile. –  CodeART Aug 30 '13 at 14:06
1  
@sandeep.gosavi I have a class for storing resource keys and I have a class for retrieving resources. Advantage is that I no longer have to use "magic strings" each time I pull out resource, instead I get a list of available resources through intellisense. I can also easily replace my resource provider (from resx to database). Finally it's easily testable. –  CodeART Aug 30 '13 at 14:09
1  
ok i got it. Can you please provide me a demo code, because I am new in dotnet –  sandeep.gosavi Aug 30 '13 at 14:11
show 3 more comments

I have just done something similar following this CodeProject walkthrough: link

It is for making multiple easily updateable text files into resource files which you can add to your project in order to have different languages (in my case, a combo box allows me to change between as many languages as I create resource files for)

The advantage of this over .resx files is that you can have only 1 per language per project, instead of having to have 1 per language per form

Just something else to think about

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.