Dim objPB As PropertyBag
Dim strValueToPersist As String
Private Sub Command1_Click()
strValueToPersist = "TestingPersist"
Set objPB = New PropertyBag
Call objPB.WriteProperty("PersistValue", strValueToPersist, vbNullString)
' 为了能够实现从PropertyBag读取数据,可以使用ReadProperty方法。
'这一方法的参数包含有一个属性名称和一个缺省的数值,返回值是属性的数值:
'strValueToPersist = objPB.ReadProperty("PersistValue", vbNullString)
MsgBox objPB.ReadProperty("PersistValue", vbNullString)
' 为了保存PropertyBag对象的内容,可以将内容属性保存到一个二进制字节的数组或一个变量。
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Dim vntContents As Variant
'vntContents = objPB.Contents
'' 注释:Save to desired location
''
''' 为了能够从PropertyBag对象中重新获取信息,将保存数值读入一个二进制的数组。
''然后将内容属性设置为二进制数组,这样所有的属性数值将可以重新获得。
'
'Dim b() As Byte
'Dim vntContents As Variant
'
'' 注释:Read saved contents into vntContents from saved location
'
'Set objPB = New PropertyBag
'b = vntContents
'objPB.Contents = b
End Sub
Private Sub Command2_Click()
' Class1 MySetting
Dim obj, obj2 As Class1
Set objPB = New PropertyBag
Set obj = New Class1
obj.MySetting = "hello Class1"
Call objPB.WriteProperty("PersistValue", obj, Nothing)
Set obj = Nothing
obj2 = objPB.ReadProperty("PersistValue", Nothing)
MsgBox obj2.MySetting
End Sub
Private Sub Command1_Click()
Dim obj As Persist
Dim pb As PropertyBag
Set obj = New Persist
Debug.Print 1 & ":" & obj.MySetting
obj.MySetting = "Testing"
Debug.Print 2 & obj.MySetting
Set pb = New PropertyBag
'注释: Save object
Call pb.WriteProperty("PersistSample", obj)
Set obj = Nothing
'注释: Restore object
Set obj = pb.ReadProperty("PersistSample")
Debug.Print 3 & obj.MySetting
End Sub
Private cstrMySetting As String
'当Class_InitProperties第一次建立时即初始化类中的属性:
Private Sub Class_InitProperties()
cstrMySetting = vbNullString
End Sub
'在每一个属性中,Property Let函数调用PropertyChanged,将把这一属性标记为“dirty”。
'如果任一属性被标记为“dirty”,Class_WriteProperties函数即被调用,过程如下所示:
Public Property Let MySetting(ByValNewValue As String)
cstrMySetting = NewValue
PropertyChanged "MySetting"
End Property
Public Property Get MySetting() As String
MySetting = cstrMySetting
End Property
'Class_ReadProperties函数可以从一个属性标签中返回一个对象的值:
Private Sub Class_ReadProperties(PropBag As PropertyBag)
cstrMySetting = PropBag.ReadProperty("MySetting", vbNullString)
End Sub
'Class_WriteProperties函数将数值写入到属性标签:
Private Sub Class_WriteProperties(PropBag As PropertyBag)
Call PropBag.WriteProperty("MySetting", cstrMySetting, vbNullString)
End Sub
PropertyBag
最新推荐文章于 2019-05-17 10:17:40 发布