Solved

clone() of TreeNode.

Posted on 2004-04-13
2
1,084 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

808 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