TypeConverter for ArrayList

Discussion in 'ASP .Net Building Controls' started by news.microsoft.com, Mar 20, 2007.

  1. Hi,

    I'm trying to write TypeConverter which converts ArrayList to a
    comma-separated string and back which can be stored in the attribute of
    control's tag. In simplified form I have a property:

    private ArrayList _test;
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    [TypeConverter(typeof(ArrayListConverter))]
    [NotifyParentProperty(true)]
    public ArrayList Test
    {
    get
    {
    if (this._test == null)
    this._test = new ArrayList();
    return this._test;
    }
    set
    {
    this._test = value;
    }
    }

    And a type converter class:

    public class ArrayListConverter : TypeConverter
    {

    //
    // skipped
    //

    public override object ConvertTo(ITypeDescriptorContext context,
    System.Globalization.CultureInfo culture, object value, Type
    destinationType)
    {
    if (value != null && !(value is ArrayList))
    throw new ArgumentException("Invalid ArrayList object",
    "value");

    if (destinationType == typeof(string))
    {
    if (value == null)
    return String.Empty;

    ArrayList colors = (ArrayList)value;
    string[] strColors = new string[colors.Count];
    for (int i = 0; i < colors.Count; i++)
    strColors = colors.ToString();

    return String.Join(culture.TextInfo.ListSeparator,
    strColors);
    }
    if (destinationType == typeof(InstanceDescriptor) && value is
    ArrayList)
    {
    ArrayList clrs = (ArrayList)value;

    ConstructorInfo cif = typeof(ArrayList).GetConstructor(new
    Type[] { typeof(ICollection) });
    if (cif != null)
    return new InstanceDescriptor(cif, new object[] {
    clrs }, false);
    }

    return base.ConvertTo(context, culture, value, destinationType);
    }

    //
    // skipped
    //
    }

    With this code I can edit the "Test" property in design mode via properties
    window and as a string in the controls tag but when I try to build the site
    I get "Object reference not set to an instance of an object" error. If I
    change:

    ConstructorInfo cif = typeof(ArrayList).GetConstructor(new
    Type[] { typeof(ICollection) });
    if (cif != null)
    return new InstanceDescriptor(cif, new object[] {
    clrs }, false);

    to

    ConstructorInfo cif =
    typeof(ArrayList).GetConstructor(Type.EmptyTypes);
    if (cif != null)
    return new InstanceDescriptor(cif, null, false);

    I don't get the error but tag is rendered to simple

    @__ctrl.Test = new System.Collections.ArrayList();

    which doesn't set anything from the attributes value.

    What am I doing wrong?

    Thank you,
    Alan.
     
    news.microsoft.com, Mar 20, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christian
    Replies:
    1
    Views:
    467
    Gary Varga
    Jul 18, 2003
  2. Saravanan Rathinavelu

    Iterate through ArrayList using an another ArrayList

    Saravanan Rathinavelu, Aug 16, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    2,744
    Natty Gur
    Aug 19, 2003
  3. Kaidi
    Replies:
    4
    Views:
    2,387
    Kaidi
    Jan 3, 2004
  4. xz
    Replies:
    16
    Views:
    2,379
  5. Philipp
    Replies:
    6
    Views:
    928
    Arne Vajhøj
    May 28, 2008
Loading...

Share This Page