如何在VB6中克隆对象

时间:2009-02-24 06:01:17

标签: vb6 clone propertybag

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

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

有人记得还是有其他方法?

4 个答案:

答案 0 :(得分:4)

我过去使用的方法是将所有实例变量放在UDT上。只要您使UDT保持最新,您就可以使用单个方法/语句复制类的数据。

给出一个“Person”类,这是一个简单的例子:

Private Type tPerson
    ID As Long
    FirstName As String
    LastName As String
End Type  

Private m_Person As tPerson  

Public Sub InitPerson(ID As Long, FirstName As String, LastName As String)
    m_Person.ID = ID
    m_Person.FirstName = FirstName
    m_Person.LastName = LastName
End Sub  

Friend Sub SetData(PersonData As tPerson)
    m_Person = PersonData

End Sub  

Public Function GetClone() As Person
    Dim p As New Person
    p.SetData m_Person

    Set GetClone = p

End Function  

Public Property Get FirstName() As String
    FirstName = m_Person.FirstName

End Property  

Private Type tPerson ID As Long FirstName As String LastName As String End Type Private m_Person As tPerson Public Sub InitPerson(ID As Long, FirstName As String, LastName As String) m_Person.ID = ID m_Person.FirstName = FirstName m_Person.LastName = LastName End Sub Friend Sub SetData(PersonData As tPerson) m_Person = PersonData End Sub Public Function GetClone() As Person Dim p As New Person p.SetData m_Person Set GetClone = p End Function Public Property Get FirstName() As String FirstName = m_Person.FirstName End Property

尝试代码:

Dim p As New Person
p.InitPerson 1, "MyName", "MyLastName"

Dim p2 As Person
Set p2 = p.GetClone

MsgBox p2.FirstName

如果你在UDT中维护所有实例变量而不是单独声明它们,你可以使用简单的Clone方法,只需要很少的维护。

另一个优点是您可以将UDT放到文件句柄中,以便快速序列化到磁盘。

Dim p As New Person p.InitPerson 1, "MyName", "MyLastName" Dim p2 As Person Set p2 = p.GetClone MsgBox p2.FirstName

一个穷人的序列化解决方案真的: - )

答案 1 :(得分:4)

this您要找的是什么?文章在下面复制给后人。

使用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

要检索序列化数据,只需创建一个新的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

Mike Kurtz,McKees Rocks,Pa。

答案 2 :(得分:1)

另请阅读:Persisting a Component's Data

答案 3 :(得分:1)

我的应用程序框架中的每个对象都有一个Read和Store方法。我所做的是传递一个写入bytearray并存储字节数组的流。虽然这并没有消除必须处理每个属性,你只需要处理一次阅读和一次处理。

另一种方法是使用像Darrel Miller这样的财产袋,但你仍然必须单独处理每个房产。如果您已经阅读并存储,那么我的建议应该节省一些时间。