Home All Groups Group Topic Archive Search About
Author
9 Aug 2010 11:15 PM
Phil Hunt
I have a simple object I load up from DB, Is there a easy way to clone it to
a new object of the same type ?

Thanks

Author
10 Aug 2010 1:34 AM
ralph
On Mon, 9 Aug 2010 19:15:55 -0400, "Phil Hunt" <a**@aaa.com> wrote:

>I have a simple object I load up from DB, Is there a easy way to clone it to
>a new object of the same type ?
>

Probably not. <g>

Depends on what you mean by "clone", ie, how much of a duplicate/copy
do you need, or if you even need one?

Cloning 'data' always takes you into the realm of "Shallow Copies" or
"Deep Copies", but in some cases it isn't the data but the objects's
behavior or state we want to 'clone'. (The ADO Recordset.Clone is an
example of the latter.)

We need more information.

-ralph
Author
10 Aug 2010 1:41 AM
phil hunt
I am more interested in the data. As I am changing data, I want to 'undo'
the change just by reverting back to the clone.


Show quoteHide quote
"ralph" <nt_consultin***@yahoo.net> wrote in message
news:haa166hspc24r3esjk8h0u712u2jebjcng@4ax.com...
> On Mon, 9 Aug 2010 19:15:55 -0400, "Phil Hunt" <a**@aaa.com> wrote:
>
>>I have a simple object I load up from DB, Is there a easy way to clone it
>>to
>>a new object of the same type ?
>>
>
> Probably not. <g>
>
> Depends on what you mean by "clone", ie, how much of a duplicate/copy
> do you need, or if you even need one?
>
> Cloning 'data' always takes you into the realm of "Shallow Copies" or
> "Deep Copies", but in some cases it isn't the data but the objects's
> behavior or state we want to 'clone'. (The ADO Recordset.Clone is an
> example of the latter.)
>
> We need more information.
>
> -ralph
Author
10 Aug 2010 2:28 AM
ralph
On Mon, 9 Aug 2010 21:41:28 -0400, "phil hunt" <a@abc.com> wrote:

>I am more interested in the data. As I am changing data, I want to 'undo'
>the change just by reverting back to the clone.
>
>

Don't know what your 'data' looks like, but assuming it is a group of
properties, this is often accomplished by storing the last value for
each of your properties/attributes as a backup. Which is something I
figure you have already considered. Takes a bit of work.

It means everytime you change something, you have to reassign the
'backup'. If for some reason you want to undo, you either have to
create new 'Undo' method for each value, or do a blanket 'put-back'.

So it depends on how and when you feel you might need to undo, and the
granularity - per item, per process, per run, etc.

Perhaps 'Serialization' would be something to look into. For objects
that actually refers to packaging up all the data/state, etc and
trasporting it, or storing it, so you can come back later and read the
info to recreate the object. But you can also simply considered the
serialized data as a backup.

Your object would have a 'Save' method, just before you planned to do
something dangerous, where it simply stores everything. Then an 'Undo'
on an Oops. <g>

This also has the advantage of being able to perform muliple undos.

For an example, I have often done this for disconnected
ADODB.Recordsets, by wrapping the Recordset with my own object. Then
use the contained Recordset.Save feature to store a copy to a file.
Bang around on the original Recordset, then if I have to go back,
reload a new from the file.

But again it depends on what is needed for YOUR problem domain.

Any option you chose will look like a lot of typing, thus not easy,
but once you pick a particular strategy, most of it will be
boiler-plate.

-ralph
Author
10 Aug 2010 2:38 AM
ralph
On Mon, 09 Aug 2010 21:28:53 -0500, ralph <nt_consultin***@yahoo.net>
wrote:

>On Mon, 9 Aug 2010 21:41:28 -0400, "phil hunt" <a@abc.com> wrote:
>
>>I am more interested in the data. As I am changing data, I want to 'undo'
>>the change just by reverting back to the clone.
>>
>>
>

Another strategy that is occasionally useful, is based on a timeless
principle of programming: "There is no problem that can't be resolve
with another layer of indirection". <g>

Wrap your object with one that combines a factory with storage.

-ralph
Author
10 Aug 2010 3:45 AM
phil hunt
It sounds too much like work. Maybe some other time. But I am curious what
you mean by 'factory'. I came across them in some other language, but not
VB6

Show quoteHide quote
"ralph" <nt_consultin***@yahoo.net> wrote in message
news:4fe166538h1e7enblsfdm460dgfjaa1bs6@4ax.com...
> On Mon, 09 Aug 2010 21:28:53 -0500, ralph <nt_consultin***@yahoo.net>
> wrote:
>
>>On Mon, 9 Aug 2010 21:41:28 -0400, "phil hunt" <a@abc.com> wrote:
>>
>>>I am more interested in the data. As I am changing data, I want to 'undo'
>>>the change just by reverting back to the clone.
>>>
>>>
>>
>
> Another strategy that is occasionally useful, is based on a timeless
> principle of programming: "There is no problem that can't be resolve
> with another layer of indirection". <g>
>
> Wrap your object with one that combines a factory with storage.
>
> -ralph
Author
10 Aug 2010 5:57 AM
ralph
On Mon, 9 Aug 2010 23:45:37 -0400, "phil hunt" <a@abc.com> wrote:

