Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
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
{
public partial class FrmHireCo : Form
/*
* This is the main form for the car hire system.
* It allows the adding to new cars to the system, and displaying them in the fleet list
*
*/
{
public Fleet myFleet = new Fleet();
private CustomerList NewCustomer = new CustomerList();
//Fleet object used to store cars
public FrmHireCo()
{
//Default constructor
InitializeComponent();
Stream stream = new FileStream(@"MyApplicationData.dat", System.IO.FileMode.Open);
IFormatter formatter = new BinaryFormatter();
Fleet myFleet = (Fleet)formatter.Deserialize(stream););
stream.Close();
}
private void updateFleetList()
{ //Used to update the fleet displayed on the form
lstFleet.Items.Clear();
foreach (Transport t in myFleet.fleet)
{
if (t is Car)
{
Car m = (Car)t;
lstFleet.Items.Add("Car: " + m.reg + " " + m.make + " " + m.model + " " + m.colour + "\t " + m.hirer);
}
else
if (t is Mini)
{
Mini m = (Mini)t;
lstFleet.Items.Add("Minibus: " + m.reg + " " + m.make + " " + m.model + " " + m.colour + "" + m.seat + "\t " + m.hirer);
}
else
if (t is Truck)
{
Truck m = (Truck)t;
lstFleet.Items.Add("Truck: " + m.reg + " " + m.make + " " + m.model + " " + m.colour + " " + m.weight + "\t " + m.hirer);
}
}
}
private void updatecustomerlist()
{
lstCustomers.Items.Clear();
foreach (Customer c in NewCustomer.customerlist)
{
lstCustomers.Items.Add("Customer ID :" + c.custid + " Customers Name:" + c.name + " Address:" + c.address);
}
}
private void btnAddCar_Click(object sender, EventArgs e)
{
//Add a new car
FrmCar carGui = new FrmCar(); //Form used to add new car
carGui.ShowDialog();
Car myCar = carGui.car; //Get new car from form
myFleet.addToFleet(myCar); //Add to fleet list
updateFleetList(); //Uodate fleet list
}
private void lstFleet_SelectedIndexChanged(object sender, EventArgs e)
{
/*
* This method is used to control the list box
* It is called when a row is selected by the user, it then displays frmCar
* with the car details
*/
if (lstFleet.SelectedIndex > -1)
{
int index = lstFleet.SelectedIndex;
Transport myCar = myFleet.fleet.ElementAt(index);
if (myCar is Car)
{
FrmCar carGui = new FrmCar();
carGui.car = (Car)myCar;
carGui.Show();
}
else
if (myCar is Mini)
{
FrmMini miniGui = new FrmMini();
miniGui.mini = (Mini)myCar;
miniGui.Show();
}
else
if (myCar is Truck)
{
FrmTruck truckGui = new FrmTruck();
truckGui.truck = (Truck)myCar;
truckGui.Show();
}
}
}
private void FrmHireCo_Load(object sender, EventArgs e)
{
}
private void FrmHireCo_Activated(object sender, EventArgs e)
{
updateFleetList();
updatecustomerlist();
}
private void btnAddMini_Click(object sender, EventArgs e)
{
//Add a new car
FrmMini miniGui = new FrmMini();
miniGui.ShowDialog();
Mini myMini = miniGui.mini;
myFleet.addToFleet(myMini);
updateFleetList();
}
private void btnAddTruck_Click(object sender, EventArgs e)
{
//Add a new car
FrmTruck truckGui = new FrmTruck();
truckGui.ShowDialog();
Truck myTruck = truckGui.truck;
myFleet.addToFleet(myTruck);
updateFleetList();
}
private void btnAddCustomer_Click(object sender, EventArgs e)
{
FrmCustomer customerGui = new FrmCustomer();
customerGui.ShowDialog();
Customer theCustomer = customerGui.customer;
NewCustomer.addToCustomerlist(theCustomer);
updatecustomerlist();
}
private void lstCustomers_SelectedIndexChanged(object sender, EventArgs e)
{
if (lstCustomers.SelectedIndex > -1)
{
int index = lstCustomers.SelectedIndex;
Customer selectedcust = NewCustomer.customerlist.ElementAt(index);
FrmCustomer customerGui = new FrmCustomer();
customerGui.customer = selectedcust;
customerGui.Show();
}
}
private void FrmHireCo_FormClosing(object sender, FormClosingEventArgs e)
{
// Create a new XmlSerializer instance with the type of the test class
// XmlSerializer SerializerObj = new XmlSerializer(typeof(TestClass));
// Create a new file stream to write the serialized object to a file
// TextWriter WriteFileStream = new StreamWriter(@"test.xml");
// SerializerObj.Serialize(WriteFileStream, TestObj);
// Cleanup
// WriteFileStream.Close();
Stream stream = new FileStream(@"MyApplicationData.dat", System.IO.FileMode.Create);
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, myFleet.theFleet);
stream.Close();
}
private void btnHire_Click(object sender, EventArgs e)
{
if (myFleet.fleet.Count == 0)
{
MessageBox.Show("No Vehicle's Exist Within The Fleet");
}
else
foreach (Transport t in myFleet.fleet)
{
if (txthirereg.Text == t.reg)
{
if (t.hirer == null)
{
t.hirer = txtcusthire.Text;
}
else
if (t.hirer == txtcusthire.Text)
{
MessageBox.Show("This Vehicle is Already Being Hired By This Person");
}
else
{
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");
}
}
else if (txthirereg.Text == (""))
{
MessageBox.Show("You Have Entered The 2 Required Detials");
}
else
{
MessageBox.Show("The Registration Number You Have Entered Does Not Exist Within The System");
}
updateFleetList();
}
}
private void btnReturn_Click(object sender, EventArgs e)
{
string search = txthirereg.Text;
if (myFleet.fleet.Count == 0)
{
MessageBox.Show("No vehicle Exist Within The Fleet");
}
else
{
foreach (Transport t in myFleet.fleet)
{
if (txthirereg.Text == t.reg)
{
if (t.hirer == txtcusthire.Text)
{
t.hirer = null;
}
else
{
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");
}
}
else
{
MessageBox.Show("The Registration Number You Have Entered Does Not Exist Within The System");
}
updateFleetList();
}
}
}
}
}
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);
}
public void savefleet()
{
}
}
}
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SD2coursework
{
[Serializable]
public class Transport
{
private string theRegno;
private string theMake; //Holds the value of the make property
private string theModel;
private string theColour; //Holds the value of the colour property
private string theHirer;
//Default constructor
public Transport(string aRegno, string aMake, string aModel, string aColour, string aHirer)
{
theRegno = aRegno;
theMake = aMake;
theModel = aModel;
theColour = aColour;
theHirer = aHirer;
}
//Make property
public string reg
{
get
{
return theRegno;
}
set
{
theRegno = value;
}
}
public string make
{
get
{
return theMake;
}
set
{
theMake = value;
}
}
public string model
{
get
{
return theModel;
}
set
{
theModel = value;
}
}
//Colour property
public string colour
{
get
{
return theColour;
}
set
{
theColour = value;
}
}
public string hirer
{
get
{
return theHirer;
}
set
{
theHirer = value;
}
}
}
}
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'
Comment