Using the Settings Designer I can easily store individual items (string, int, double, etc...), but how do I store a list of items. I used the Browse... selection to set the Type to ArrayList, but I couldn't figure out how to set the default value. So, in the constructor of my main form, I added the following code...
Settings.Default.MyList = new ArrayList(); Settings.Default.MyList.Add( "abc"); Settings.Default.MyList.Add( "123"); Settings.Default.Save(); |
However, I get a null reference exception on the Save(). I worked around the problem by stuffing the list into a delimited string, but that's pretty ugly. It shouldn't be that difficult (or ugly). What am I missing

How to store list using Settings Designer (in 2.0)?
Walt Parkman
You may be glad to hear that we have added support for
System.Collections.Specialized.StringCollection
in the settings designer (that is, it is in the list of "well known" types in the type combobox).
The example that you showed in your post should work - it certainly does on my machine(s), but I have slightly newer versions than what was released for beta2 installed on them. I will try to give it a spin on beta2 and see if I can reproduce the issue there...
Best regards,
Johan Stenberg
GeoffP
mbudiman
if( Settings.Default.MyList == null)
{
Settings.Default.MyList = new ArrayList();
Settings.Default.MyList.Add( new Size( 800, 600));
Settings.Default.MyList.Add( new Size( 1024, 768));
try { Settings.Default.Save(); }
catch (Exception e) { Debug.WriteLine( e.Message + e.StackTrace); }
}
This outputs...
Object reference not set to an instance of an object. at System.Xml.XmlTextReaderImpl.InitStringInput(String baseUriStr, Encoding originalEncoding, String str)
at System.Xml.XmlTextReaderImpl..ctor(String xmlFragment, XmlNodeType fragType, XmlParserContext context)
at System.Xml.XmlLoader.CreateInnerXmlReader(String xmlFragment, XmlNodeType nt, XmlParserContext context, XmlDocument doc)
at System.Xml.XmlLoader.ParsePartialContent(XmlNode parentNode, String innerxmltext, XmlNodeType nt)
at System.Xml.XmlElement.set_InnerXml(String value)
at System.Configuration.LocalFileSettingsProvider.SerializeToXmlElement(SettingsProperty setting, SettingsPropertyValue value)
at System.Configuration.LocalFileSettingsProvider.SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection values)
at System.Configuration.SettingsBase.SaveCore()
at System.Configuration.SettingsBase.Save()
at System.Configuration.ApplicationSettingsBase.Save()
at SettingsTest.Form1..ctor() in C:\dev2\c#\SettingsTest\SettingsTest\Form1.cs:line 27
Chris_Kavanagh
This particular bug has been fixed in the current builds (you no longer get the NULL reference exception).
Unfortunately there will be other issues that you'll run into with this approach though - the reason for this is that the XML serializer that is created in order to serialize the ArrayList has no knowledge about objects of type System.Drawing.Size, so it'll still fail to serialize these values (You can try this behavior yourself by creating an XmlSerializer(typeof(System.Collections.ArrayList) and try to get it to serialize your array list from this example)
I believe that the only types that will work reliably as items in an ArrayList are types defined in mscorlib...
I would recommend using a setting of type System.Drawing.Size[] instead.
Best regards,
Johan Stenberg
Richard Purchas
johnlaus
Joel Bennett
aahd1