Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Cloning an object

Cloning an object

Scheduled Pinned Locked Moved Visual Basic
csharptutorialquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MikeMarq
    wrote on last edited by
    #1

    Does anybody know of a simple way to make a shallow clone of an object in vb.net? If anybody has a code example that would also be usefull. thanks, Mike

    K 1 Reply Last reply
    0
    • M MikeMarq

      Does anybody know of a simple way to make a shallow clone of an object in vb.net? If anybody has a code example that would also be usefull. thanks, Mike

      K Offline
      K Offline
      Keith Malwitz
      wrote on last edited by
      #2

      http://msdn2.microsoft.com/en-us/library/system.object.memberwiseclone.aspx[^]

      M 1 Reply Last reply
      0
      • K Keith Malwitz

        http://msdn2.microsoft.com/en-us/library/system.object.memberwiseclone.aspx[^]

        M Offline
        M Offline
        MikeMarq
        wrote on last edited by
        #3

        Thank you for that link Keith. But I'm getting an error when I try to do the same thing with my object that says this: Object.Protected Function MemberwiseClone() As Object' is not accessible in this context because it is 'Protected' Here is the line in my code based on the msdn example that's causing the error: newobj.PartAtom = CType(oldobj.PartAtom.memberwiseclone,PartAtomCLS) If anybody has any ideas on what is causing an error please let me know. thanks, Mike

        A K 2 Replies Last reply
        0
        • M MikeMarq

          Thank you for that link Keith. But I'm getting an error when I try to do the same thing with my object that says this: Object.Protected Function MemberwiseClone() As Object' is not accessible in this context because it is 'Protected' Here is the line in my code based on the msdn example that's causing the error: newobj.PartAtom = CType(oldobj.PartAtom.memberwiseclone,PartAtomCLS) If anybody has any ideas on what is causing an error please let me know. thanks, Mike

          A Offline
          A Offline
          Andre L A C Bittencourt
          wrote on last edited by
          #4

          Hi MikeMarq, Perhaps its because you are not calling the MemberwiseClone() method within its class or by a derived class. Remember that a protected member is accessible within its class and by a derived classes.

          Andre Luiz de Alcântara Chaves Bittencourt

          M 1 Reply Last reply
          0
          • M MikeMarq

            Thank you for that link Keith. But I'm getting an error when I try to do the same thing with my object that says this: Object.Protected Function MemberwiseClone() As Object' is not accessible in this context because it is 'Protected' Here is the line in my code based on the msdn example that's causing the error: newobj.PartAtom = CType(oldobj.PartAtom.memberwiseclone,PartAtomCLS) If anybody has any ideas on what is causing an error please let me know. thanks, Mike

            K Offline
            K Offline
            Keith Malwitz
            wrote on last edited by
            #5

            The previous poster is dead-on. You will need to write a function in your class that implements the ICloneable.Clone. For example: Public Class MyClass Public Function Clone() as Object Implements ICloneable.Clone Return Me.MemberwiseClone() End Function End Class You can then call the Clone function from outside the class to create a shallow copy of your type.

            1 Reply Last reply
            0
            • A Andre L A C Bittencourt

              Hi MikeMarq, Perhaps its because you are not calling the MemberwiseClone() method within its class or by a derived class. Remember that a protected member is accessible within its class and by a derived classes.

              Andre Luiz de Alcântara Chaves Bittencourt

              M Offline
              M Offline
              MikeMarq
              wrote on last edited by
              #6

              Your suggestions fixed it. Thank you guys for your help. :)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups