Generic object Clone function?

Discussion in 'ASP General' started by Noozer, Feb 6, 2006.

  1. Noozer

    Noozer Guest

    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
     
    Noozer, Feb 6, 2006
    #1
    1. Advertising

  2. 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.
     
    Bob Barrows [MVP], Feb 6, 2006
    #2
    1. Advertising

  3. 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.
     
    Anthony Jones, Feb 6, 2006
    #3
  4. 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" <> 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
     
    Ray Costanzo [MVP], Feb 6, 2006
    #4
  5. >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.
     
    Anthony Jones, Feb 6, 2006
    #5
  6. Noozer

    Noozer Guest

    "Anthony Jones" <> wrote in message
    news:...
    > 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!
     
    Noozer, Feb 6, 2006
    #6
  7. Noozer

    Noozer Guest

    "Ray Costanzo [MVP]" <my first name at lane 34 dot commercial> wrote in
    message news:%...
    > 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.
     
    Noozer, Feb 6, 2006
    #7
  8. Noozer

    Noozer Guest

    >> 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!
     
    Noozer, Feb 6, 2006
    #8
  9. 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.
     
    Anthony Jones, Feb 6, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim Tyler
    Replies:
    28
    Views:
    6,293
    Words13100
    Aug 8, 2010
  2. Murat Tasan
    Replies:
    1
    Views:
    8,130
    Chaitanya
    Feb 3, 2009
  3. Replies:
    4
    Views:
    3,918
    Roedy Green
    Dec 24, 2005
  4. Replies:
    2
    Views:
    478
  5. lordy

    To clone or not to clone..

    lordy, Jul 7, 2006, in forum: Java
    Replies:
    3
    Views:
    750
    lordy
    Jul 7, 2006
Loading...

Share This Page