Click to See Complete Forum and Search --> : Serializing problem (urgent!)


lior6543
January 17th, 2005, 07:34 PM
I'm serializing an array of class objects to a binary file.
When I open the created file, I can see that for each item inside the array, all class's variables are saved except of one which is another ArrayList variable:

My class is:

Class ...{
String* ... //saved fine
String* ... //saved fine
static ArrayList* ArItems = new ArrayList(); // all values here are not saved.
double ... // saved fine

I got a ArrayList variable which I add the class inside
and I'm serializing this ArrayList variable.
What is wrong?

NetMaster
January 18th, 2005, 03:51 AM
Hi,


Can you post the code for Serialize() ? I think your class needs to be a subclass of CObject.

lior6543
January 18th, 2005, 07:05 AM
this is the class file:
=============
using namespace System;
using namespace System::Collections;
using namespace System::Data;

namespace VCNET
{
// This attribute makes the class serializable
[Serializable] public __gc class Items
{
// All fields in this class will be serialized, regardless of scope.
public:
String* Code;
String* Name;
double Price;
double Quantity;
double Total;

// Constructor to load in values for the fields.
Items( String* argCode, String* argName,
double argPrice, double argQuantity, double argTotal )
{
this->Code = argCode;
this->Name = argName;
this->Price = argPrice;
this->Quantity = argQuantity;
this->Total = argPrice*argQuantity;
}
};

// This attribute makes the class serializable
[Serializable] public __gc class Invoices
{
// All fields in this class will be serialized, regardless of scope.
public:
String* Date;
String* CustomerID;
String* CustomerName;
static System::Collections::ArrayList* ArItems = new ArrayList();
double Total;

// Constructor to load in values for the fields.
Invoices()
{
this->Total = 0;
}
Invoices( String* argDate, String* argCustomerID, String* argCustomerName )
{
this->Date = argDate;
this->CustomerID = argCustomerID;
this->CustomerName = argCustomerName;
this->Total = 0;
}
};
}

this is the serializing function:
=====================
public: static void Save_Invoices()
{
// Get a filestream that writes to Customers.dat
FileStream* fs = new FileStream(".\\Data\\Invoices.dat", FileMode::OpenOrCreate);

// Get a Binary Formatter instance
BinaryFormatter* bf = new BinaryFormatter();

bf->Serialize( fs, ArInvoices );

// Close the file and release resources->(avoids GC delays)
fs->Close();
}


also:
====
// public vars:
public: static System::Collections::ArrayList* ArInvoices = new ArrayList();
public: static VCNET::Items* itm = new VCNET::Items();

Add like this: ArInvoices ->ArItems->Add( itm );

darwen
January 20th, 2005, 10:51 AM
.NET Serialization only works on non-static members : not static ones.

To do this you'll have to inherit from the ISerializable interface and override its methods.

Have a look at

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemruntimeserializationiserializableclassgetobjectdatatopic.asp

Either that our serialize the static member seperately.

Darwen.

lior6543
January 21st, 2005, 03:32 PM
thank you very much but i already understood it...
thanks anyway