473,398 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Referenced object versus copy of object

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
Mar 18 '06 #1
4 1417
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

Mar 18 '06 #2
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?...c-61641f5c8d9d

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

I hope this helps,

Cor

"OpticTygre" <op********@adelphia.net> schreef in bericht
news:G5******************************@adelphia.com ...
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

Mar 18 '06 #3
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:
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?...c-61641f5c8d9d

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

I hope this helps,

Cor

"OpticTygre" <op********@adelphia.net> schreef in bericht
news:G5******************************@adelphia.com ...
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


Mar 18 '06 #4
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

"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:up**************@tk2msftngp13.phx.gbl...
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?...c-61641f5c8d9d

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

I hope this helps,

Cor

"OpticTygre" <op********@adelphia.net> schreef in bericht
news:G5******************************@adelphia.com ...
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


Mar 18 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: franky.backeljauw | last post by:
Hello, I am wondering which of these two methods is the fastest: std::copy, which is included in the standard library, or a manually written pointer copy? Do any of you have any experience with...
1
by: Jimi | last post by:
(Sorry for the repost, but I thought maybe I'd have better luck this time) I'm working on a utility to provide an overview of one's .NET projects and one of the things I want to display is...
3
by: Axel | last post by:
HI I want to reference code in an external Access Database, and refresh that database (by downloading from network location). Do I need to bootstrap? Or is it possible (during autorun)...
1
by: Kenny D | last post by:
Sample input XML... <Publication> <FilingMetadata> <Id>38EA51240E6643208DB1B6D52F779A82</Id> <Cycle>BC</Cycle> </FilingMetadata> <PublicationComponent Role="Main" MediaType="Text">...
13
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...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
6
by: Rick | last post by:
Hi, Can anyone explain to me why the below fails to compile - seeing otherA->f(); as a call to a inaccessible function, while otherB->f(); is ok? It seems you can happily access protected...
1
by: cnixuser | last post by:
Hello, I am currently working on an ASP.NET web application. When I compile my code behind the page, I do not get any errors ;however, when I attempt to view my code in a browser I get the error that...
2
by: Veloz | last post by:
Hiya My question is whether or not you should associated related objects in your software using a scheme of id's and lookups, or wether objects should actually hold actual object references to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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,...
0
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...

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.