title: How do I make a derived class cloneable?
body: <p>I am attempting to create a base class and a derived class that both implement <code>ICloneable</code>. It seems to me that the base class <code>Clone</code> method should take care of all the base class properties and the derived class <code>Clone</code> method all of the derived class properties and use the base class's <code>Clone</code> method. The result is something like this:-</p>
  
  <pre><code>Public Class MyBaseClass
      Implements ICloneable
  
      Private my_base_var As Integer
  
      Public Function Clone() As Object Implements System.ICloneable.Clone
          Dim new_base_class As MyBaseClass = New MyBaseClass
          new_base_class.my_base_var = my_base_var
          Return new_base_class
      End Function
  
  End Class
  
  Public Class MySubClass
      Inherits MyBaseClass
      Implements ICloneable
  
      Private my_sub_var As Integer
  
      Public Overloads Function Clone() As Object Implements System.ICloneable.Clone
          Dim new_sub_class As MySubClass = CType(MyBase.Clone, MySubClass)    ' (1)
          new_sub_class.my_sub_var = Me.my_sub_var
          Return new_sub_class
      End Function
  
  End Class
  </code></pre>
  
  <p>This results in a run-time exception <code>Unable to cast object of type MyBaseClass to type MySubClass</code> at line (1), which I confess did not surprise me. Replacing that line with</p>
  
  <pre><code>    Dim new_sub_class As MySubClass = MyBase.Clone
  </code></pre>
  
  <p>produced a compilation error. It seems to me that I need some way of converting a <code>MyBaseClass</code> to <code>MySubClass</code> in order to get the base class's members set up with the base class's <code>Clone</code>. A less appealing alternative would be to copy the base class's members in the sub-class's <code>Clone</code>. Does anyone know what to do here? </p>
  
  <p>I have looked at the question <a href="https://stackoverflow.com/questions/14103693/cloneable-in-derived-classes">here</a> but can't translate that into VB.Net. If the solution given <a href="https://stackoverflow.com/questions/14072206/vb-net-forced-inheritance-through-multiple-generations">here</a> is necessary, then I'll give up the idea. In the fullness of time I plan to make the base class <code>MustInherit</code>, if that makes any difference.</p>