locked
Serialization problem RRS feed

  • Question

  • //Sample code:
    
    [Serializable ]
      public partial class Form1 : Form
      {
         Form1 frm1 = new Form1();
        Form2 frm2 = new Form2();
        Form3 frm3 = new Form3();
    
     private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
          IFormatter formatter = new BinaryFormatter();
          Stream stream = new FileStream("c:\\MyFile.bin", FileMode.Create, FileAccess.Write, FileShare.None);
          formatter.Serialize(stream, frm1);
          formatter.Serialize(stream, frm2);
          formatter.Serialize(stream, frm3);
          stream.Close();
        }
    }
    
    
    runtime error:
    
     An unhandled exception of type 'System.StackOverflowException' occurred in myproject.exe
    Tuesday, June 8, 2010 9:55 AM

Answers

  • When you instantiate a Form1 object, its frm1 field is initialized with a new Form1 object. In that Form1 object, the frm1 field is initialized with a new Form1 object........
    Tuesday, June 8, 2010 10:00 AM
  • Deleted
    Tuesday, June 8, 2010 10:06 AM
  • Hi S.Guru,

       Welcome to MSDN forums! I'm glad to see your active participation and discussion in MSDN forums. 

        Did your form implement interface ISerializable? If you do it, I think you need seriailze some propetry not form object is it? please try

         public class Form:Form1,ISerializable { ........ //then you can write what field you want serialize }

       ----------------------------------------------------------------- ---------------------------------------------------------------------------after you can serialize some form's property into a file

        formatter.Serialize(stream, frm1.XXX);

        formatter.Serialize(stream, frm2.XXX);

        formatter.Serialize(stream, frm3.XXX);

       Please refer to  http://www.switchonthecode.com/tutorials/csharp-tutorial-serialize-objects-to-a-file .   Hope this will help you! 

       If you still have any doubt and concern about this issue, please let me know. If I misunderstood you, please kindly elaborate your question.


    Please Mark as Answered If this is helpful Or Un-Mark as Answered if it is not helpful.

    Best Regards,
    Yan Jun
    Microsoft Online Community Support

    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Wednesday, June 9, 2010 3:23 AM
    Moderator

All replies

  • When you instantiate a Form1 object, its frm1 field is initialized with a new Form1 object. In that Form1 object, the frm1 field is initialized with a new Form1 object........
    Tuesday, June 8, 2010 10:00 AM
  • Deleted
    Tuesday, June 8, 2010 10:06 AM
  • Hi S.Guru,

       Welcome to MSDN forums! I'm glad to see your active participation and discussion in MSDN forums. 

        Did your form implement interface ISerializable? If you do it, I think you need seriailze some propetry not form object is it? please try

         public class Form:Form1,ISerializable { ........ //then you can write what field you want serialize }

       ----------------------------------------------------------------- ---------------------------------------------------------------------------after you can serialize some form's property into a file

        formatter.Serialize(stream, frm1.XXX);

        formatter.Serialize(stream, frm2.XXX);

        formatter.Serialize(stream, frm3.XXX);

       Please refer to  http://www.switchonthecode.com/tutorials/csharp-tutorial-serialize-objects-to-a-file .   Hope this will help you! 

       If you still have any doubt and concern about this issue, please let me know. If I misunderstood you, please kindly elaborate your question.


    Please Mark as Answered If this is helpful Or Un-Mark as Answered if it is not helpful.

    Best Regards,
    Yan Jun
    Microsoft Online Community Support

    This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Wednesday, June 9, 2010 3:23 AM
    Moderator