D
D
Dmitry2019-02-05 13:13:37
.NET
Dmitry, 2019-02-05 13:13:37

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

3 answer(s)
V
VoidVolker, 2019-02-05
@Lepeshka

For example:

class MyClass
{
    ...
    static class Strings
    {
        public static string str1 = "string 1";
    }
}

Or just put it in a separate file.

J
John_Nash, 2019-02-06
@John_Nash

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

It is not necessary to drag the form into the dll, the resources live perfectly without it
PS: moreover, by default, any net assembly contains information about the file version, and this information is stored in resources

D
Dmitry Korolev, 2019-02-13
@adressmoeistranici

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() { }
...
}

and one can consider the performance of readonly static string

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question