关于对象的持久保存

'testDLL 中的代码
'================================================
Option Explicit
    Private strT As String
    Private myT As Variant
    Private myP As StdPicture
    Private P As StdPicture

Public Property Get myTest() As String
    myTest = strT
    PropertyChanged "myTest"
End Property

Public Property Let myTest(newTest As String)
    strT = newTest
    PropertyChanged "myTest"
End Property

Public Property Let loadP(newP As StdPicture)
    Set P = newP
    PropertyChanged "loadP"
End Property

Public Property Get loadP() As StdPicture
    Set loadP = P
    PropertyChanged "loadP"
End Property

Private Sub Class_ReadProperties(PropBag As PropertyBag)
    myTest = PropBag.ReadProperty("myTest", myT)
    loadP = PropBag.ReadProperty("loadP", myP)
End Sub


Private Sub Class_WriteProperties(PropBag As PropertyBag)
    PropBag.WriteProperty "myTest", myTest, myT
    PropBag.WriteProperty "loadP", loadP, myP
End Sub
'================================================

'工程1中的代码
'================================================
Option Explicit
    Public objT  As testDLL.class1
    Private pb As PropertyBag
    Private p As StdPicture
   
Private Sub Command1_Click()
   
    objT.myTest = Text1.Text
    Set p = LoadPicture(VB.App.Path & "/10.jpg")
    objT.loadP = p
End Sub

Private Sub Command2_Click()
On Error Resume Next
   Dim varTemp As Variant
    Dim F As Long
   ' PropertyBag 对象的实例化。
   Set pb = New PropertyBag
   ' 使用 WriteProperty 将对象保存在 PropertyBag 中。
   pb.WriteProperty "MyObj", objT
   ' 将 PropertyBag 的内容赋予一个 Variant。
   varTemp = pb.Contents
   ' 将其保存到一个文本文件中。
   F = FreeFile
   Open "D:/objT.txt" For Binary As #F
   Put #F, , varTemp
   Close #F

End Sub

Private Sub Command3_Click()
On Error Resume Next
    Image1.Picture = p
    MsgBox objT.myTest
End Sub

Private Sub Form_Load()
    Set objT = New testDLL.class1
    Set pb = New PropertyBag
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Set objT = Nothing
    Set pb = Nothing
End Sub

'================================================

'工程2中的代码
'================================================
Option Explicit

    Private objT As testDLL.Class1
    Private pb As PropertyBag

Private Sub Command1_Click()
'On Error Resume Next
   Dim varTemp As Variant
   Dim byteArr() As Byte
    Dim F As Long
   ' PropertyBag 对象的实例化。
   Set pb = New PropertyBag
   ' 将文件的内容读入一个 Variant 中。
   F = FreeFile
   Open "D:/objT.txt" For Binary As #F
   Get #F, , varTemp
   Close #F
   ' 将 Variant 赋值到一个 Byte 数组中。
   byteArr = varTemp
   ' 赋予 PropertyBag Contents 属性。
   pb.Contents = byteArr
   ' 使用 PropertyBag 进行对象实例化。
   Set objT = pb.ReadProperty("MyObj")

End Sub

Private Sub Command2_Click()
On Error Resume Next
    Image1.Picture = objT.loadP
    MsgBox objT.myTest
End Sub

Private Sub Form_Load()
    Set pb = New PropertyBag
End Sub

Private Sub Form_Unload(Cancel As Integer)
    Set objT = Nothing
    Set pb = Nothing
End Sub

'================================================
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值