0

After spending countless hours changing my code and removing everything that wont serialize and changing them to strings and writing functions to convert from Font/Color to strings and back again i've got fed up and decided to make my own basic serializer..

private void saveSettings()
{
    PropertyInfo[] properties = typeof(settingsObj).GetProperties();
    foreach (settingsObj s in settings)
    {
        foreach (PropertyInfo property in properties)
        {
            MessageBox.Show(s.[property.Name]);
        }
    }
}

How do i refer to a variable using a string, in PHP i'd do:

$varIWantToRead = "foobar";
$varName = "varIWantToRead";
print $$varName;
  • posible duplicate http://stackoverflow.com/questions/453161/best-practice-to-save-application-settings-in-a-windows-forms-application – spajce Jan 22 '13 at 19:12

3 Answers3

1

This can easily be done in the exe.config file. Here is a write up from Microsoft:

http://msdn.microsoft.com/en-us/library/aa730869.aspx

You could also create your own class or struct and serialize them into XML:

http://support.microsoft.com/kb/815813

Edit to answer your new question: Im not quite sure I know what your talking about but here we go:

In PHP: 
$varIWantToRead = "foobar";
$varName = "varIWantToRead";
print $$varName;


in c#: 
string varIWantToRead = "foobar"; 
string varName = varIWantToRead; 
Console.WriteLine(varName); // Outputs foobar
Botonomous
  • 1,746
  • 1
  • 16
  • 39
  • Yes thats exactly what i meant, in PHP its called a variable variable, i meant it as an example of what i'm after: `MessageBox.Show(s.[property.Name]);` that doesnt actually work.. – Dean Bayley Jan 23 '13 at 17:05
  • what type is s.[Property.Name] You may need to call .ToString() – Botonomous Jan 23 '13 at 17:33
0

You can use the default behaviour of winforms setting, Application Settings. Take a look at what microsoft says in brief and also goes into more details in another link

XML is a dynamic way of keeping data, there are lots of samples online like this tutorial and this microsoft link

Mahdi Tahsildari
  • 13,065
  • 14
  • 55
  • 94
  • I need to programatically add profiles and from what I can see you have to define all the settings at design time.. – Dean Bayley Jan 22 '13 at 19:26
  • well in the case of dynamic profiles you can use serialization/deserialization an XML to save user information in a flexible format. – Mahdi Tahsildari Jan 22 '13 at 19:36
  • That's what I'm trying to do, what object type can I use to store a class, so I can do something like: `profiles['profile1'].FontColor = Color.FromArgb(255, 0, 0, 0);` – Dean Bayley Jan 22 '13 at 19:42
0

This is exactly what i was looking for.

Thanks Guys.

Serializing Lists of Classes to XML

Community
  • 1
  • 1