Okay, so my hurdle here is to Serialize a list that has a bunch of objects in them, write them to a file, and be able to open the file to load the list of objects back into a listbox. I've read the MSDN articles and browsed some Forums, but everything about this is way over my head, and I've never touched Serialization before. I've got everything laid out, I think, but some guidance would be greatly appreciated.
// Main.CS
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace AlfordScottFinalProgrammingProject
{
public partial class Main : Form
{
public Main()
{
InitializeComponent();
}
// Fields
public List<VehicleClass.Automobile> MyVehicles = new List<VehicleClass.Automobile>();
public VehicleClass.Car car = new VehicleClass.Car();
public VehicleClass.SportsCar sportscar = new VehicleClass.SportsCar();
public VehicleClass.Truck truck = new VehicleClass.Truck();
public VehicleClass.MiniVan minivan = new VehicleClass.MiniVan();
public bool isCar = false;
public bool isSportsCar = false;
public bool isTruck = false;
public bool isMiniVan = false;
// Save File Element
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
string SavePath = @"C:\CarFile\";
System.IO.Directory.CreateDirectory(SavePath);
if (!System.IO.Directory.Exists(SavePath))
{
MessageBox.Show("Please check permissions");
}
else
{
SavePath += ("My_Car_File");
SavePath = System.IO.Path.ChangeExtension(SavePath, ".xml");
if (System.IO.File.Exists(SavePath))
System.IO.File.Delete(SavePath);
}
}
// Load File Element
private void loadToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
DialogResult dr = ofd.ShowDialog();
if (dr != DialogResult.OK) return;
var x = new System.Xml.Serialization.XmlSerializer(typeof(VehicleClass));
using (var sw = new StreamReader(ofd.FileName))
{
// Load from File
}
}
// Exit Program Element
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
// Create New Car Button
public void btnNewCar_Click(object sender, EventArgs e)
{
CarForm NewCarDLG = new CarForm();
{
VehicleClass.Car myCar = new VehicleClass.Car();
NewCarDLG.ShowDialog();
myCar = NewCarDLG.getCar();
MyVehicles.Add(myCar);
lsbAutomobiles.Items.Add(myCar);
NewCarDLG.Dispose();
}
}
// Create New Sports Car Button
private void btnNewSportsCar_Click(object sender, EventArgs e)
{
SportsCarForm NewSportsCarDLG = new SportsCarForm();
{
VehicleClass.SportsCar mySportsCar = new VehicleClass.SportsCar();
NewSportsCarDLG.ShowDialog();
mySportsCar = NewSportsCarDLG.getSportsCar();
MyVehicles.Add(mySportsCar);
lsbAutomobiles.Items.Add(mySportsCar.ToString());
NewSportsCarDLG.Dispose();
}
}
// Create New Truck Button
private void btnNewTruck_Click(object sender, EventArgs e)
{
TruckForm NewTruckDLG = new TruckForm();
{
VehicleClass.Truck myTruck = new VehicleClass.Truck();
NewTruckDLG.ShowDialog();
myTruck = NewTruckDLG.getTruck();
MyVehicles.Add(myTruck);
lsbAutomobiles.Items.Add(myTruck.ToString());
NewTruckDLG.Dispose();
}
}
// Create New Minivan Button
private void btnNewMinivan_Click(object sender, EventArgs e)
{
MinivanForm NewMinivanDLG = new MinivanForm();
{
VehicleClass.MiniVan myMiniVan = new VehicleClass.MiniVan();
NewMinivanDLG.ShowDialog();
myMiniVan = NewMinivanDLG.getMiniVan();
MyVehicles.Add(myMiniVan);
lsbAutomobiles.Items.Add(myMiniVan.ToString());
NewMinivanDLG.Dispose();
}
}
// Listbox Selector
private void lsbAutomobiles_SelectedIndexChanged(object sender, EventArgs e)
{
// Car
if (MyVehicles[lsbAutomobiles.SelectedIndex]._type == "Car")
{
isCar = true;
btnStartVehicle.Enabled = true;
btnWashVehicle.Enabled = true;
btnDriveVehicle.Enabled = false;
btnParkVehicle.Enabled = false;
isSportsCar = false;
isTruck = false;
isMiniVan = false;
}
// Sports Car
if (MyVehicles[lsbAutomobiles.SelectedIndex]._type == "Sports Car")
{
isCar = false;
isSportsCar = true;
btnStartVehicle.Enabled = true;
btnWashVehicle.Enabled = true;
btnDriveVehicle.Enabled = false;
btnParkVehicle.Enabled = false;
isTruck = false;
isMiniVan = false;
}
// Truck
if (MyVehicles[lsbAutomobiles.SelectedIndex]._type == "Truck")
{
isCar = false;
isSportsCar = false;
isTruck = true;
btnStartVehicle.Enabled = true;
btnWashVehicle.Enabled = true;
btnDriveVehicle.Enabled = false;
btnParkVehicle.Enabled = false;
isMiniVan = false;
}
// Minivan
if (MyVehicles[lsbAutomobiles.SelectedIndex]._type == "MiniVan")
{
isCar = false;
isSportsCar = false;
isTruck = false;
isMiniVan = true;
btnStartVehicle.Enabled = true;
btnWashVehicle.Enabled = true;
btnDriveVehicle.Enabled = false;
btnParkVehicle.Enabled = false;
}
}
// Vehicle Actions - Start Vehicle
private void btnStartVehicle_Click(object sender, EventArgs e)
{
// Car - Start Vehicle
if (isCar == true)
{
car.StartVehicle();
btnDriveVehicle.Enabled = true;
btnStartVehicle.Enabled = false;
btnParkVehicle.Enabled = false;
}
// Sports Car - Start Vehicle
if (isSportsCar == true)
{
sportscar.StartVehicle();
btnDriveVehicle.Enabled = true;
btnStartVehicle.Enabled = false;
btnParkVehicle.Enabled = false;
}
// Truck - Start Vehicle
if (isTruck == true)
{
truck.StartVehicle();
btnDriveVehicle.Enabled = true;
btnStartVehicle.Enabled = false;
btnParkVehicle.Enabled = false;
}
// Minivan - Start Vehicle
if (isMiniVan == true)
{
minivan.StartVehicle();
btnDriveVehicle.Enabled = true;
btnStartVehicle.Enabled = false;
btnParkVehicle.Enabled = false;
}
}
// Vehicle Actions - Drive Vehicle
private void btnDriveVehicle_Click(object sender, EventArgs e)
{
// Car - Drive Vehicle
if (isCar == true)
{
car.Drive();
btnParkVehicle.Enabled = true;
btnStartVehicle.Enabled = false;
btnDriveVehicle.Enabled = false;
}
// Sports Car - Drive Vehicle
if (isSportsCar == true)
{
sportscar.Drive();
btnParkVehicle.Enabled = true;
btnStartVehicle.Enabled = false;
btnDriveVehicle.Enabled = false;
}
// Truck - Drive Vehicle
if (isTruck == true)
{
truck.Drive();
btnParkVehicle.Enabled = true;
btnStartVehicle.Enabled = false;
btnDriveVehicle.Enabled = false;
}
// Minivan - Drive Vehicle
if (isMiniVan == true)
{
minivan.Drive();
btnParkVehicle.Enabled = true;
btnStartVehicle.Enabled = false;
btnDriveVehicle.Enabled = false;
}
}
// Vehicle Actions - Drive Vehicle
private void btnParkVehicle_Click(object sender, EventArgs e)
{
// Car - Drive Vehicle
if (isCar == true)
{
car.Park();
btnStartVehicle.Enabled = true;
btnParkVehicle.Enabled = false;
btnDriveVehicle.Enabled = false;
}
// Sports Car - Park Vehicle
if (isSportsCar == true)
{
sportscar.Park();
btnStartVehicle.Enabled = true;
btnParkVehicle.Enabled = false;
btnDriveVehicle.Enabled = false;
}
// Truck - Park Vehicle
if (isTruck == true)
{
truck.Park();
btnStartVehicle.Enabled = true;
btnParkVehicle.Enabled = false;
btnDriveVehicle.Enabled = false;
}
// Minivan - Park Vehicle
if (isMiniVan == true)
{
minivan.Park();
btnStartVehicle.Enabled = true;
btnParkVehicle.Enabled = false;
btnDriveVehicle.Enabled = false;
}
}
// Vehicle Actions - Wash Vehicle
private void btnWashVehicle_Click(object sender, EventArgs e)
{
// Car - Wash Vehicle
if (isCar == true)
car.Wash();
// Sports Car - Wash Vehicle
if (isSportsCar == true)
sportscar.Wash();
// Truck - Wash Vehicle
if (isTruck == true)
truck.Wash();
// Minivan - Wash Vehicle
if (isMiniVan == true)
minivan.Wash();
}
}
}
---------------------------------------------------------------------------------------------------------------
// VehicleClass.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace AlfordScottFinalProgrammingProject
{
public class VehicleClass
{
// Abstract Class
public abstract class Vehicle
{
// Abstract Property
public int Year
{
get;
set;
}
// Abstract Method
public virtual void StartVehicle()
{
}
}
// Base Class
[Serializable()]
public class Automobile : Vehicle
{
// Fields
public string _type;
public int _year;
public string _make;
public string _model;
public string _color;
// Default Constructor
public Automobile()
{
}
// Constructor
public Automobile(string type, int year, string make, string model, string color)
{
_type = type;
_year = year;
_make = make;
_model = model;
_color = color;
}
// Type Property
public string Type
{
get { return _type; }
set { _type = value; }
}
// Year Property
public new int Year
{
get { return _year; }
set { _year = value; }
}
// Make Property
public string Make
{
get { return _make; }
set { _make = value; }
}
// Model Property
public string Model
{
get { return _model; }
set { _model = value; }
}
// Color Property
public string Color
{
get { return _color; }
set { _color = value; }
}
// Override StartVehicle Method
public override void StartVehicle()
{
MessageBox.Show("Automobile Started.");
}
// Drive Method
public virtual void Drive()
{
MessageBox.Show("Went for a nice drive in the Automobile.");
}
// Park Method
public virtual void Park()
{
MessageBox.Show("Parked the Automobile in the driveway");
}
// Wash Method
public virtual void Wash()
{
MessageBox.Show("Washed the Automobile. Looks Great!");
}
}
[Serializable()]
public class Car : Automobile
{
// Fields
public new string _type;
public int _doors;
// Default Constructor
public Car()
{
}
// Constructor
public Car(string type, int year, string make, string model, string color, int doors) : base (type, year, make, model, color)
{
_type = "Car";
_doors = doors;
}
// Type Property
public new string Type
{
get { return _type; }
set { _type = value; }
}
// Year Property
public new int Year
{
get { return _year; }
set { _year = value; }
}
// Make Property
public new string Make
{
get { return _make; }
set { _make = value; }
}
// Model Property
public new string Model
{
get { return _model; }
set { _model = value; }
}
// Color Property
public new string Color
{
get { return _color; }
set { _color = value; }
}
// Doors Property
public int Doors
{
get { return _doors; }
set { _doors = value; }
}
// Override StartVehicle Method
public new void StartVehicle()
{
MessageBox.Show("Car Started. It's Quiet!");
}
// Drive Car Method
public new void Drive()
{
MessageBox.Show("Went for a nice drive in the Car.");
}
// Park Car Method
public new void Park()
{
MessageBox.Show("Parked the Car in the driveway");
}
// Wash Car Method
public new void Wash()
{
MessageBox.Show("Washed the Car. Looks Great!");
}
public override string ToString()
{
return _year + " " + _make + " " + _model + " " + _color + " - " + _doors + " Door";
}
}
[Serializable()]
public class SportsCar : Car
{
// Fields
public new int _doors;
// Default Constructor
public SportsCar()
{
}
// Constructor
public SportsCar(string type, int year, string make, string model, string color, int doors) : base (type, year, make, model, color, doors)
{
_type = "Sports Car";
_doors = doors;
}
// Type Property
public new string Type
{
get { return _type; }
set { _type = value; }
}
// Year Property
public new int Year
{
get { return _year; }
set { _year = value; }
}
// Make Property
public new string Make
{
get { return _make; }
set { _make = value; }
}
// Model Property
public new string Model
{
get { return _model; }
set { _model = value; }
}
// Color Property
public new string Color
{
get { return _color; }
set { _color = value; }
}
// Doors Property
public new int Doors
{
get { return _doors; }
set { _doors = value; }
}
// Override StartVehicle Method
public new void StartVehicle()
{
MessageBox.Show("Sports Car Started. It's Loud!");
}
// Drive Sports Car Method
public new void Drive()
{
MessageBox.Show("Went for a nice drive in the Sports Car.");
}
// Park Sports Car Method
public new void Park()
{
MessageBox.Show("Parked the Sports Car in the driveway");
}
// Wash Sports Car Method
public new void Wash()
{
MessageBox.Show("Washed the Sports Car. Looks Great!");
}
public override string ToString()
{
return _year + " " + _make + " " + _model + " " + _color + " - " + _doors + " Door";
}
}
[Serializable()]
public class Truck : Automobile
{
// Field
public int _doors;
public int _driveType;
// Default Constructor
public Truck()
{
}
// Constructor
public Truck(string type, int year, string make, string model, string color, int doors, int driveType)
: base(type, year, make, model, color)
{
_type = "Truck";
_doors = doors;
_driveType = driveType;
}
// Type Property
public new string Type
{
get { return _type; }
set { _type = value; }
}
// Year Property
public new int Year
{
get { return _year; }
set { _year = value; }
}
// Make Property
public new string Make
{
get { return _make; }
set { _make = value; }
}
// Model Property
public new string Model
{
get { return _model; }
set { _model = value; }
}
// Color Property
public new string Color
{
get { return _color; }
set { _color = value; }
}
// Doors Property
public int Doors
{
get { return _doors; }
set { _doors = value; }
}
// Drive Type Property
public int DriveType
{
get { return _driveType; }
set { _driveType = value; }
}
// Override StartVehicle Method
public new void StartVehicle()
{
MessageBox.Show("Truck Started. It's Loud!");
}
// Drive Truck Method
public new void Drive()
{
MessageBox.Show("Went for a nice drive in the Truck.");
}
// Park Truck Method
public new void Park()
{
MessageBox.Show("Parked the Truck in the driveway");
}
// Wash Truck Method
public new void Wash()
{
MessageBox.Show("Washed the Truck. Looks Great!");
}
public override string ToString()
{
return _year + " " + _make + " " + _model + " " + _color + " - " + _doors + " Door - " + _driveType + "WD";
}
}
[Serializable()]
public class MiniVan : Automobile
{
// Fields
public int _doors;
public int _passengers;
// Default Constructor
public MiniVan()
{
}
// Constructor
public MiniVan(string type, int year, string make, string model, string color, int doors, int passengers)
: base(type, year, make, model, color)
{
_type = "MiniVan";
_doors = doors;
_passengers = passengers;
}
// Type Property
public new string Type
{
get { return _type; }
set { _type = value; }
}
// Year Property
public new int Year
{
get { return _year; }
set { _year = value; }
}
// Make Property
public new string Make
{
get { return _make; }
set { _make = value; }
}
// Model Property
public new string Model
{
get { return _model; }
set { _model = value; }
}
// Color Property
public new string Color
{
get { return _color; }
set { _color = value; }
}
// Doors Property
public int Doors
{
get { return _doors; }
set { _doors = value; }
}
// Passengers Property
public int Passengers
{
get { return _passengers; }
set { _passengers = value; }
}
// Override StartVehicle Method
public new void StartVehicle()
{
MessageBox.Show("Minivan Started. It's Quiet!");
}
// Drive Minivan Method
public new void Drive()
{
MessageBox.Show("Went for a nice drive in the Minivan.");
}
// Park Minivan Method
public new void Park()
{
MessageBox.Show("Parked the Minivan in the driveway");
}
// Wash Minivan Method
public new void Wash()
{
MessageBox.Show("Washed the Minivan. Looks Great!");
}
public override string ToString()
{
return _year + " " + _make + " " + _model + " " + _color + " - " + _doors + " Door - " + _passengers + " Passengers";
}
}
}
}