views:

81

answers:

3

I have an XML file that I deserialize into a class object. This class contains a number of other classes, some of which are arrays.

I would like to increase the array size of some of the objects.

How can I do this?

In the following example, the MyData object has an array size of 5 but the MyArrayClass say only has an array size of 1.

        for (int i = 0; i < 5; i++)
        {
            MyMainClass.MyAnotherClass.MyArrayClass[i] = MyData[i];
        }

This code thus fails because of the original array size of MyArrayClass. How do I increase it's size?

thank you.

A: 

I'm not very up on my C# deserialization, but does it have to be an array? Could you use some type of list instead? That way, you don't have to deal with the sizing.

Jeff Barger
I'm not certain generic collections serialize - I recall having a problem doing this by I don't remember what it is. Are you sure this will work?
Steve H.
No, I'm not sure it will work. It was just a thought.
Jeff Barger
+1  A: 

C# has manager memory - just delcare it a new array with size whatever you want. The garbage collector will clean up the old one for you if you don't need the data. If you do, then create a new one of the size you want, and copy all of the old data over in which ever method you prefer, then just assign over the old one with the new one and let the garbage collector clean up the old one.

Steve H.
Essentially, what I am trying to do is deserialize an XML file to a class object, get user input (which changes the array size of a couple classes within this object) and then serialize the same object. The catch is this object "MyMainClass" in the example above has tons of classes within it. I'm unsure how I would write the code to declare the main classes object with specific array sizes in some of the child classes. Currently, the deserialization process did all this for me. Thoughts? Thanks again.
CM
Just use accessors if that's a problem, or overload the constructor and allow size arguments.
Steve H.
Nice, that worked perfectly. Thanks.
CM
A: 

A List<T> should work just fine; having arrays on the object API is just going to cause pain:

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
public class Foo
{
    private List<Bar> bars = new List<Bar>();
    public List<Bar> Bars { get { return bars; } }
}

public class Bar {
    public string Name { get; set; }
}

static class Program
{
    static void Main()
    {
        Foo orig = new Foo { Bars = { new Bar { Name = "abc"},
            new Bar { Name = "def"}}}, clone;
        using(MemoryStream ms = new MemoryStream()) {
            XmlSerializer ser = new XmlSerializer(orig.GetType());
            ser.Serialize(ms, orig);
            ms.Position = 0;
            clone = (Foo)ser.Deserialize(ms);
        }
        clone.Bars.Add(new Bar { Name = "ghi" });
        foreach (Bar bar in clone.Bars)
        {
            Console.WriteLine(bar.Name);
        }
    }
}

Alternatively, you could write an append extension method, but this is not an efficient way to manipulate lists of data:

using System;
public class Foo
{
    public Bar[] Bars { get; set; }
}

public class Bar {
    public string Name { get; set; }
}

static class Program
{
    static T[] Append<T>(this T[] arr, T value)
    {
        Array.Resize<T>(ref arr, arr == null ? 1 : (arr.Length + 1));
        arr[arr.Length - 1] = value;
        return arr;
    }
    static void Main()
    {
        Foo obj = new Foo { Bars = new[] { new Bar { Name = "abc" }, new Bar { Name = "def" } } };
        obj.Bars = obj.Bars.Append(new Bar { Name = "ghi" });
        foreach (Bar bar in obj.Bars)
        {
            Console.WriteLine(bar.Name);
        }
    }
}
Marc Gravell
Thanks Marc, I may try this as an alternative to Steve's solution.
CM