Solved

clone() of TreeNode.

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

697 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