How to copy an object?
How to copy an object?
How to copy an object?
How to copy an object?
How to copy an object?
How to copy an object? How to copy an object? How to copy an object? How to copy an object? How to copy an object? How to copy an object? How to copy an object? How to copy an object?
How to copy an object? How to copy an object?
How to copy an object?
Go Back  Xtreme Visual Basic Talk > > > How to copy an object?


Reply
 
Thread Tools Display Modes
  #1  
Old 07-10-2004, 10:40 AM
PhoenixX PhoenixX is offline
Newcomer
 
Join Date: Jun 2004
Location: Stukenbrock, Germany
Posts: 23
Unhappy How to copy an object?


Hi all

How can I copy an object? When I use obj1 = oby2 I copy only the pointer. I know some objects have the methode .clone. But What's about the other ones? Some objects (for example a button) I can copy of course by creating a new object and set each property. (btn1.Left = btn2.Left) But that works rarly. And of course what's about my own classes? Is there now change to copy them like you can do it for example in C++...?

If someone could help me, thanks a lot! It's a big problem for me...
Reply With Quote
  #2  
Old 07-10-2004, 01:03 PM
MKoslof's Avatar
MKoslofHow to copy an object? MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

To create a "shallow copy" of your object (i.e. just value or member fields, but references to other objects will be the same), use the MemberwiseClone function.

Code:
myNewObject = DirectCast(myObject.MemberwiseClone, <myType>)

If you want a deep copy your object, you will need to have your class implement the IClonable interface.
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #3  
Old 07-10-2004, 01:24 PM
DeadalusHow to copy an object? Deadalus is offline
Promising Talent

Retired Moderator
* Guru *
 
Join Date: May 2002
Location: Brussels
Posts: 3,601
Default

I hope PhoenixX doesn't mind that I pick in on this, I find it an interesting question.

MKoslof, would the MemberwiseClone approach be useful to copy controls on a form? It's Protected, so you can only use it inside the class and in derived classes, right?

But going with the IClonable approach would mean that you have to write the cloning code yourself, whether it is for a deep or shallow copy. In Java this is solved by not putting the Clone method in the Clonable interface, but in the Object class instead. That way Clone can get a default implementation (which does a shallow copy). MemberwiseClone seems to be meant to make up for the lack of a default Clone implemention in .NET, but then why was it made Protected?
Reply With Quote
  #4  
Old 07-10-2004, 03:01 PM
PhoenixX PhoenixX is offline
Newcomer
 
Join Date: Jun 2004
Location: Stukenbrock, Germany
Posts: 23
Default

First thanks MKoslof. You helped me alot. But what's about the controls on a form. Will that code work? Can you clone MyButton now?

Code:
Public Class MyButton Inherits System.Forms.Button Implements ICloneable Public Function Clone() As Object Implements System.ICloneable.Clone Return MyBase.MemberwiseClone() End Function End Class

Last edited by PhoenixX; 07-10-2004 at 03:09 PM.
Reply With Quote
  #5  
Old 07-10-2004, 04:27 PM
PhoenixX PhoenixX is offline
Newcomer
 
Join Date: Jun 2004
Location: Stukenbrock, Germany
Posts: 23
Default

OK I see:
The code doesn't work well... You said that MemberwiseClone only creates a shallow copy of my object. I think for example the position of the button is a System.Drawing.Point. An object and MemberwiseClone copies only the pointer of that object so that when I change the Position of the 1st Button the Position of the 2nd one also changes. I think to clone a button correctly I have to see the code of it(With all private stuff) And then it would be too complicated to pogram everything on his own. I thing I'd have to create the whole button again if I wanted to copy it...

Last edited by PhoenixX; 07-10-2004 at 05:11 PM.
Reply With Quote
  #6  
Old 07-10-2004, 06:04 PM
DeadalusHow to copy an object? Deadalus is offline
Promising Talent

Retired Moderator
* Guru *
 
Join Date: May 2002
Location: Brussels
Posts: 3,601
Default

One method that is apparently used for deep copies of complex objects is serializing the object into a bytestream and then rebuilding the object.
http://www.dotnetextreme.com/article...ypePattern.asp

Last edited by Deadalus; 07-10-2004 at 06:15 PM. Reason: Fixed link.
Reply With Quote
  #7  
Old 07-10-2004, 06:45 PM
MKoslof's Avatar
MKoslofHow to copy an object? MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

Yes, I will admit this is an interesting topic . And I must admit, in terms of actual, working examples, most of the copying I have done is via "shallow" object casting and direct cast. Because, copying a full object isn't as cut and try as you would hope in .Net, though it is possible.

As an example, say I have a large .Net application. Frequently I have to gain access to a datasource or a value member of an object. So, if I need to call a DataGrid's current datasource or gain access to the values within its current reference, I can use a directcast. Something like:

Code:
For i = 0 To DirectCast(f.DataGrid1.DataSource, DataTable).Rows.Count() - 1 'get the data members of the datagrid and do something Next

So, I am getting a direct "copy" of the value members within the DataGrid. This is quite useful. However, If I write a function or class that will physically copy the object, this method won't work .

I have come to realize there is no elegant way to do this. What you could do is, mark the class as serializable, and use serialization. This should make a "deep" copy of the object (also copying any other objects referenced by the copied object).

The problem is, every class is special, and the language runtime can't make assumptions of what data needs to be copied. So the class designer must decide what it takes to clone an instance. For this purpose, the framework provides the ICloneable interface, which has the clone method that the client can call. VB.NET allows you access private members that live in a different instance of the same class, this is something I really want to play more with.

