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. Saving class instance to file??

Saving class instance to file??

Scheduled Pinned Locked Moved C#
question
2 Posts 2 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.
  • H Offline
    H Offline
    HG
    wrote on last edited by
    #1

    Hi, I'm wondering if it is possible to save an instance of a class to a file (as is) and vice versa. I have a class that can contain about 2000 ethernet packages. I would like to save this entire class to a file, but also be able to open it again for later use. Something like: MyClass = (MyClassType)FileStream ??? Any ideas?? G

    J 1 Reply Last reply
    0
    • H HG

      Hi, I'm wondering if it is possible to save an instance of a class to a file (as is) and vice versa. I have a class that can contain about 2000 ethernet packages. I would like to save this entire class to a file, but also be able to open it again for later use. Something like: MyClass = (MyClassType)FileStream ??? Any ideas?? G

      J Offline
      J Offline
      James T Johnson
      wrote on last edited by
      #2

      You need to look at something called serialization. Heres a simple example

      using System;
      using System.Runtime.Serialization.Formatter.Binary;
      using System.IO;

      [Serializable()]
      public class MyClass
      {
      public int MyInt;
      private int privateInt;

      public MyClass(int pub, int pri)
      {
      MyInt = pub;
      privateInt = pri;
      }

      public void PrintOut()
      {
      Console.WriteLine("MyInt = {0}, privateInt = {1}", MyInt.ToString(), privateInt.ToString());
      }
      }

      public class Driver
      {
      public static void Main()
      {
      MyClass myClass = new MyClass(4, 2);

      // Save the instance to a file
      FileStream stream = new FileStream("C:\\\\myclass.bin", FileMode.CreateNew, FileAccess.Write);
      IFormatter formatter = new BinaryFormatter();
      
      // Actually does the saving
      formatter.Serialize(myClass);
      // Close the file
      stream.Close();
      
      // Set MyClass to null so that there are no handles to it anymore
      myClass = null;
      
      
      // Read the instance from the file
      stream = new FileStream("C:\\\\myclass.bin", FileMode.Open, FileAccess.Read);
      
      // Actually create the class instance from the file
      myClass = (MyClass) formatter.Deserialize(stream);
      
      // Close the stream
      stream.Close();
      
      // Show that all data was saved, public AND private
      myClass.PrintOut();
      

      }
      }

      If you want to customize what is saved in the serialization process refer to the documentation on the ISerializable interface HTH, James Simplicity Rules!

      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