Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Serialize/Deserialize file

Status
Not open for further replies.

egil84

Programmer
Oct 19, 2003
13
Gosh, I wished I was as good as you guys are. Anyway, here's the problem:

I have a form that once you click on the bottom, it passes the information about a cruise into an array, and from there I store that value to an ArrayList, which is where I have all of the cruises information stored. Then I want to serialize it. If the file where I'll store the cruises information is not created, it will create it, and then save the information from my ArrayList to this file, which is called Cruise.bin.

Code:
//load values to array
string [] dataSetArray = {variable1, variable2, variable3};

//pass array to method Add
oDataStore.Add(dataSetArray);//oDataStore is the object instantiated out of DataStoreCruise

//here is my DataStore Namespace

using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using IPropertyNamespace //this is where I have all my Get and Set Methods

namespace DataStoreCruiseNamespace
{
[Serializable()]
class DataStoreCruise
{
public ArrayList propertyList;

public DataStoreCruise
{
  try
  {
  IFormatter formatter = new BinatyFormatter();
  Stream stream = new FileStream("Cruise.bin",FileMode.Open)
  propertyList = (ArrayList)formatter.Deserialize(stream);
  stream.Close();
  }
  catch(Exception)
  {
  IFormatter formatter = new BinatyFormatter();
  Stream stream = new FileStream("Cruise.bin",FileMode.Create);
  formatter.Serialize(stream, propertyList)
  stream.Close();
  }
}

public void Add(string [] newProperty)
{
  try
  {
  //create an arrayList
  //then insert information from the array
  ArrayList propertyList = new ArrayList();
  propertyList.AddRange(newProperty);
  }
  catch(Exception err)
  {
  MessageBox.Show(err.Message);
  }
}
}
Once I instantiate the oDataStore object out of DataStoreCruise it will open the file so i can insert data into it. If the file is not created then it will create and then insert the information from my ArrayList into Cruise.bin. Once I try to run my form it gives me an error message saying "Attempting to deserialize and empty stream".
Any help will be greatly appreciated.
Thanks in advance.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top