473,498 Members | 1,833 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I Cast When Deserializing A List

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Runtime.Serialization;
  10. using System.Runtime.Serialization.Formatters.Binary;
  11. using System.IO;
  12.  
  13.  
  14. namespace SD2coursework
  15. {
  16.     public partial class FrmHireCo : Form
  17.     /*
  18.      * This is the main form for the car hire system.
  19.      * It allows the adding to new cars to the system, and displaying them in the fleet list
  20.      * 
  21.      */
  22.     {
  23.         public Fleet myFleet = new Fleet();
  24.  
  25.  
  26.  
  27.  
  28.         private CustomerList NewCustomer = new CustomerList();
  29.         //Fleet object used to store cars
  30.  
  31.  
  32.         public FrmHireCo()
  33.         {
  34.             //Default constructor
  35.             InitializeComponent();
  36.  
  37.             Stream stream = new FileStream(@"MyApplicationData.dat", System.IO.FileMode.Open);
  38.             IFormatter formatter = new BinaryFormatter();
  39.             Fleet myFleet = (Fleet)formatter.Deserialize(stream););
  40.             stream.Close();
  41.         }
  42.  
  43.  
  44.         private void updateFleetList()
  45.         {   //Used to update the fleet displayed on the form
  46.             lstFleet.Items.Clear();
  47.             foreach (Transport t in myFleet.fleet)
  48.             {
  49.                 if (t is Car)
  50.                 {
  51.                     Car m = (Car)t;
  52.                     lstFleet.Items.Add("Car: " + m.reg + " " + m.make + " " + m.model + " " + m.colour + "\t " + m.hirer);
  53.                 }
  54.                 else
  55.                     if (t is Mini)
  56.                     {
  57.                         Mini m = (Mini)t;
  58.                         lstFleet.Items.Add("Minibus: " + m.reg + " " + m.make + " " + m.model + " " + m.colour + "" + m.seat + "\t " + m.hirer);
  59.                     }
  60.                     else
  61.                         if (t is Truck)
  62.                         {
  63.                             Truck m = (Truck)t;
  64.                             lstFleet.Items.Add("Truck: " + m.reg + " " + m.make + " " + m.model + " " + m.colour + " " + m.weight + "\t " + m.hirer);
  65.                         }
  66.             }
  67.         }
  68.  
  69.         private void updatecustomerlist()
  70.         {
  71.             lstCustomers.Items.Clear();
  72.             foreach (Customer c in NewCustomer.customerlist)
  73.             {
  74.                 lstCustomers.Items.Add("Customer ID :" + c.custid + " Customers Name:" + c.name + " Address:" + c.address);
  75.             }
  76.  
  77.         }
  78.  
  79.  
  80.  
  81.  
  82.         private void btnAddCar_Click(object sender, EventArgs e)
  83.         {
  84.             //Add a new car
  85.             FrmCar carGui = new FrmCar(); //Form used to add new car
  86.             carGui.ShowDialog();
  87.             Car myCar = carGui.car;         //Get new car from form
  88.             myFleet.addToFleet(myCar);      //Add to fleet list
  89.             updateFleetList();              //Uodate fleet list
  90.         }
  91.  
  92.         private void lstFleet_SelectedIndexChanged(object sender, EventArgs e)
  93.         {
  94.             /*
  95.              * This method is used to control the list box
  96.              * It is called when a row is selected by the user, it then displays frmCar
  97.              * with the car details
  98.              */
  99.             if (lstFleet.SelectedIndex > -1)
  100.             {
  101.                 int index = lstFleet.SelectedIndex;
  102.                 Transport myCar = myFleet.fleet.ElementAt(index);
  103.  
  104.                 if (myCar is Car)
  105.                 {
  106.                     FrmCar carGui = new FrmCar();
  107.                     carGui.car = (Car)myCar;
  108.                     carGui.Show();
  109.                 }
  110.                 else
  111.                     if (myCar is Mini)
  112.                     {
  113.                         FrmMini miniGui = new FrmMini();
  114.                         miniGui.mini = (Mini)myCar;
  115.                         miniGui.Show();
  116.                     }
  117.                     else
  118.                         if (myCar is Truck)
  119.                         {
  120.                             FrmTruck truckGui = new FrmTruck();
  121.                             truckGui.truck = (Truck)myCar;
  122.                             truckGui.Show();
  123.                         }
  124.             }
  125.         }
  126.  
  127.         private void FrmHireCo_Load(object sender, EventArgs e)
  128.         {
  129.  
  130.         }
  131.  
  132.         private void FrmHireCo_Activated(object sender, EventArgs e)
  133.         {
  134.             updateFleetList();
  135.             updatecustomerlist();
  136.         }
  137.  
  138.         private void btnAddMini_Click(object sender, EventArgs e)
  139.         {
  140.             //Add a new car
  141.             FrmMini miniGui = new FrmMini();
  142.             miniGui.ShowDialog();
  143.             Mini myMini = miniGui.mini;
  144.             myFleet.addToFleet(myMini);
  145.             updateFleetList();
  146.         }
  147.  
  148.         private void btnAddTruck_Click(object sender, EventArgs e)
  149.         {
  150.             //Add a new car
  151.             FrmTruck truckGui = new FrmTruck();
  152.             truckGui.ShowDialog();
  153.             Truck myTruck = truckGui.truck;
  154.             myFleet.addToFleet(myTruck);
  155.             updateFleetList();
  156.         }
  157.  
  158.         private void btnAddCustomer_Click(object sender, EventArgs e)
  159.         {
  160.             FrmCustomer customerGui = new FrmCustomer();
  161.             customerGui.ShowDialog();
  162.             Customer theCustomer = customerGui.customer;
  163.             NewCustomer.addToCustomerlist(theCustomer);
  164.             updatecustomerlist();
  165.  
  166.  
  167.         }
  168.  
  169.         private void lstCustomers_SelectedIndexChanged(object sender, EventArgs e)
  170.         {
  171.             if (lstCustomers.SelectedIndex > -1)
  172.             {
  173.  
  174.                 int index = lstCustomers.SelectedIndex;
  175.                 Customer selectedcust = NewCustomer.customerlist.ElementAt(index);
  176.                 FrmCustomer customerGui = new FrmCustomer();
  177.                 customerGui.customer = selectedcust;
  178.                 customerGui.Show();
  179.             }
  180.  
  181.         }
  182.  
  183.         private void FrmHireCo_FormClosing(object sender, FormClosingEventArgs e)
  184.         {
  185.             // Create a new XmlSerializer instance with the type of the test class
  186.             // XmlSerializer SerializerObj = new XmlSerializer(typeof(TestClass));
  187.             // Create a new file stream to write the serialized object to a file
  188.             // TextWriter WriteFileStream = new StreamWriter(@"test.xml");
  189.             // SerializerObj.Serialize(WriteFileStream, TestObj);
  190.             // Cleanup
  191.             // WriteFileStream.Close();
  192.  
  193.             Stream stream = new FileStream(@"MyApplicationData.dat", System.IO.FileMode.Create);
  194.             IFormatter formatter = new BinaryFormatter();
  195.             formatter.Serialize(stream, myFleet.theFleet);
  196.  
  197.             stream.Close();
  198.  
  199.  
  200.  
  201.         }
  202.  
  203.  
  204.  
  205.         private void btnHire_Click(object sender, EventArgs e)
  206.         {
  207.             if (myFleet.fleet.Count == 0)
  208.             {
  209.                 MessageBox.Show("No Vehicle's Exist Within The Fleet"); 
  210.             }
  211.  
  212.             else
  213.             foreach (Transport t in myFleet.fleet)
  214.             {
  215.                 if (txthirereg.Text == t.reg)
  216.                 {
  217.  
  218.                     if (t.hirer == null)
  219.                     {
  220.                         t.hirer = txtcusthire.Text;
  221.                     } 
  222.                     else
  223.                         if (t.hirer == txtcusthire.Text)
  224.                         {
  225.                             MessageBox.Show("This Vehicle is Already Being Hired By This Person");
  226.                         }
  227.                         else
  228.                         {
  229.                             MessageBox.Show("The Customer Number You Have Entered Does Not Exist Within The System \n\n OR \n\n The Vechile Is Already Hired Out");
  230.                         }
  231.                 }
  232.                 else if (txthirereg.Text == (""))
  233.                 {
  234.                     MessageBox.Show("You Have Entered The 2 Required Detials");
  235.                 }
  236.  
  237.                 else
  238.                 {
  239.                     MessageBox.Show("The Registration Number You Have Entered Does Not Exist Within The System");
  240.                 }
  241.  
  242.                 updateFleetList();
  243.  
  244.             }
  245.         }
  246.  
  247.         private void btnReturn_Click(object sender, EventArgs e)
  248.         {
  249.  
  250.             string search = txthirereg.Text;
  251.  
  252.             if (myFleet.fleet.Count == 0)
  253.             {
  254.                 MessageBox.Show("No vehicle Exist Within The Fleet");
  255.             }
  256.  
  257.             else
  258.             {
  259.                 foreach (Transport t in myFleet.fleet)
  260.                 {
  261.                     if (txthirereg.Text == t.reg)
  262.                     {
  263.                         if (t.hirer == txtcusthire.Text)
  264.                         {
  265.                             t.hirer = null;
  266.  
  267.                         }
  268.                         else
  269.                         {
  270.                             MessageBox.Show("The Customer Number You Have Entered Does Not Exist Within The System \n\n OR \n\n The Vechile Is Already Hired Out");
  271.                         }
  272.                     }
  273.                     else
  274.                     {
  275.                         MessageBox.Show("The Registration Number You Have Entered Does Not Exist Within The System");
  276.                     }
  277.  
  278.                     updateFleetList();
  279.                 }
  280.             }
  281.         }
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.     }
  293. }
  294.  
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. using System.Runtime.Serialization;
  7. using System.Runtime.Serialization.Formatters.Binary;
  8. using System.IO;
  9.  
  10. namespace SD2coursework
  11. {
  12.     [Serializable()]
  13.     public class Fleet
  14.     {
  15.         /*
  16.          * This class is used to hold a list of Car objects that make up the fleet:
  17.          * The car objects may be added through the addToFleet() method.
  18.          * The car objects may be deleted tgrough the deleteFromFleet() method 
  19.          * Use the fleet property to access the list of car objects
  20.          */ 
  21.  
  22.         public List<Transport> theFleet = new List<Transport>(); //The list of car objects being stored
  23.         public List<Transport> fleet 
  24.             /* The fleet property. Note that you can only read it
  25.              * use the addToFleet and deleteFromFleet to update it
  26.              */
  27.         {
  28.             get
  29.             {
  30.                 return theFleet;
  31.             }
  32.         }
  33.  
  34.         public void deleteFromFleet(Transport a)
  35.             //Delete car from fleet
  36.         {
  37.             theFleet.Remove(a);
  38.         }
  39.  
  40.         public void addToFleet(Transport a)
  41.             //Add car to fleet
  42.         {
  43.             theFleet.Add(a);
  44.         }
  45.  
  46.         public void savefleet()
  47.         {
  48.  
  49.  
  50.         }
  51.  
  52.     }
  53. }
  54.  
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace SD2coursework
  7. {
  8.     [Serializable]
  9.     public class Transport
  10.     {
  11.         private string theRegno;
  12.         private string theMake;  //Holds the value of the make property
  13.         private string theModel;
  14.         private string theColour; //Holds the value of the colour property
  15.         private string theHirer;
  16.  
  17.  
  18.  
  19.  
  20.  
  21.         //Default constructor
  22.         public Transport(string aRegno, string aMake, string aModel, string aColour, string aHirer)
  23.         {
  24.             theRegno = aRegno;
  25.             theMake = aMake;
  26.             theModel = aModel;
  27.             theColour = aColour;
  28.             theHirer = aHirer;
  29.  
  30.         }
  31.  
  32.         //Make property
  33.         public string reg
  34.         {
  35.             get
  36.             {
  37.                 return theRegno;
  38.             }
  39.             set
  40.             {
  41.                 theRegno = value;
  42.             }
  43.         }
  44.         public string make
  45.         {
  46.             get
  47.             {
  48.                 return theMake;
  49.             }
  50.             set
  51.             {
  52.                 theMake = value;
  53.             }
  54.         }
  55.         public string model
  56.         {
  57.             get
  58.             {
  59.                 return theModel;
  60.             }
  61.             set
  62.             {
  63.                 theModel = value;
  64.             }
  65.         }
  66.         //Colour property
  67.         public string colour
  68.         {
  69.             get
  70.             {
  71.                 return theColour;
  72.             }
  73.             set
  74.             {
  75.                 theColour = value;
  76.             }
  77.         }
  78.  
  79.         public string hirer
  80.         {
  81.             get
  82.             {
  83.                 return theHirer;
  84.             }
  85.             set
  86.             {
  87.                 theHirer = value;
  88.             }
  89.         }
  90.     }
  91.  
  92. }
  93.  
  94.  
