I have a number of plain text strings that I need to allow a user of my program to change, what is the usual/best way of doing something like this?

I was considering using C# properties for them all (and a "edit properties page") but I'm not sure that's the best solution.

Recommended Answers

All 8 Replies

Depends on what you like to change in the text strings. Have a look at the methods of the string class.

My question was referring to the best way to store these strings, as in, in a text file or as C# properties or some XML-ness that I don't know how to use?

If you need data in run-time, then sure best option is to keep the data inisde classes, this is like you said using properties. If you want to store data for a longer run (when you close and re-open application), then its best to use xml or some files (ini, txt), or even thining of using dataBase.

Since you didnt specficly explained your needs, I cannot advice you better, so far.

To expand on Mitja's post, C# properties are not a way of storing information. They're effectively a way to access member fields of a class. There is nothing written to disk, so when you shut your app, the properties are gone.

That being said, however, you can store the entire class data as an XML file using the XmlSerializer class. This may be what you would prefer to do, especially if you only have one instance of your class, you can use this as a type of configuration file.
To use the XML Serializer though, you would not be able to use properties (unless they linked to private fields)

C# Property:

public class MyClass
{
  public String MyProperty { get;set; } // No need to set a field as one is implied here.
}

To use properties with the XML Serializer...

[Serializable]
public class MyClass
{
  public String MyProperty { get { return _myField; } set { _myField = value; } } // A benefit of using a property is that we can treat the get and set like methods. Inside which we can validate input and output. ie. get { if(String.IsNullOrWhitespace(_myField) return "empty"; else return _myField; }
  private String _myField; // We must have a field for the property to access as we have defined out own accessors. As properties themselves do not store data, we need to have somewhere to put the data for our serialiser to read. Also, I recommend setting a default value for when the class is instantiated.
}

So there you go. If you have time to investigate, I would look into XML Serialisation, it's a very handy tool.
Otherwise, use a text file or database (depending on whether it warrants the overhead of a dedicated database)

It really comes down to the lifespan of the data you're needing to keep track of. If you only need the data for the lifespan of the application then you can probably just keep it in memory via class properties, but if you need to persist the data then you'll need to make use of xml, database or some other storage medium.

Darn, I meant to call them "settings" instead of properties, I was pretty tired that day sorry.

<shrug>

The way I do it is to use a singleton that stores all my configuration data. This singleton is then serialised to disk using the XML serialiser.

As I'm starting to use mostly wpf, I use properties to access private members. In the setter for the property I raise an OnPropertyChanged event which notifies my application that this property has changed. This is useful for doing things such as updating the UI when a certain value changes or setting some code into action based on a certain property etc.

I didn't understand your qusetion sorry...

commented: I fail to see the point with this post. Original question was clear enough and was later re-clarified in the thread. -2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.