Results 1 to 15 of 15

Thread: Create Instance Object in Visual Basic 6.00

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Posts
    30
    how do i copy an object that having the same value, properties from other object?
    the object is come from the same class module.
    the other copy of object is needed for manipulation without interference to original object.
    thanks for all your info

  2. #2
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    I do not think there is anything built in

    So you will have to do it yourself.

    Normally I have a method called Clone within the class module which returns a new instance of the class with each of the members in the original cloned (copied) in the new instance.

    If any of the members are references to other objects, you will have to decide whether you want to clone or copy the reference. If you copy it, then both the original instance and the new one will reference the same (other) object. If you need to, you might choose to create a duplicate instance of the referenced object as well, and then the new instance will be completely separate from the original..

    Regards
    Paul Lewis

  3. #3
    Addicted Member S@NSIS's Avatar
    Join Date
    Aug 2000
    Location
    Stoke-On-Trent, England
    Posts
    243
    Hi,
    try creating a new instance of your object.
    Code:
    Dim myObj as New MyObject
    Hope this helps

    Shaun
    Web/Application Developer
    VB6 Ent (SP5), Win 2000,SQL Server 2000

  4. #4
    Frenzied Member mlewis's Avatar
    Join Date
    Sep 2000
    Posts
    1,226
    S@NSIS, that will not duplicate properties. That will create an unitialized copy of the object.

    Try:

    Set Obj2 = Obj1

    Where Obj1 is the original object and Obj2 is the object that you're copying Obj1 to.
    M. Lewis
    Pi-Q Software
    How many mouse clicks does it take to cook breakfast?

    Blargh! I am dead!

  5. #5
    Addicted Member S@NSIS's Avatar
    Join Date
    Aug 2000
    Location
    Stoke-On-Trent, England
    Posts
    243

    Thumbs up You're right!

    I totally misread the post (too many late night's I think)!!!
    Web/Application Developer
    VB6 Ent (SP5), Win 2000,SQL Server 2000

  6. #6
    Frenzied Member mlewis's Avatar
    Join Date
    Sep 2000
    Posts
    1,226
    those late nights sure wack you up, don't they?
    M. Lewis
    Pi-Q Software
    How many mouse clicks does it take to cook breakfast?

    Blargh! I am dead!

  7. #7
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Copy/Clone/Duplicate

    S@NSIS, your code will copy the object by one sense of the word, and if it is what aliank wants then cool.

    However, since he said "without interference to original object", I thin you will find that your method performs a bitwise copy of the original. This means that the two variables will bot reference the very same object.

    The code below demonstrates how your method will cause the property on the original object to be changed when the copied object's property is changed.


    Code:
    ' in a form module
    Option Explicit
    
    Private Sub Form_Load()
      Dim myA As Class1
      Dim myB As Class1
      
      Set myA = New Class1
      myA.A = 23
      
      Set myB = myA
      
      myB.A = 101
      
      Debug.Print myA.A, myB.A
      
    End Sub
    Code:
    ' in a class module 
    Option Explicit
    Private mA As Integer
    Public Property Let A(val As Integer)
      mA = val
    End Property
    Public Property Get A() As Integer
      A = mA
    End Property
    I hope this clarifies the difference for alaink and others

    Cheers
    Paul Lewis

  8. #8
    Frenzied Member mlewis's Avatar
    Join Date
    Sep 2000
    Posts
    1,226
    Didn't I just say that?
    M. Lewis
    Pi-Q Software
    How many mouse clicks does it take to cook breakfast?

    Blargh! I am dead!

  9. #9
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    OOps, I was replying to the wrong person...

    My previous post was meant for mlewis

    S@NSIS's suggestion would create a new (and quite separate) instance of the object. However none of the attributes would be set in the new object.

    mlewis correctly pointed this out. However mlewis, your suggestion will create a second variable which will have the same address in memory as the first.

    As per my previous post, this will cause you great concern if you truly wanted to copy an object and keep the two objects distinct.

    Regards
    Paul Lewis

  10. #10
    Guest
    Problem to be solved with the API.

    Code:
    Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
     (Destination As Any, Source As Any, ByVal Length As Long)
    
    
    Sub Dimmed()
        Dim a as Class1, Dim b As Class1
        Set a = New Class1
        a.a = 34
        CopyMemory (b, a, [#length])
        Debug.Print a.a & ", " & b.a
        b.a = 40
        Debug.Print a.a & ", " & b.a
    End Sub
    
    'Note: The code in class module should be the same as PLewis's.

  11. #11
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Interesting..

    Escaflowne,

    I have never approached the "problem" by using CopyMemory to duplicate an object. If you can elaborate on how to ensure that you have copied the entire object I'd be very interested.

    If it were a var declared as an instance of a UDT, then we could simply use the Len function. However, I haven't looked for nor heard of an equivalent for object manipulation.

    One this however that your example still falls short on (given that it is possible somehow). If I try to imagin the ibject looking like a UDT at runtime, then I can see your suggestion successfully copying some of the values in the object. However, for others, I see it would only copy the reference. For example, if the class had a member which was an instance of another class, or a Collection or something of that nature, then I am sure that the CopyMemory would not do the job for us.

    However, since we are all learning here, I'd bery much like to know if this is indeed possible. Although it breaks certain OO rules, I'd probably use it myself if it can be shown to work reliably

    Cheers
    Paul Lewis

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Posts
    30

    Smile Re: Interesting..

    isit secured to use API in this event. but thanks anyway for all of you outside there...

  13. #13
    Frenzied Member mlewis's Avatar
    Join Date
    Sep 2000
    Posts
    1,226
    I think this works for the following reason (note the I think is in need of very much emphasis, this is from memory from a book, it may be wrong)

    the first nnn bytes of an object is a pointer to the vtable (list of members) of that object. copying these 4 bytes will yield a perfect copy of the two objects, as the vtable would point to the same place, and thus the objects would use the same memory for their members.

    I think the nnn above is 4, but I may be wrong. Try it and see.

    NOTE: this will not work for controls, only Classes.
    M. Lewis
    Pi-Q Software
    How many mouse clicks does it take to cook breakfast?

    Blargh! I am dead!

  14. #14
    Guest
    Mlewis: Correct!

    The 4 memory...I'm not sure. You need to know the # of blocks of memory in order to use it. I'll try coming up with a DetectMemory Function, but yes, I did flip over my API knowledge.

  15. #15
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    Howdy,
    If this is correct, I am *VERY* interested. I'm working on a project right now where I have a CopyObject function that takes an object as an argument. It then procedes to copy each member over manually (my objects in this case are dynamic but still If there is a way to copy an object directly with copymemory, that'd save me alot of hassle

    Any more information on this subject would be greatly appreciated!

    -Excalibur

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width