Solved

clone() of TreeNode.

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now