Answer the question
In order to leave comments, you need to log in
How to store text data in a C# application?
I am writing a program in c# (win forms). In the program, I have a class and I need to store a certain number (no more than a hundred) of lines that are used in this class. How it is better to implement it? External resources are not suitable - you need only an executable and that's it. Form resources are not appropriate because the data belongs to the class, not the form, and calling Properties.Resources from the class is a bad idea. There should be an opportunity then (suddenly it is required) to use a class separately from the form (napimer in dll) without dragging this data from somewhere. That is, everything should be compact. The data does not change.
The question arose because storing string constants directly in the class or in the code of methods is somehow not beautiful or something, so I decided to ask if there might be some beautiful way not to write a bunch of constants directly in the class. It may be more correct to make another class with data next to it or some kind of structure right inside the class. What do you advise?
Answer the question
In order to leave comments, you need to log in
For example:
class MyClass
{
...
static class Strings
{
public static string str1 = "string 1";
}
}
It should be possible later (suddenly needed) to use the class separately from the form (for example, in a dll) without dragging this data from somewhere
a static constructor can help to initialize static fields in the same class as the functional
static string replacement methods if required
class Base
{
private int field1;
...
...
#region Strings
private static string string1;
private static string string2;
private static string string3;
#region Group1
private static string string4;
private static string string5;
private static string string6;
#endregion
#endregion
static Base()
{
string1 = "string1";
string2 = "string2";
string3 = "string3";
//group1
string4 = "string4";
string5 = "string5";
string6 = "string6";
}
#endregion
public Base() { }
...
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question