如何在VB6中克隆对象 [英] How to clone an object in VB6

查看:281
本文介绍了如何在VB6中克隆对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to automatically clone an object without having to instantiate a new one and manually copy every single variable.

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

I remember back in the day (when I did VB6 everyday) I came up with a method of cloning objects using the PropertyBag, which was pretty cool. But I've lost the code and don't remember how to do it anymore.

有人记得或有其他方法吗?

Does anyone remember or have another method?

推荐答案

您在找什么?

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

You can serialize your data quickly by placing it into a PropertyBag object, then reading the PropertyBags Contents property. This property is really a Byte array that is a serial representation of the data in your PropertyBag object. You can use this byte array for many purposes, including an efficient means of data transmission over 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属性之前,将其转换为字节数组:

To retrieve the serialized data, simply create a new PropertyBag object and set the serialized string to its Contents property. Convert the string into a byte array before assigning it to the Contents property:

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

宾夕法尼亚州McKees Rocks的Mike Kurtz。

Mike Kurtz, McKees Rocks, Pa.

这篇关于如何在VB6中克隆对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