Solved

cloneing object derived from treenode

Posted on 2004-04-23
5
335 Views
Last Modified: 2007-12-19
Hello,
I am trying to implement ICloneable's Clone() function,  using Serialization.  However, my code causes an exception.

I have a class derived from TreeNod called "Node1".

In Node1, I have implemented Clone() using serialization.

Wen i call the Clone() function, the following exception is thrown at the "Deseialize" call.   ------>

"An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll
Additional information: The constructor to deserialize an object of type WindowsApplication1.Node1 was not found."


Can somebody explain please.  To run the example that is posted below, select a node in the form, and press "button 1".

TIA,
David.


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

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

namespace WindowsApplication1
{
      /// <summary>
      /// Summary description for Form1.
      /// </summary>
      [Serializable]
      public class Form1 : System.Windows.Forms.Form
      {
            private System.Windows.Forms.TreeView treeView1;
            private System.Windows.Forms.Button button1;
            private Node1 mClone;
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.Container components = null;

            public Form1()
            {
                  //
                  // Required for Windows Form Designer support
                  //
                  InitializeComponent();

                  treeView1.Nodes.Add(new Node1("node1.1",1));
                  treeView1.Nodes[0].Nodes.Add(new Node1("node1.2",2));
                  treeView1.Nodes[0].Nodes.Add(new Node1("node1.3",3));            
            }

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if (components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }

            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  this.treeView1 = new System.Windows.Forms.TreeView();
                  this.button1 = new System.Windows.Forms.Button();
                  this.SuspendLayout();
                  //
                  // treeView1
                  //
                  this.treeView1.ImageIndex = -1;
                  this.treeView1.Location = new System.Drawing.Point(32, 24);
                  this.treeView1.Name = "treeView1";
                  this.treeView1.SelectedImageIndex = -1;
                  this.treeView1.Size = new System.Drawing.Size(121, 192);
                  this.treeView1.TabIndex = 0;
                  //
                  // button1
                  //
                  this.button1.Location = new System.Drawing.Point(192, 72);
                  this.button1.Name = "button1";
                  this.button1.TabIndex = 1;
                  this.button1.Text = "button1";
                  this.button1.Click += new System.EventHandler(this.button1_Click);
                  //
                  // Form1
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(292, 273);
                  this.Controls.Add(this.button1);
                  this.Controls.Add(this.treeView1);
                  this.Name = "Form1";
                  this.Text = "Form1";
                  this.ResumeLayout(false);

            }
            #endregion

            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                  Application.Run(new Form1());
            }

            private void button1_Click(object sender, System.EventArgs e)
            {
                  mClone = (Node1)((Node1)treeView1.SelectedNode).Clone();

            }
      }


      [Serializable]
      public class Node1 : TreeNode,ICloneable
      {
        public int mI;
            
            public Node1():base()
            {
            }

            public Node1(String s, int myInt): base(s)
            {
                  Text = s;
                  mI = myInt;
            }

            public override object Clone()
            {
                  MemoryStream ms = new MemoryStream();
                  BinaryFormatter bf = new BinaryFormatter();
                  // Serialize the object into the stream.
                  bf.Serialize(ms, this);
                  //Position streem pointer back to first byte.
                  ms.Seek(0, SeekOrigin.Begin);
                  // Deserialize into another object.
                  Node1 cloneObject = (Node1)bf.Deserialize(ms);
                  // Release memory.
                  ms.Close();

                  return cloneObject;
            }
      };

}


0
Comment
Question by:dave_p_r_b
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 10901014
0
 

Author Comment

by:dave_p_r_b
ID: 10901524
hello R.K.
I applied the changes from the article, but it appears to make no difference....

thanks for your input.
David.
0
 

Author Comment

by:dave_p_r_b
ID: 11151483
answer: its not possible to serialize a treenode.
0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 11757138
PAQed, with points refunded (300)
ee_ai_construct - CS Mod
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question