Cloning an object
-
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
-
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
-
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
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
-
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
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.
-
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