>It sounds too much like work. Maybe some other time. But I am curious what
>you mean by 'factory'. I came across them in some other language, but not
>VB6
>

A Factory pattern/method is where you define an interface for creating
an object, and then let it decide who and what to create. In your case
also providing a Proxy - a surrogate to control access to the object/s
created. eg, swapping out objects with the client being none the
wiser.

Factory (and Proxy) are what is called 'Design Patterns'. A way of
describing general reuseable software architecture.

http://en.wikipedia.org/wiki/Design_pattern_(computer_science)
and
http://en.wikipedia.org/wiki/Factory_method_pattern

A Factory/Proxy in this case, would be a mechanism to simply store
whole objects instead of serializing them.

-ralph
Author
10 Aug 2010 10:19 AM
Jason Keats
phil hunt wrote:
> I am more interested in the data. As I am changing data, I want to 'undo'
> the change just by reverting back to the clone.


If you want to ignore all changes to your object, then the easiest thing
to do is simply throw it away and reload it from the database.

Cloning would probably mean you have to write code to serialise and
deserialise your object's state (unless you use Ralph's suggestion of
saving the ADO recordset you used to populate your object, to XML).

If, however, you needed to keep track of changes to each property - so
you can undo undo changes one property at a time - then you probably
want to keep a LIFO stack containing your changes.

Many use code generation (eg one class per table) to solve this sort of
problem, especially if similar code is required for many classes.
Author
10 Aug 2010 2:10 PM
Nobody
"Phil Hunt" <a**@aaa.com> wrote in message
news:%2333oQjBOLHA.4824@TK2MSFTNGP05.phx.gbl...
>I have a simple object I load up from DB, Is there a easy way to clone it
>to a new object of the same type ?

MZTools lists all properties of your classes when you use "Generate XML
Documentation". You need to copy them to Excel or similar program to extract
property names.

Also, the code below dumps a list of properties, methods, and events of your
class from Object Browser, so you can easily write code to save the
contents. It uses SendKeys and copy from the Clipboard. To use it, bring up
Object Browser(F2), then select your project name from the list, then click
on your class. You would see the list of properties on the right pan. Right
click anywhere and make sure that "Group Members" is selected. Click on the
first property on the right pan, then start a new VB IDE instance, add a
Timer to Form1, then add the code below. Run the project, and switch to the
VB IDE that is showing Object Browser. After 5 seconds, the running program
would capture property names and print them to the immediate window. From
there, you can copy them and do with them whatever you want.

Option Explicit

Private Sub Form_Load()
    Timer1.Interval = 5000
End Sub

Private Sub Timer1_Timer()
    Timer1.Enabled = False
    DumpObjectBrowser
End Sub

Private Sub DumpObjectBrowser()
    Dim Str As String
    Dim LastStr As String
    Dim Counter As Long

    Clipboard.Clear
    Counter = 0
    Do
        LastStr = Str
        SendKeys "^c", True
        Delay 0.1
        Str = Clipboard.GetText
        If LastStr = Str Then
            ' We reached the end
            Exit Do
        End If
        Debug.Print Str
        SendKeys "{DOWN}", True
        Delay 0.1
        Counter = Counter + 1
    Loop Until Counter = 100
    Me.Caption = "Done!"
End Sub

Private Sub Delay(ByVal Seconds As Single)
    Dim t As Single

    t = Timer
    Do While Abs(Timer - t) < Seconds
        DoEvents
    Loop
End Sub
Author
10 Aug 2010 3:05 PM
Phil Hunt
Intersting.
I am thinking about cloning the data, not the definition.
It is academic now. But can I use a Property bag in my situation. I seen it,
but never play with it.


