Solved

clone() of TreeNode.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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