Solved

clone() of TreeNode.

Posted on 2004-04-13
2
1,079 Views
Last Modified: 2008-02-01
A chunk of code.... and two questions follow.


      [Serializable]
      public class myNode : TreeNode
      {
                                 int i;
            
            public myNode():base()
            {
            }

            public myNode(String name): base(name)

            {
                  i = 99;
            }

            public new 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.
                myNode cloneObject = (myNode)bf.Deserialize(ms);
                // Release memory.
                ms.Close();
            
                return cloneObject;
            }
      };

      [Serializable]
      public class myNode2 : myNode
      {
            public myNode2()
            {
            }
            
            public myNode2(String name): base(name)
            {      
            }
      
      };




If I change "new" to "override" in the myNode.Clone() function, then at deserialization, I get the exception :

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

Is there a reason I shouldn't use "new" here?


Also,


Why do I have to have a parameterless constructor in each of my classes? removing one  throws :

An unhandled exception of type 'System.MissingMethodException' occurred in mscorlib.dll
Additional information: No parameterless constructor defined for this object.


Thanks.
Dave.
0
Comment
Question by:dave_p_r_b
2 Comments
 
LVL 12

Accepted Solution

by:
esteban_felipe earned 125 total points
ID: 10813868
Hi dave_p_r_b,

The new keyword is for hiding the inherited member. If this is what you want to do, then don't provide an implementation:

 public new object Clone(){}

Any form of serialization require a parameterless constructor. You should design your classes accordly... Sorry, but there's no escape here...


Esteban Felipe
www.estebanf.com
0
 

Author Comment

by:dave_p_r_b
ID: 10950173
hi,
i wonder if anyone seeing this could attempt

http://oldlook.experts-exchange.com:8080/Programming/Programming_Languages/C_Sharp/Q_20964811.html

I am not getting any replies :S
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

770 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