473,407 Members | 2,359 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,407 software developers and data experts.

Generic object Clone function?

I'm looking for a way to generate a "clone" of an object. Right now I need
to write a Clone function for every class that make and I'd like to have a
generic routine.

Instead of doing this:
For i = 0 to Me.Count-1
Set obj = New cSkill
obj.ID = Me.Item(i).ID
obj.Desc = Me.Item(i).Desc
obj.... etc.
Next

I'd like to do something like:
For i = 0 to Me.Count-1
Set obj = New cSkill
For j = 0 to number of properties
obj.property(j) = Me.Item(i).property(j)
Next
Next

Two problems... Is there a "property" property on objects? In my loop I
could be copying a simple variable or another object, so how do I know when
to say A=B or Set A=B.Clone ???
Here is my current clone function. This one is an array containing Skill
objects:

Public Function Clone()

Dim i 'General Counter
Dim objColl 'A new instance of our collection
Dim obj 'A single skill object used to copy

Set objColl = New cSkills

If Me.Count>0 then
Set Clone = New cSkills

For i=lBound(vCollection) To uBound(vCollection)
'Create a new Skill object
Set obj = New cSkill
obj.ID = Me.Item(i).ID
obj.Desc = Me.Item(i).Desc
Clone.Add obj
Next
End If

'Destroy temporary objects
Set obj = Nothing

End Function
Feb 6 '06 #1
8 3801
Noozer wrote:
I'm looking for a way to generate a "clone" of an object. Right now I
need to write a Clone function for every class that make and I'd like
to have a generic routine. Yeah, that would be nice ...
Two problems... Is there a "property" property on objects?
You mean "properties", as in Properties collection, right?
Only if you create one.
In my loop
I could be copying a simple variable or another object, so how do I
know when to say A=B or Set A=B.Clone ???

I guess you need to know what you are dealing with. The TypeName function
might come in handy for this ...

Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Feb 6 '06 #2
There isn't a collection of properties on a VB Class so you will have to
create a clone method for each class.

At the very least I would suggest that you move the cloning of an objects
properties out of the collection class and into the class itself.

E.g.,

Your Skill class should have a clone method that returns another instance of
a Skill object with the same properties. Then the code to clone the
collectiion class is pretty much boilerplate:-

Set Clone = New cSkills '<-- this is the only word that changes.

For i = 0 to Me.Count -1
Set obj = Me.Item(i).Clone
Clone.add obj
Next

This means that if you add a property to a class you don't need to edit a
different class to ensure correct cloning.
Having said that I wonder where all the data comes from in the classes and
where it goes to.

Can I suggest that you consider XML as a mechanism for storing state. Not
only is cloning a lot easier but ultimately in ASP you want to generate
output as HTML and XSL would likely be useful in this regard.

Also many DB engines support XML fairly well making updating and retrieving
data (even of heiarchical nature) reasonably easy (at least compared loading
object heiarchies of VB Classes).
Anthony.
Feb 6 '06 #3
Can't you just do

Set copyOfObject = theObject?
Option Explicit
Dim X, copyOfX
Set X = New test
X.SomeProperty = "property value"

Set copyOfX = X
Response.Write copyOfX.SomeProperty
Class test
Private sSomeProperty

Private Sub Class_Initialize()
sSomeProperty = "undefined"
End Sub

Public Property Get SomeProperty()
SomeProperty = sSomeProperty
End Property

Public Property Let SomeProperty(s)
sSomeProperty = s
End Property

End Class
"Noozer" <do*******@me.here> wrote in message
news:_OJFf.454971$2k.199725@pd7tw1no...
I'm looking for a way to generate a "clone" of an object. Right now I need
to write a Clone function for every class that make and I'd like to have a
generic routine.

Instead of doing this:
For i = 0 to Me.Count-1
Set obj = New cSkill
obj.ID = Me.Item(i).ID
obj.Desc = Me.Item(i).Desc
obj.... etc.
Next

I'd like to do something like:
For i = 0 to Me.Count-1
Set obj = New cSkill
For j = 0 to number of properties
obj.property(j) = Me.Item(i).property(j)
Next
Next

Two problems... Is there a "property" property on objects? In my loop I
could be copying a simple variable or another object, so how do I know
when to say A=B or Set A=B.Clone ???
Here is my current clone function. This one is an array containing Skill
objects:

Public Function Clone()

Dim i 'General Counter

Feb 6 '06 #4
>Can't you just do
Set copyOfObject = theObject?


That wouldn't be a clone but rather two variables pointing at the same
object instance.

Typically one wants to clone an object in order to use its state as a
template for a new object.

