title: VB.NET copying Class instance to temp in Class Instance
body: <p>I am studying VB.NET and I have a question about copying Class instance.</p>
  
  <p>For example, I have </p>
  
  <pre><code>Public Class frmMain_CLT
  
    Public Shared GEN_SETTING As MY_OBJ
  
    Public Sub frmMain_CLT_Load(sender As Object, e As EventArgs) Handles MyBase.Load          
      Dim Test_GM As MY_OBJ ' I would like to copy GEN_SETTING to Test_GM
    End Sub
  
  End Class
  </code></pre>
  
  <p>I would like to copy GEN_SETTING to Test_GM</p>
  
  <p>I know that I can use  "Test_GM = GEN_SETTING". However, Test_GM will reference GEN_SETTING. Therefore, Data, where is in the GEN_SETTING will change when I modify  Data in the Test_GM.</p>
  
  <p>I would like to make Copy of GEN_SETTING to Test_GM , which is "NOT" referencing original Copy.</p>
  
  <p>But i am not sure how to do it. I was thinking about DirectCast to copy instance but I am getting Error..</p>
  
  <p><strong>Update</strong></p>
  
  <p>I just created Clone method in the MY_OBJ class</p>
  
  <p>for example, </p>
  
  <pre><code> Public Function ShallowCopy() As MY_OBJ
         Return DirectCast(Me.MemberwiseClone(), MY_OBJ)
      End Function 
  </code></pre>
  
  <p>However, I am not really sure if it is the best way to copy class instance </p>