Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > Generic object Clone function?

Reply
Thread Tools

Generic object Clone function?

 
 
Noozer
Guest
Posts: n/a
 
      02-06-2006
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


 
Reply With Quote
 
 
 
 
Bob Barrows [MVP]
Guest
Posts: n/a
 
      02-06-2006
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.


 
Reply With Quote
 
 
 
 
Anthony Jones
Guest
Posts: n/a
 
      02-06-2006
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.


 
Reply With Quote
 
Ray Costanzo [MVP]
Guest
Posts: n/a
 
      02-06-2006
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" <(E-Mail Removed)> 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



 
Reply With Quote
 
Anthony Jones
Guest
Posts: n/a
 
      02-06-2006
>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.


 
Reply With Quote
 
Noozer
Guest
Posts: n/a
 
      02-06-2006

"Anthony Jones" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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!


 
Reply With Quote
 
Noozer
Guest
Posts: n/a
 
      02-06-2006

"Ray Costanzo [MVP]" <my first name at lane 34 dot commercial> wrote in
message news:%(E-Mail Removed)...
> 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.


 
Reply With Quote
 
Noozer
Guest
Posts: n/a
 
      02-06-2006
>> 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!


 
Reply With Quote
 
Anthony Jones
Guest
Posts: n/a
 
      02-06-2006
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Wanted: generic deep copy for clone() Tim Tyler Java 28 08-08-2010 09:14 PM
generic interfaces with generic methods Murat Tasan Java 1 02-03-2009 12:17 PM
To clone or not to clone.. lordy Java 3 07-07-2006 04:30 PM
Generic class in a non generic class nramnath@gmail.com Java 2 07-04-2006 07:24 AM
Generic clone for deep copying - is this any good? castillo.bryan@gmail.com Java 4 12-24-2005 11:47 AM



Advertisments