Click here to Skip to main content
16,015,177 members
Home / Discussions / C#
   

C#

 
GeneralRe: OutlookBar Pin
BLaZiNiX17-Apr-02 18:46
BLaZiNiX17-Apr-02 18:46 
GeneralRe: OutlookBar Pin
James T. Johnson17-Apr-02 19:03
James T. Johnson17-Apr-02 19:03 
GeneralRe: OutlookBar Pin
Ben Kloosterman17-Apr-02 18:48
Ben Kloosterman17-Apr-02 18:48 
GeneralDataSet: no MoveNext or any indexing at all Pin
17-Apr-02 10:38
suss17-Apr-02 10:38 
GeneralRe: DataSet: no MoveNext or any indexing at all Pin
James T. Johnson17-Apr-02 11:51
James T. Johnson17-Apr-02 11:51 
GeneralRe: DataSet: no MoveNext or any indexing at all Pin
18-Apr-02 4:34
suss18-Apr-02 4:34 
QuestionSaving class instance to file?? Pin
HG17-Apr-02 9:41
HG17-Apr-02 9:41 
AnswerRe: Saving class instance to file?? Pin
James T. Johnson17-Apr-02 11:43
James T. Johnson17-Apr-02 11:43 
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!
GeneralLogicalDrives Pin
Mazdak17-Apr-02 9:32
Mazdak17-Apr-02 9:32 
GeneralRe: LogicalDrives Pin
Nick Parker17-Apr-02 9:57
protectorNick Parker17-Apr-02 9:57 
GeneralRe: LogicalDrives Pin
Mazdak18-Apr-02 5:21
Mazdak18-Apr-02 5:21 
GeneralFileAttribute Pin
Mazdak17-Apr-02 8:05
Mazdak17-Apr-02 8:05 
GeneralRe: FileAttribute Pin
James T. Johnson17-Apr-02 12:43
James T. Johnson17-Apr-02 12:43 
GeneralRe: FileAttribute Pin
Mazdak18-Apr-02 5:22
Mazdak18-Apr-02 5:22 
QuestionF1 Help? Pin
Neo Anderson17-Apr-02 6:28
Neo Anderson17-Apr-02 6:28 
AnswerRe: F1 Help? Pin
James T. Johnson17-Apr-02 15:00
James T. Johnson17-Apr-02 15:00 
GeneralAVI Pin
17-Apr-02 5:29
suss17-Apr-02 5:29 
GeneralRe: AVI Pin
James T. Johnson17-Apr-02 16:38
James T. Johnson17-Apr-02 16:38 
GeneralRe: AVI Pin
Mike Nordell17-Apr-02 17:35
Mike Nordell17-Apr-02 17:35 
GeneralXor() Pin
Mazdak17-Apr-02 5:13
Mazdak17-Apr-02 5:13 
GeneralRe: Xor() Pin
James T. Johnson17-Apr-02 5:54
James T. Johnson17-Apr-02 5:54 
GeneralRe: Xor() Pin
Mazdak17-Apr-02 7:04
Mazdak17-Apr-02 7:04 
GeneralI don't have topic for this Pin
Mazdak17-Apr-02 3:44
Mazdak17-Apr-02 3:44 
GeneralRe: I don't have topic for this Pin
Rüpel18-Apr-02 2:15
Rüpel18-Apr-02 2:15 
GeneralRe: I don't have topic for this Pin
Mazdak18-Apr-02 5:23
Mazdak18-Apr-02 5:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.