如何在VB6中克隆对象

Ang*_*ker 5 vb6 clone propertybag

我试图自动克隆一个对象,而不必实例化一个新的并手动复制每个变量.

我记得当天(当我每天都做VB6时)我想出了一种使用PropertyBag克隆对象的方法,这非常酷.但是我丢失了代码,不记得怎么做了.

有没有人记得或有另一种方法?

Dar*_*ler 4

是您要找的吗?文章抄录如下,以供后人参考。

使用 PropertyBag 序列化数据

您可以通过将数据放入 PropertyBag 对象中,然后读取 PropertyBags Contents 属性来快速序列化数据。该属性实际上是一个 Byte 数组,它是 PropertyBag 对象中数据的串行表示形式。您可以将此字节数组用于多种用途,包括通过 DCOM 进行数据传输的有效方法:

Private Function PackData() As String
    Dim pbTemp  As PropertyBag

    'Create a new PropertyBag object
    Set pbTemp = New PropertyBag
    With pbTemp
        'Add your data to the PB giving each item a 
        'unique string key
        Call .WriteProperty("FirstName", "John")
        Call .WriteProperty("MiddleInitial", "J")
        Call .WriteProperty("LastName", "Doe")

        'Place the serialized data into a string 
        'variable.
        Let PackData = .Contents
    End With

    Set pbTemp = Nothing
End Function
Run Code Online (Sandbox Code Playgroud)

要检索序列化数据,只需创建一个新的 PropertyBag 对象并将序列化字符串设置为其 Contents 属性即可。在将字符串分配给 Contents 属性之前,将其转换为字节数组:

Private Sub UnPackData(sData As String)
    Dim pbTemp  As PropertyBag
    Dim arData()    As Byte

    'Convert the string representation of the data to 
    'a Byte array
    Let arData() = sData

    'Create a new PropertyBag object
    Set pbTemp = New PropertyBag
    With pbTemp
        'Load the PropertyBag with data
        Let .Contents = arData()

        'Retrieve your data using the unique key
        Let m_sFirstName = .ReadProperty("FirstName")
        Let m_sMiddleInitial = _
            .ReadProperty("MiddleInitial")
        Let m_sLastName = .ReadProperty("LastName")
    End With

    Set pbTemp = Nothing
      End Sub
Run Code Online (Sandbox Code Playgroud)

迈克·库尔茨 (Mike Kurtz),宾夕法尼亚州麦基斯岩石