I'm trying to figure out how to do this more dynamically. Right now I save each and every form field individually/manually. I would love to maybe have some kind of master form list that I could loop through. I'm fairly new to C#, so I don't know many tricks yet. Please let me know what you think.
Here is my simplified version of my code:
Note: None of the variable names are numbered in my real code. I changed them for simplicity in my example. So I can't loop through form names by iterating.
//BINDED FORM COLLECTION
public class FormLink
{
private string _fObj1;
private string _fObj2;
private string _fObj3;
private string _fObj4;
private bool _fObj5;
private bool _fObj6;
private bool _fObj7;
public string fObj1
{
get
{
return this._fObj1;
}
set
{
this._fObj1 = value;
}
}
public string fObj2 { /*...*/ }
public string fObj3 { /*...*/ }
public string fObj4 { /*...*/ }
public bool fObj5 { /*...*/ }
public bool fObj6 { /*...*/ }
public bool fObj7 { /*...*/ }
}
//SETTINGS HANDLE
public class Settings
{
private string SettingsFile = "settings.xml";
FormLink form;
public Settings(FormLink form)
{
this.form = form;
}
public void iStart()
{
if (!File.Exists(this.SettingsFile))
{
this.createDefaultsFile();
}
this.iLoad();
}
public void iEnd()
{
this.alterNodeValue(this.SettingsFile, "Settings", "fObj1", this.form.fObj1.ToString());
this.alterNodeValue(this.SettingsFile, "Settings", "fObj2", this.form.fObj2.ToString());
this.alterNodeValue(this.SettingsFile, "Settings", "fObj3", this.form.fObj3.ToString());
this.alterNodeValue(this.SettingsFile, "Settings", "fObj4", this.form.fObj4.ToString());
this.alterNodeValue(this.SettingsFile, "Settings", "fObj5", this.form.fObj5.ToString());
this.alterNodeValue(this.SettingsFile, "Settings", "fObj6", this.form.fObj6.ToString());
this.alterNodeValue(this.SettingsFile, "Settings", "fObj7", this.form.fObj7.ToString());
}
private void createDefaultsFile()
{
XDocument xml = new XDocument(
new XElement("Settings",
new XElement("fObj1", "string"),
new XElement("fObj2", "string"),
new XElement("fObj3", "string"),
new XElement("fObj4", "string"),
new XElement("fObj5", false),
new XElement("fObj6", false),
new XElement("fObj7", false),
));
xml.Save(this.SettingsFile, SaveOptions.None);
}
private void iLoad()
{
var settings = this.getNodes(XDocument.Load(this.SettingsFile, "Settings");
this.form.fObj1 = Help.getDictVal(settings, "fObj1");
this.form.fObj1 = Help.getDictVal(settings, "fObj2");
this.form.fObj1 = Help.getDictVal(settings, "fObj3");
this.form.fObj1 = Help.getDictVal(settings, "fObj4");
this.form.fObj1 = Help.getDictVal(settings, Help.stringToBool("fObj5"));
this.form.fObj1 = Help.getDictVal(settings, Help.stringToBool("fObj6"));
this.form.fObj1 = Help.getDictVal(settings, Help.stringToBool("fObj7"));
}
private void alterNodeValue(string xmlFile, string parent, string node, string newVal)
{
/* Alters a single XML Node and saves XML File */
}
private Dictionary<string, string> getNodes(XDocument xml, string parent)
{
/* Retrieves all Child Nodes of specified Parent and returns them in a Dictionary */
}
}
//Basic Utilies class
public static class Help
{
public static bool stringToBool(string BoolMe)
{
/* Safely converts String to Bool */
}
public static string getDictVal(Dictionary<string, string> dict, string key)
{
/* Safely gets value from Dictionay based on Key*/
}
}