How to store list using Settings Designer (in 2.0)?

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




Answer this question

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

    just stupid noob suggestion not worth staying around :|

  • mbudiman

    OK, my example was slightly oversimplified.  This example fails.  I'm guessing it's because Size is a value type not a reference type, but doesn't it get boxed   If so, why the problem   Note that System.Drawing.Size is in the list of types that you can select without having to "Browse".



    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

    Ok, sorry about that, I don't know how to help you then :( still learning ;)

  • johnlaus

    Thanks, Johan.  Your comments made me try to reproduce it in as simple a project as I could, so I created a new project and tried to reproduce.  Tongue Tied  Hmmm... it worked!  Thanks for the idea.  Now, I'll have to go back and see if I can reproduce it again in my actual project.

  • Joel Bennett

    Yep.  That was a shot in the dark.  MyList is a property, not a class.

  • aahd1

    I had tried "string[]" but got an error message saying "string[]" was not defined, so I assumed I couldn't enter an array.  I didn't think to fully qualify it.  I just now tried "System.Drawing.Size[]" and sure enough it worked.  Thanks!

  • How to store list using Settings Designer (in 2.0)?