Store ArrayList as Settings Object

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

I see quite a few questions, but no valid responses to the question...

How do I store an ArrayList of items in a Properties file via the GUI?

I am able to select the System.Collections.ArrayList type, but I'm unable to
determine what to type into the Value feild for a default setting so the
property will not be null at runtime...

Thanks,
Mike
 
Hi Mike,

When we add a setting in the Settings tab in a project's Project designer
and select System.Collections.ArrayList as the type of the setting, we
could click the cell under the Value column in the same row and click the
button on the right to edit the items in the collection.

In the Object Collection Editor dialog, if we click Add button, a new item
of type System.Object is added to the collection. However, we couldn't
modify the value of the object, because the properties grid on the right
hand is grey. Click ok button and close the dialog. We could see an xml
text appears in the Value cell. The content of the xml text is like below.

<?xml version="1.0" encoding="utf-16"?>
<ArrayOfAnyType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<anyType/>
</ArrayOfAnyType>

So we could only modify content in the <anyType> node in the above xml text
to specify the default values of items in the ArryList setting. The
following is a sample.

<?xml version="1.0" encoding="utf-16"?>
<ArrayOfAnyType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<anyType xsi:type="xsd:int">1</anyType>
<anyType xsi:type="xsd:string">2</anyType>
<anyType
xsi:type="xsd:dateTime">2006-09-06T10:58:55.4961857+08:00</anyType>
</ArrayOfAnyType>

Copy the new xml text back to the Value cell of the setting.

In the above sample, three items are added in the collection, among which
the first item is of type Int32 with the value of 1 and the second item is
of type String with the value of "2" and the third item is of type DateTime
with the value of "2006-09-06 10:58:55".

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Ok.. I understand and have that working... now if I want the ArrayList to be
an ArrayList of a custom class I write...

Do I just mark the class Serializable and then select that type?
 
Hi Mike,

When we add a setting in the graphic designer and click the arrow button in
the Type column to select a type, we will see some simple type in the
drop-down list. If the required type does not appear in this list by
default, you can choose one by clicking the Browse list option to open the
Select a Type dialog.

The only items that appear in this list are .NET Framework types that can
be serialized, whether by TypeConverter(by converting to and from a string)
or with the XMLSerializer.

The type System.Collections.ArrayList uses XMLSerializer to serialize. So
an XML text appears in the value cell for a setting of type ArrayList. We
could modify the XML text to add elements of simple type, such as string,
integer, datetime, etc. However, if we want to add an element of a complex
type, e.g a custom type, it's not enough to set the custom type
Serializable. We may need to make a use of XMLSerializer to do this. I am
researching on how to do this now and will get it back to you ASAP.

On the other hand, you could also add a setting of your custom types,
although they'll need a TypeConverter just as the .NET Framework types do.

To do this, we should derive a new class from TypeConverter and apply it to
the custom type. For more information on how to implement TypeConverter,
you may refer to the link as follow.

http://msdn2.microsoft.com/en-us/library/ayybcxe5.aspx

Hope this helps.


Sincerely,
Linda Liu
Microsoft Online Community Support
 
Back
Top