Hey Guys,
Been Stuck On Serializing This For A While Ive Gotten Round parameters by using binary. im new to using this so i dont have a clue on how to correct the casting error, wondering if anyone can help

The Exact Error Is As Follows,

Unable to cast object of type 'System.Collections.Generic.List`1[SD2coursework.Transport]' to type 'SD2coursework.Fleet'
Nov 27 '10 #1
3 1822
NeoPa
32,557 Recognized Expert Moderator MVP
Someone's going to want the line number of the code that errored too.
Nov 27 '10 #2
Line 39 On The First Piece Of Code, Sorry About THAT
Nov 27 '10 #3
I Have Also Tried Using Transport Instead Of Fleet And Error Says Similiar

"Error 1 Cannot implicitly convert type 'SD2coursework.Transport' to 'SD2coursework.Fleet' G:\Software Deelopment Coursework 2\Assessment\WindowsFormsApplication1\FrmHire.cs 39 30 SD2coursework
"
Nov 27 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1832
by: Thomas | last post by:
Hi, I implemented a composite pattern which should be serializable to xml. After spending some time in the newsgroups, i finally managed serializing, even with utf-8 instead of utf-16, which...
0
1046
by: Jon Fairchild | last post by:
I am getting the following error when deserializing an XML with attribute overrides: "There is an error in XML document (2, 2) … <RulesConfig xmlns=''> was not expected" My XML looks like this:...
5
3143
by: Daniel Gackle | last post by:
I'm getting a strange ArgumentNullException after deserializing a SortedList. Haven't seen this discussed in the newsgroups, but it looks like a bug - unless I missed something obvious? I've...
2
7928
by: ce | last post by:
Being a newbie regarding serialization and memorystreams, I was trying to see if i could improve page performance (avoiding going to the db on a postback) by saving my serialized business object in...
0
1250
by: Chris Newby | last post by:
Given: public class MyClass{ public String MyPropertyOne; } I have a soap document created by serializing an instance of a previous version of MyClass. However, now MyClass looks like: ...
2
964
by: Neal Andrews | last post by:
Hi All, I have an object that gets serialized, now when the object gets deserialized I what to be able to pass it an object reference which is not serialized and is used internally by the class....
2
1604
by: Chukkalove | last post by:
I'm sorry this message is so long. I have had to make some changes to an application written by a previous developer who used unmanaged serialization to store complex objects to file. The...
4
1446
by: dmac | last post by:
Below is some really simple code - its just a trivial class used to populate a combo box from which I want to pull out one of the properties of the selected object. I am just curious to know why -...
0
2782
by: theonlydavewilliams | last post by:
Hi there I'm hoping there's an easy answer to a (hopefully) not too long-winded issue... I'm building a C# web client using a proxy wsdl.exe'd from a wsdl file and six schemas, each in a different...
1
1244
by: Jon Skeet [C# MVP] | last post by:
On Jun 26, 2:11 pm, "RichB" <ri...@community.nospamwrote: <snip> Yes, there's a way to do it - you need to make the method generic: public bool IsChildListValid<T>(List<TdomainList) where...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7004
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7167
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7208
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6890
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4915
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3095
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
657
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
292
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.