Having made a clone some of it's properties would be modified. You would
not want the orginal object to have its state modified when changing values
on the clone. What is required is two independant objects each holding
their own state.
Feb 6 '06 #5

"Anthony Jones" <An*@yadayadayada.com> wrote in message
news:uk**************@TK2MSFTNGP15.phx.gbl...
There isn't a collection of properties on a VB Class so you will have to
create a clone method for each class.

At the very least I would suggest that you move the cloning of an objects
properties out of the collection class and into the class itself.
DOH!!!

I *DO* have a Clone function in each Class.

E.g.,

Your Skill class should have a clone method that returns another instance
of
a Skill object with the same properties. Then the code to clone the
collectiion class is pretty much boilerplate:-

Set Clone = New cSkills '<-- this is the only word that changes.

Hrm... I know that I shouldn't do it, but... Could I set Clone = New
Variant??? Is there no way to avoid having to replicate the function for
each class because of this one word???
Having said that I wonder where all the data comes from in the classes and
where it goes to.
MS SQL server...
Can I suggest that you consider XML as a mechanism for storing state. Not
only is cloning a lot easier but ultimately in ASP you want to generate
output as HTML and XSL would likely be useful in this regard.

Also many DB engines support XML fairly well making updating and
retrieving
data (even of heiarchical nature) reasonably easy (at least compared
loading
object heiarchies of VB Classes).


ARGH! Not another thing to learn!!!! : )

I was quite happy writing 6809 assember - forget this high level stuff!

....I'm fairly new to ASP, very new to MS SQL and now I should start looking
at XML. Looks like I'm going to be a busy boy.

Thanks for the help!
Feb 6 '06 #6

"Ray Costanzo [MVP]" <my first name at lane 34 dot commercial> wrote in
message news:%2****************@tk2msftngp13.phx.gbl...
Can't you just do

Set copyOfObject = theObject?


Nope the copy and the original object are still one and the same. Change one
and the other changes automatically.

There really should have been a Clone function built into VBScript.
Feb 6 '06 #7
>> Set Clone = New cSkills '<-- this is the only word that changes.


Hrm... I know that I shouldn't do it, but... Could I set Clone = New
Variant??? Is there no way to avoid having to replicate the function for
each class because of this one word???


Doh... Nevermind. I still need to duplicate the code in each Class anyhow,
so chaning one word ain't so bad.

Thanks again!
Feb 6 '06 #8
SQL Server is fairly XML friendly. I particularly like being able to use
the OPENXML command to pass large chunks of XML representing a heiarchy of
objects into a stored procedure for writing to the DB.

If your just getting in to ASP/SQL and other high level things then XML is
frankly mandatory :)

This is a good 101 tutorial:-

http://www.w3schools.com/xml/default.asp

then:-

http://www.w3schools.com/dom/default.asp

follow this with:-

http://www.w3schools.com/xsl/default.asp

then use SQL Server books online for OPENXML and SELECT FOR XML

Use MSDN to learn details of MSXML:-

http://msdn.microsoft.com/library/de...ae3597eb0c.asp

There might seem to be a lot learn but it's well worth the effort.

Anthony.
Feb 6 '06 #9

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

Similar topics

2
by: Ryan Mitchley | last post by:
Hi all I have code for an object factory, heavily based on an article by Jim Hyslop (although I've made minor modifications). The factory was working fine using g++, but since switching to the...
7
by: Harag | last post by:
Hi all If I create an object with the following: var ob1 = new objMyDefinedObject(); then I assign it to a new variable. var ob2 = ob1
7
by: mike p. | last post by:
I have a docbook xml file, and am using standard docbook 1.61.3 xsl stylesheets to do xhtml transform. Transform works fine when using MSXML. When I try to do the following using asp.net 1.1: ...
3
by: Jason | last post by:
Hi prolly a simple solution...how do i make a "copy" of an existing object eg object obj1 = new object(); object obj2 = obj1; if i change a property in obj2, it also changes obj1, since...
5
by: Kurt Lange | last post by:
How to I dereference a pointer to an object. I say dereference, because I don't now how to better explain this.... function firstFunction(byval obj as class1) as class1 dim returnVal as...
2
by: Rob R. Ainscough | last post by:
Is there an easy way to clone an object? What I've done in the past is write my own Clone method for each class i create and then just assign property values across objects. I was hoping VS...
2
by: Nathan | last post by:
I'm working with Clone() for the first time, and noticed that you have to unbox the Clone of an object that implements ICloneable: MyObject var1 = new MyObject(); // Where MyObject implements...
14
by: Philipp Reif | last post by:
Hi all, I've got a little hole in my head concerning references. Here's what I'm trying to do: I'm calling a function, passing the reference of a business object for editing. The function clones...
7
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.