Save inside a file ArrayList code
-
I've create an object: class myClass{ int value; string name; } now i've created an arraylist of it ArrayList[] myArray = new ArrayList[4]; now i create multiarray of it: for(int i=0; i<4; i++){ myArray[i] = new ArrayList[7]; } now i assign value in this way ... myClass myClassRef = new myClass(); myClassRef.name = "James"; myArray[2][4] = myClassRef; ... ok now i want to save all myArray inside a text file and then read back again a new myNewArray. How? Can you show me a bit of code? I choose arraylist becouse it is not definited the dimension. I don't want to use XML becouse C# is a full self sufficient language! Tnx to all.
-
I've create an object: class myClass{ int value; string name; } now i've created an arraylist of it ArrayList[] myArray = new ArrayList[4]; now i create multiarray of it: for(int i=0; i<4; i++){ myArray[i] = new ArrayList[7]; } now i assign value in this way ... myClass myClassRef = new myClass(); myClassRef.name = "James"; myArray[2][4] = myClassRef; ... ok now i want to save all myArray inside a text file and then read back again a new myNewArray. How? Can you show me a bit of code? I choose arraylist becouse it is not definited the dimension. I don't want to use XML becouse C# is a full self sufficient language! Tnx to all.
Add a custom serializer and deserializer to the class, then. Something like:
class myClass{ public int value; public string name; public MyClass() {} public MyClass(string serializedData) { int pos = serializedData.FirstIndexOf(' '); this.value = int.Parse(serializedData.Substring(0, pos)); this.name = serializedData.SubString(pos + 1); } public string Serialize() { return this.value.ToString() + " " + this.name; } }
Then just loop through the arrays and write the serialized string of each object to the file (using WriteLine). When you wish to recreate the arrays, read each line and use the deserializer constructor to recreate each object. --- b { font-weight: normal; } -
Add a custom serializer and deserializer to the class, then. Something like:
class myClass{ public int value; public string name; public MyClass() {} public MyClass(string serializedData) { int pos = serializedData.FirstIndexOf(' '); this.value = int.Parse(serializedData.Substring(0, pos)); this.name = serializedData.SubString(pos + 1); } public string Serialize() { return this.value.ToString() + " " + this.name; } }
Then just loop through the arrays and write the serialized string of each object to the file (using WriteLine). When you wish to recreate the arrays, read each line and use the deserializer constructor to recreate each object. --- b { font-weight: normal; } -
I've create an object: class myClass{ int value; string name; } now i've created an arraylist of it ArrayList[] myArray = new ArrayList[4]; now i create multiarray of it: for(int i=0; i<4; i++){ myArray[i] = new ArrayList[7]; } now i assign value in this way ... myClass myClassRef = new myClass(); myClassRef.name = "James"; myArray[2][4] = myClassRef; ... ok now i want to save all myArray inside a text file and then read back again a new myNewArray. How? Can you show me a bit of code? I choose arraylist becouse it is not definited the dimension. I don't want to use XML becouse C# is a full self sufficient language! Tnx to all.
Sasuko wrote: I don't want to use XML becouse C# is a full self sufficient language! Huh? So you won't use SQL because C# is a "full sufficient language!"? XML is designed for storing data, C# is not, so what's the point in staying away from XML? You can use Binary serialization if you want to, just serialize the arraylist using BinaryFormatter and deserialize it back. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
Sasuko wrote: I don't want to use XML becouse C# is a full self sufficient language! Huh? So you won't use SQL because C# is a "full sufficient language!"? XML is designed for storing data, C# is not, so what's the point in staying away from XML? You can use Binary serialization if you want to, just serialize the arraylist using BinaryFormatter and deserialize it back. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
You can use Binary serialization if you want to, just serialize the arraylist using BinaryFormatter and deserialize it back. Yes that is the way, but i need example of code.
Check out these two functions for serializing and deserializing data using a BinaryFormatter. It serializes a hashtable but can be replaced by an arraylist and should work just fine.
static void Serialize() { // Create a hashtable of values that will eventually be serialized. Hashtable addresses = new Hashtable(); addresses.Add("Jeff", "123 Main Street, Redmond, WA 98052"); addresses.Add("Fred", "987 Pine Road, Phila., PA 19116"); addresses.Add("Mary", "PO Box 112233, Palo Alto, CA 94301"); // To serialize the hashtable and its key/value pairs, // you must first open a stream for writing. // In this case, use a file stream. FileStream fs = new FileStream("DataFile.dat", FileMode.Create); // Construct a BinaryFormatter and use it to serialize the data to the stream. BinaryFormatter formatter = new BinaryFormatter(); try { formatter.Serialize(fs, addresses); } catch (SerializationException e) { Console.WriteLine("Failed to serialize. Reason: " + e.Message); throw; } finally { fs.Close(); } } static void Deserialize() { // Declare the hashtable reference. Hashtable addresses = null; // Open the file containing the data that you want to deserialize. FileStream fs = new FileStream("DataFile.dat", FileMode.Open); try { BinaryFormatter formatter = new BinaryFormatter(); // Deserialize the hashtable from the file and // assign the reference to the local variable. addresses = (Hashtable) formatter.Deserialize(fs); } catch (SerializationException e) { Console.WriteLine("Failed to deserialize. Reason: " + e.Message); throw; } finally { fs.Close(); } // To prove that the table deserialized correctly, // display the key/value pairs. foreach (DictionaryEntry de in addresses) { Console.WriteLine("{0} lives at {1}.", de.Key, de.Value); } }
Elvis (a.k.a Azerax) Life is Music listen to it before it fades