Results 1 to 7 of 7

Thread: How to determine size of Object?

  1. #1

    Thread Starter
    Guru Clunietp's Avatar
    Join Date
    Oct 1999
    Location
    USA
    Posts
    1,844
    Hello people

    I am attempting to use the COPYMEMORY API function with Objects, and it expects the size of the object I am copying as a parameter. I am not using a string, so LEN or LENB won't work here.

    I've searched all over the net and in these archives here, but no answers yet....

    Is there anything in VB similar to the C++ sizeof function for OBJECTS? I need the size of my object.

    Ultimately, I just need to copy an object. Set NewVar = OldVar just gives me a reference to the original object, I need a copy of it (without resorting to looping through the properties of the old object and assigning them to a new virgin object)

    Thanks for your help!


    Tom

    [Edited by Clunietp on 07-13-2000 at 02:28 AM]

  2. #2
    Lively Member
    Join Date
    Jul 2000
    Location
    Vaxjo, Sweden
    Posts
    85
    I don't know of a way to get the object size, but couldn't you just write a clone function for it? I'm doing that myself and it works very well, and you don't have the hazzle of messing directly with the memory.

    //Anders

  3. #3

    Thread Starter
    Guru Clunietp's Avatar
    Join Date
    Oct 1999
    Location
    USA
    Posts
    1,844
    I tried to write a Clone function like this:

    Code:
    Public Function CloneIt(ByVal TheObject as Object) as Object
        Set CloneIt = TheObject
    End Function
    I figured if I passed it ByVal, then back again, it would copy it -- but it didn't

    Can you show me your clone function?

    Thanks Anders

  4. #4

    Thread Starter
    Guru Clunietp's Avatar
    Join Date
    Oct 1999
    Location
    USA
    Posts
    1,844
    Anders' clone function didnt work....

    anyone else?

    Thanks!

  5. #5
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    You Can't Copy the Object like that I'm afraid, VB stores an Object as a long, which is a pointer to an array of function pointers, these functions are what make up the object. The Best way to Clone an object is to have a clone function Inside the object, have Friend Property Lets in the Object for every variable in your object, and have a clone property which creates a new instance of itself and uses the Freind properties to set each Variable in the new object

    Something like this

    Code:
    Option Explicit
    
    
    Dim m_Public1 As Long
    Dim m_Public2 As Long
    Dim m_Internal1 As Long
    Dim m_Internal2 As Long
    Dim m_Internal3 As Long
    
    
    
    
    Public Property Get Public1() As Long
        Public1 = m_Public1
    End Property
    
    Public Property Let Public1(ByVal New_Public1 As Long)
        m_Public1 = New_Public1
    End Property
    
    
    Public Property Get Public2() As Long
        Public2 = m_Public2
    End Property
    
    Public Property Let Public2(ByVal New_Public2 As Long)
        m_Public2 = New_Public2
    End Property
    
    
    
    Friend Property Let Internal1(ByVal New_Internal1 As Long)
        m_Internal1 = New_Internal1
    End Property
    
    Friend Property Let Internal2(ByVal New_Internal2 As Long)
        m_Internal2 = New_Internal2
    End Property
    
    Friend Property Let Internal3(ByVal New_Internal3 As Long)
        m_Internal3 = New_Internal3
    End Property
    
    
    
    Public Property Get Clone() As Class1
    Dim retval As New Class1
    
    retval.Internal1 = m_Internal1
    retval.Internal2 = m_Internal2
    retval.Internal3 = m_Internal3
    retval.Public1 = m_Public1
    retval.Public2 = m_Public2
    
    Set Clone = retval
    
    End Property
    if the class contains any internal classes they'll have to have clone methods too, If any of the Classes aren't your own then you're pretty stuck.

    What was Anders Clone Function BTW?

  6. #6

    Thread Starter
    Guru Clunietp's Avatar
    Join Date
    Oct 1999
    Location
    USA
    Posts
    1,844
    Thanks Sam, but I don't have access to the class module of the object I am copying, and I need this to work with any type of object.

    This seems to work OK for now, but I'm not sure what my max object size could be for any type of object....

    CopyMemory ByVal (ObjPtr(rsOriginal)), ByVal (ObjPtr(rsInput)), 1200

  7. #7

    Thread Starter
    Guru Clunietp's Avatar
    Join Date
    Oct 1999
    Location
    USA
    Posts
    1,844
    Anders had a clone function basically what you had...the function was internal to the object I was cloning

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