How do i go about Serailizing A List

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • J William

    How do i go about Serailizing A List

    I Have A List (Fleet) Which Stores Instances of A Class (Transport) which is more specifficly (Car ,Minibus ,Truck)

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.IO;
    
    namespace SD2coursework
    {
        [Serializable()]
        public class Fleet
        {
            /*
             * This class is used to hold a list of Car objects that make up the fleet:
             * The car objects may be added through the addToFleet() method.
             * The car objects may be deleted tgrough the deleteFromFleet() method 
             * Use the fleet property to access the list of car objects
             */ 
    
            public List<Transport> theFleet = new List<Transport>(); //The list of car objects being stored
            public List<Transport> fleet 
                /* The fleet property. Note that you can only read it
                 * use the addToFleet and deleteFromFleet to update it
                 */
            {
                get
                {
                    return theFleet;
                }
            }
    
            public void deleteFromFleet(Transport a)
                //Delete car from fleet
            {
                theFleet.Remove(a);
            }
    
            public void addToFleet(Transport a)
                //Add car to fleet
            {
                theFleet.Add(a);
            }
    
        }
    }
    The Problems Occuring are , whilst attempting to use XML i have no parameterless contructor, and using binary it cannot explictly change Fleet to Transport
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    The reason you need a parameterless constructor is because when the object is being created, the framework calls the parameterless constructor and then calls various properties to set the information.

    Have a look here:



    I've found that link to be extremely valuable when trying to get XML Serialization working. There's also a section at the end for how to work with collection objects.

    Comment

    Working...