473,508 Members | 3,343 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need shallow copy of a class

6 New Member
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
3 1203
Frinavale
9,735 Recognized Expert Moderator Expert
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
297 Contributor
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
297 Contributor
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

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

Similar topics

5
3263
by: Tony Johansson | last post by:
Hello! I'm reading in a book about C++ and that is something that sound strange. It says "Pointers have reference-assignment semantics similar to those in Java. For example, after the...
2
12340
by: Alex | last post by:
Entering the following in the Python shell yields >>> help(dict.copy) Help on method_descriptor: copy(...) D.copy() -> a shallow copy of D >>>
8
1927
by: coredumperror | last post by:
Hi all you C++ gurus out there, I've got what may be an unusual question. What's the best way to get a shallow copy of a class that you've programmed to prevent accidental shallow copying? I've...
26
15765
by: saxenavaibhav17 | last post by:
what is Deep Copy, Shallow copy and Bitwise copy, Memberwise copy? and what is the difference between them? pls help vaibhav
13
4986
by: blangela | last post by:
I have decided (see earlier post) to paste my Word doc here so that it will be simpler for people to provide feedback (by directly inserting their comments in the post). I will post it in 3 parts...
9
1655
by: blangela | last post by:
2.0 Sample Code class ABC // dummy class used below {}; class Example2 { public: Example2(); // default ctor Example2( const Example2 &); // copy ctor
1
2106
by: blangela | last post by:
3.0 Advanced Topic Addendum There are a few cases where the C++ compiler cannot provide an overloaded assignment operator for your class. If your class contains a const member or/and a...
4
3132
by: shuisheng | last post by:
Dear All, Is there any easy way to make sure all my object copies are deep copy or shallow copy? I do not like to implement it in each class one by one. Thanks, Shuisheng
2
3896
by: Stefan Hoffmann | last post by:
hi, as i'm farely new to C#, i have some problem of creating a shallow copy of a class inherited of CollectionBase. As far as i see, i just need to copy the CollectionBase.List using the...
5
2086
by: raylopez99 | last post by:
In C++, you have symbolic overriding of "+", "=", etc, which C# also has. This question is not really about that. Rather, in C#, when you say: MyObject X = new MyObject(); MyObject Y = new...
0
7229
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7333
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5637
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5057
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3208
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3194
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.