Generic object Clone function?

N

Noozer

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
 
B

Bob Barrows [MVP]

Noozer said:
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
 
A

Anthony Jones

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.
 
R

Ray Costanzo [MVP]

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
 
A

Anthony Jones

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.
 
N

Noozer

Anthony Jones said:
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!
 
N

Noozer

Ray Costanzo said:
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.
 
N

Noozer

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!
 
A

Anthony Jones

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/d...html/b24aafc2-bf1b-4702-bf1c-b7ae3597eb0c.asp

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

Anthony.
 

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

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top