By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,344 Members | 1,139 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,344 IT Pros & Developers. It's quick & easy.

Need shallow copy of a class

P: 6
Hello everybody

I have defined a class and I have declared 'a' and 'b' to be new instances of this class.
I want to shallow copy (NOT DEEP COPY) 'a' into 'b'.
As I know, in shallow copy just the values (not the references) are copied so you can change 'a' without affecting 'b'

what is the related command?
THNX A LOT
Jun 13 '13 #1
Share this Question
Share on Google+
3 Replies


Frinavale
Expert Mod 5K+
P: 9,731
You could just implement a Copy(ByVal myClassInstance As MyClass) method. You could even make this a Static err...Shared method.

For example:
Expand|Select|Wrap|Line Numbers
  1. Public Class MyClass
  2.   Public Property A As String
  3.   Public Property B As Integer
  4.  
  5.   Public Sub New()
  6.   End Sub
  7.  
  8.   Public Sub New(ByVal a As String, ByVal b As Integer)
  9.     Me.A = a
  10.     Me.B = b
  11.   End Sub
  12.  
  13.   Public Sub New(ByVal otherClass As MyClass)
  14.     Me.A = otherClass.A
  15.     Me.B = otherClass.B
  16.   End Sub
  17.  
  18.   Public Shared Function CopyOption1(ByVal instanceToCopy As MyClass) As MyClass
  19.     Dim copiedInstance As MyClass
  20.     copiedInstance.A = instanceToCopy.A
  21.     copiedInstance.B = instanceToCopy.B
  22.   End Sub
  23.  
  24.   Public Shared Function CopyOption2(ByVal instanceToCopy As MyClass) As MyClass
  25.     return new MyClass(nstanceToCopy)
  26.   End Sub
  27.  
Jun 13 '13 #2

!NoItAll
100+
P: 296
Add implements ICloneable interface inside your class.

It's very simple to do and even creates a stub for you to complete.

Inside then just copy the properties you want cloned.

I'm on an iPad now but will try to post a simple example later.
Jun 15 '13 #3

!NoItAll
100+
P: 296
So here is a pseudo sample on implementing iCloneable

Expand|Select|Wrap|Line Numbers
  1. Public Class MyClass
  2.    Implements ICloneable
  3.  
  4.    Public Property MyPropertyA as Integer = 0
  5.    Public Property MyPropertyB as String = String.empty
  6.  
  7.    Public Function Clone() As Object Implements System.ICloneable.Clone
  8.             Return New CameraPanels_type With
  9.            {
  10.             .MyPropertyA = MyPropertyA,
  11.             .MyPropertyB = MyPropertyB
  12.            }
  13.     End Function
  14.  
  15. End Class
  16.  
Using this is then as simple as
Expand|Select|Wrap|Line Numbers
  1. Dim MyObject as new MyClass
  2.     MyObject.MyPropertyA = 232
  3.     MyObject.MyPropertyB = "Four score and seven years ago..."
  4.  
  5. 'Now to copy this just
  6.  
  7. Dim MyNewObject as new MyClass = MyObject.Clone
  8.  
  9.  
Jun 16 '13 #4

Post your reply

Sign in to post your reply or Sign up for a free account.