Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Save inside a file ArrayList code

Save inside a file ArrayList code

Scheduled Pinned Locked Moved C#
csharpxmlquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sasuko
    wrote on last edited by
    #1

    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.

    G S 2 Replies Last reply
    0
    • S Sasuko

      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.

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      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; }

      S 1 Reply Last reply
      0
      • G Guffa

        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; }

        S Offline
        S Offline
        Sasuko
        wrote on last edited by
        #3

        tnx for your answer but it is not the way i choose... :(

        G 1 Reply Last reply
        0
        • S Sasuko

          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.

          S Offline
          S Offline
          S Senthil Kumar
          wrote on last edited by
          #4

          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

          S 1 Reply Last reply
          0
          • S Sasuko

            tnx for your answer but it is not the way i choose... :(

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            What way did you choose, then? --- b { font-weight: normal; }

            1 Reply Last reply
            0
            • S S Senthil Kumar

              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

              S Offline
              S Offline
              Sasuko
              wrote on last edited by
              #6

              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.

              A 1 Reply Last reply
              0
              • S Sasuko

                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.

                A Offline
                A Offline
                Azerax
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups