473,626 Members | 3,443 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.Collect ions.Generic.Li st`1[SD2coursework.T ransport]' to type 'SD2coursework. Fleet'
Nov 27 '10 #1
3 1827
NeoPa
32,567 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\Wi ndowsFormsAppli cation1\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
1850
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 causes ie problems. But when deserializing the xml into the object structure, the following exception is beeing thrown: There is an error in XML document (3, 701).
0
1059
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: <RulesConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="D:\Dev\Source\Miner.Data.Configuration\RulesConfig.xsd"> <RulePackage Key="SwitchingSteps" Caption="Switching Steps">
5
3154
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 distilled the problem into the following code. Can anybody reproduce/explain the problem? Thanks, Daniel
2
7961
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 viewstate and getting it back from the client on a postback. But the last line of the sample code below throws a "Stream was not readable" error when i'm trying to get the serialized object back out of the viewstate and memorystream. Any...
0
1260
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: public class MyClass{
2
971
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. Does anyone now how I can pass an object reference to the class when it is being deserialized. I am using the BinaryFormatter classes to serialize/deserialize the objects. Thanks Neal
2
1614
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 application is a RAD form designer to design screens for an embedded system and he only allowed for a single type of target system. My company now needs a designer for a 2nd type of system but because of the unmanaged serialization, any changes to the...
4
1459
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 - or better yet how to eliminate - the need to cast from an object when I have specifically filled the combo box with a List(of T) . To replicate this query, just create a new VB Windows application, put a button, a combo box and a text box on the...
0
2795
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 namespace. Some schemas extend complex types in others. When i get a soap:Fault from my test server it could contain a serialized exception object (called fault) from one of two of these schemas (and different namespaces, call them xmlns:b0="ns1" and...
1
1256
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 T : Domain { ...
0
8268
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8202
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8707
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8641
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8510
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7199
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4202
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1812
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1512
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.