Referenced object versus copy of object

O

OpticTygre

If I pass an object to another form via the new form's tag property, I want
to create an object exactly like it, with it's properties and all, but have
it be a copy of the object passed through, and not just a reference to the
object.

Example:

Public Sub Form_Load(ByVal sender as Object, ByVal e as EventArgs)

Dim obj as Object = Activator.CreateInstance(Me.Tag.GetType())
obj = Me.Tag

When I say that obj = Me.Tag, then it only provides a reference to the
object in the Tag property. How can I create an exact duplicate instead?

TIA,
-Jason
 
T

tommaso.gastaldi

Hi OpticTygre,

a way is to simply serialize the object in memory (don't need the
"Activator.CreateInstance" stuff).

In addition, if it contains nonserialized members, you have to relink
them (can use those of the object being cloned). (If this is done on
large scale in complex applications you need to get organized, in
order not to miss any relink...)

Let me know if you need more help...

-tom
 
C

Cor Ligthert [MVP]

OpticTygre,

This is difficult to explain because there is no standard.

Most Copy methods are shalow copies. The pointers of its members are copied.
The CopyTo method is a copy method, however is not everywhere.
The Copy from a datatable and a dataset object is a real copy as well as the
GetChanges in that object.

To copy an object completely you can serialize and deserialize it.
Here a sample with an arraylist. (the class has to be serializable)

http://www.vb-tips.com/default.aspx?ID=7ffd296f-9e81-47e6-88dc-61641f5c8d9d

Be aware that although this are few instructions it takes time.

I hope this helps,

Cor
 
T

tommaso.gastaldi

Actually I had in mind a binary serialization: I would like to try both
to see which one is faster (whithout trying,I would assume binary
should be better).

Actually this is a fundamental issue in programming, and as Cor has
sugested, it can take months to fully master all the subtleties.

Assume for instance you have an object which has the property (an
object) "Kind". Assume you have 100 of these objects stored in an
arraylist. Assume that there are only 5 kinds fo "Kind" objects which
are themseles stored in another arraylist, say "Kinds".

When you serialize and deserialize the arraylist with the 100 objects
and the arraylist with the 5 Kinds, you lose the referential equality
of the Kinds. You will have 100 objects each with its "Kind" and each
of these is a different reference from the 5 deserialized "Kinds" that
are in the arraylist od the Kinds. So you will need to relink them...

This is just one example. One really has to get well organized
(creating interfaces and precise rules to jerarchically restore
objects) to deal with that effectively in real world application...

Let me know if you compare the 2 types of serializations...

tom

Cor Ligthert [MVP] ha scritto:
 
O

OpticTygre

Yeah, and actually, what makes it difficult is that I can't guarantee these
objects to be Serializable, let alone all properties, variables, etc...
(What if the object is serializable but a field marked as nonserializable,
or if the object isn't serializable at all?)

-Jason
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top