So, unless your object inherits from a class that implements the IClonable interface, you're going to have to manually copy each property over one by one into a new instance of your object. I haven't really dug deep into this...but I am definately curious .
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown

Last edited by MKoslof; 07-10-2004 at 07:31 PM.
Reply With Quote
  #8  
Old 07-11-2004, 05:48 AM
PhoenixX PhoenixX is offline
Newcomer
 
Join Date: Jun 2004
Location: Stukenbrock, Germany
Posts: 23
Default

Yes, thank you. But @Deadalus: Isn't the code more effective when you implement the ICloneable interface in CEmp and write the clone code like that?

In CEmpData.Clone...

Code:
Public Function Clone() As Object Implements ICloneable.Clone Dim objEmp as CEmp Dim objEmpData as New CEmpData For each objEmp in ArrEmp objEmpData.ArrEmp.Add (objEmp.Clone) Next Return objEmpData End Function

...and in CEmp.Clone

Code:
Public Function Clone() As Object Implements ICloneable.Clone Dim objEmp as New CEmp objEmp.mstrFName = mstrFName objEmp.mstrLName = mstrLName Return objEmp End Function
Reply With Quote
  #9  
Old 07-11-2004, 07:35 AM
DeadalusHow to copy an object? Deadalus is offline
Promising Talent

Retired Moderator
* Guru *
 
Join Date: May 2002
Location: Brussels
Posts: 3,601
Default

Yes, that's the "hand-coded" way, which will indeed score much better in performance. But wasn't your question how to make a deep copy when that method becomes too cumbersome?
Reply With Quote
  #10  
Old 07-11-2004, 08:15 AM
PhoenixX PhoenixX is offline
Newcomer
 
Join Date: Jun 2004
Location: Stukenbrock, Germany
Posts: 23
Default

My question now is another one that the one before. I meant why to use serialiazing in that case? There it was as complicated as the method with the ICloneable interface and not as fast.
And: For the button serializing doesn't work, too, I think... Or is there another way to copy the button...?

Last edited by PhoenixX; 07-11-2004 at 08:43 AM.
Reply With Quote
  #11  
Old 07-11-2004, 09:22 AM
MKoslof's Avatar
MKoslofHow to copy an object? MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

In that particular case, you wouldn't neccessarily benefit from using serialization, but again, this really becomes a class by class basis.

Can't the button class implement ICloneable? I haven't tried, but I don't think there are any classes that can't implement its Clone method.
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown

Last edited by MKoslof; 07-11-2004 at 09:51 AM.
Reply With Quote
  #12  
Old 07-11-2004, 01:49 PM
PhoenixX PhoenixX is offline
Newcomer
 
Join Date: Jun 2004
Location: Stukenbrock, Germany
Posts: 23
Default

Yes of course the button class can implement that, but the problem is there isn't a way that I know to create a deep copy in that case. Serializing doesn't work because most of the objects which are used by the button aren't marked as "<Serializable>". The "hand-coded" way doesn't work because you'd have to change the classes that are used by the button. For example the ".location" property of the button uses an object, I think. And it's not marked as "<Serializable>", is it? And the other methode needs to change the code of "Sytem.Drawing.Point".
Reply With Quote
  #13  
Old 07-12-2004, 06:35 AM
MKoslof's Avatar
MKoslofHow to copy an object? MKoslof is offline
Cum Grano Salis

Retired Moderator
* Guru *
 
Join Date: Jul 2002
Location: Baltimore, Maryland
Posts: 14,636
Default

PhoenixX:

It might be that the button class is not cloneable (at least in the way you need it). I understand what you are saying and I am not sure of a way around this. The location property is object. Why do you need a deep copy of this button object? Can you pull this project off without doing this? I would have to sit down and really play with this to give you a solid answer
__________________
"Artificial Intelligence is no match for natural stupidity." ~unknown
Reply With Quote
  #14  
Old 05-28-2009, 07:56 AM
grimblegrumble grimblegrumble is offline
Newcomer
 
Join Date: May 2009
Posts: 1
Default

Quote:
Originally Posted by PhoenixX View Post
Hi all

How can I copy an object? When I use obj1 = oby2 I copy only the pointer. I know some objects have the methode .clone. But What's about the other ones? Some objects (for example a button) I can copy of course by creating a new object and set each property. (btn1.Left = btn2.Left) But that works rarly. And of course what's about my own classes? Is there now change to copy them like you can do it for example in C++...?

If someone could help me, thanks a lot! It's a big problem for me...
I was doing this in VBA, but it should work in VB.NET...

Function bazbop(ByRef cboArr() As ComboBox, ByRef cboObj As ComboBox, ByVal inx As Integer) As Boolean

On Error Resume Next

If inx < LBound(cboArr) Or inx > UBound(cboArr) Then
MsgBox "Index out of bounds!", vbExclamation, "bazbop"
bazbop = False
Exit Function
End If

Set cboArr(inx) = New ComboBox

Set cboArr(inx) = cboObj

bazbop = True

End Function

Note: the reason "On Error Resume Next" is in there is because if the combobox in my case is blank it is considered to be nullified and VB whines at you. So I just made it do it anyway. For you, in a more generalized way I guess it would look something like this:

Function bazbop(ByRef objFoo As Object, ByRef objBar As Object) As Boolean

On Error Resume Next

Set objFoo = New Object

Set objFoo = objBar

bazbop = True

End Function
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
How to copy an object?
How to copy an object?
How to copy an object? How to copy an object?
How to copy an object?
How to copy an object?
How to copy an object? How to copy an object? How to copy an object? How to copy an object? How to copy an object? How to copy an object? How to copy an object?
How to copy an object?
How to copy an object?
 
How to copy an object?
How to copy an object?
 
-->