Show quoteHide quote
"Nobody" <nob***@nobody.com> wrote in message
news:i3rmlh$c7r$1@speranza.aioe.org...
> "Phil Hunt" <a**@aaa.com> wrote in message
> news:%2333oQjBOLHA.4824@TK2MSFTNGP05.phx.gbl...
>>I have a simple object I load up from DB, Is there a easy way to clone it
>>to a new object of the same type ?
>
> MZTools lists all properties of your classes when you use "Generate XML
> Documentation". You need to copy them to Excel or similar program to
> extract property names.
>
> Also, the code below dumps a list of properties, methods, and events of
> your class from Object Browser, so you can easily write code to save the
> contents. It uses SendKeys and copy from the Clipboard. To use it, bring
> up Object Browser(F2), then select your project name from the list, then
> click on your class. You would see the list of properties on the right
> pan. Right click anywhere and make sure that "Group Members" is selected.
> Click on the first property on the right pan, then start a new VB IDE
> instance, add a Timer to Form1, then add the code below. Run the project,
> and switch to the VB IDE that is showing Object Browser. After 5 seconds,
> the running program would capture property names and print them to the
> immediate window. From there, you can copy them and do with them whatever
> you want.
>
> Option Explicit
>
> Private Sub Form_Load()
>    Timer1.Interval = 5000
> End Sub
>
> Private Sub Timer1_Timer()
>    Timer1.Enabled = False
>    DumpObjectBrowser
> End Sub
>
> Private Sub DumpObjectBrowser()
>    Dim Str As String
>    Dim LastStr As String
>    Dim Counter As Long
>
>    Clipboard.Clear
>    Counter = 0
>    Do
>        LastStr = Str
>        SendKeys "^c", True
>        Delay 0.1
>        Str = Clipboard.GetText
>        If LastStr = Str Then
>            ' We reached the end
>            Exit Do
>        End If
>        Debug.Print Str
>        SendKeys "{DOWN}", True
>        Delay 0.1
>        Counter = Counter + 1
>    Loop Until Counter = 100
>    Me.Caption = "Done!"
> End Sub
>
> Private Sub Delay(ByVal Seconds As Single)
>    Dim t As Single
>
>    t = Timer
>    Do While Abs(Timer - t) < Seconds
>        DoEvents
>    Loop
> End Sub
>
>
>
Author
10 Aug 2010 4:17 PM
ralph
On Tue, 10 Aug 2010 11:05:40 -0400, "Phil Hunt" <a**@aaa.com> wrote:

>Intersting.
>I am thinking about cloning the data, not the definition.
>It is academic now. But can I use a Property bag in my situation. I seen it,
>but never play with it.
>

lol

VB's built-in serialization product.

Glad you stumbled across it. I'd forgotten about them, and you did ask
for "easy" too. Dop! <g>

Just a word of warning. Property Bags store data as Variants and as we
know VB is very obliging when it comes to coercing a value into a
Variant, or in coercing a value out of one. Dates and alphanumeric
strings can often be troublesome. So make sure you have clear business
rules for conversion. It also is useful to add additional "format" or
"type" properties to insure there are no misunderstandings.

-ralph
Author
10 Aug 2010 4:29 PM
Dee Earley
On 10/08/2010 17:17, ralph wrote:
Show quoteHide quote
> On Tue, 10 Aug 2010 11:05:40 -0400, "Phil Hunt"<***@aaa.com>  wrote:
>
>> Intersting.
>> I am thinking about cloning the data, not the definition.
>> It is academic now. But can I use a Property bag in my situation. I seen it,
>> but never play with it.
>>
>
> lol
>
> VB's built-in serialization product.
>
> Glad you stumbled across it. I'd forgotten about them, and you did ask
> for "easy" too. Dop!<g>
>
> Just a word of warning. Property Bags store data as Variants and as we
> know VB is very obliging when it comes to coercing a value into a
> Variant, or in coercing a value out of one. Dates and alphanumeric
> strings can often be troublesome. So make sure you have clear business
> rules for conversion. It also is useful to add additional "format" or
> "type" properties to insure there are no misunderstandings.

The data in a property bag is typed, it may be implicit via the variant
type, but you do get out the same data type you put in.

--
Dee Earley (dee.ear***@icode.co.uk)
i-Catcher Development Team

iCode Systems

(Replies direct to my email address will be ignored.
Please reply to the group.)
Author
10 Aug 2010 4:52 PM
ralph
On Tue, 10 Aug 2010 17:29:20 +0100, Dee Earley
<dee.ear***@icode.co.uk> wrote:


>
>The data in a property bag is typed, it may be implicit via the variant
>type, but you do get out the same data type you put in.

An absolute given.

I stand corrected, and the OP should ignore my advice as absolutely
nothing can ever go wrong.

-ralph
Author
10 Aug 2010 5:14 PM
Phil Hunt
On that note, please considered it closed.

Show quoteHide quote
"ralph" <nt_consultin***@yahoo.net> wrote in message
news:al0366llu6epngs7udnlhqoett4c70rsns@4ax.com...
> On Tue, 10 Aug 2010 17:29:20 +0100, Dee Earley
> <dee.ear***@icode.co.uk> wrote:
>
>
>>
>>The data in a property bag is typed, it may be implicit via the variant
>>type, but you do get out the same data type you put in.
>
> An absolute given.
>
> I stand corrected, and the OP should ignore my advice as absolutely
> nothing can ever go wrong.
>
> -ralph