VB中如何把一个对象写入到文件中 请教一个问题我在VB中定一个A类然后再定义一个A类的对象b现在如何把b 以二进制方式写入到一个文件中请大家帮帮忙,急啊 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以用PropertyBag来实现简单的串行化,当然VB提供的持久化方法也是用这个。如:'以下在Class1中Option ExplicitPublic id As LongPublic data As VariantPublic Property Get Contents() As Variant Dim objPropBag As PropertyBag Set objPropBag = New PropertyBag objPropBag.WriteProperty "id", id, 0 objPropBag.WriteProperty "data", data, Empty Contents = objPropBag.Contents Set objPropBag = Nothing End PropertyPublic Property Let Contents(ByVal vData As Variant) Dim objPropBag As PropertyBag Set objPropBag = New PropertyBag objPropBag.Contents = vData id = objPropBag.ReadProperty("id", 0) data = objPropBag.ReadProperty("data", Empty) Set objPropBag = Nothing End Property'测试' 把对象持久化并写入文件Private Sub Command1_Click() Dim obj As Class1 Dim vData() As Byte Dim intFileNum As Integer Set obj = New Class1 obj.id = 123 obj.data = "hello world" vData() = obj.Contents intFileNum = FreeFile() Open "c:\test.dat" For Binary As intFileNum Put intFileNum, , vData() Close intFileNum End Sub' 从文件中读取数据并还原对象Private Sub Command2_Click() Dim obj As Class1 Dim vData() As Byte Dim intFileNum As Integer Open "c:\test.dat" For Binary As intFileNum ReDim vData(LOF(intFileNum) - 1) Get intFileNum, , vData() Close intFileNum Set obj = New Class1 obj.Contents = vData Debug.Print "id = "; obj.id Debug.Print "data = "; obj.data End Sub sorry,上面最后一个方法有错误' 从文件中读取数据并还原对象Private Sub Command2_Click() Dim obj As Class1 Dim vData() As Byte Dim intFileNum As Integer intFileNum = FreeFile() Open "c:\test.dat" For Binary As intFileNum ReDim vData(LOF(intFileNum) - 1) Get intFileNum, , vData() Close intFileNum Set obj = New Class1 obj.Contents = vData Debug.Print "id = "; obj.id Debug.Print "data = "; obj.data End Sub 新手请教一个问题,关于Excel的问题,大家进来看看~! 16进制大数转long型怎么办 这样写为什么不能显示ACCESS数据库中表的内容嘛? 菜鸟求助。FOR循环太快了,能不能实现三秒这样时间执行一次循环体中的内容? 细节题: odbc连接sql2000的问题,求教高手 日期控件哪有下载 一个动态数组,怎么把它初始化为声明时的状态啊? 在VB里当拖到form窗体时会触发什么事件??? 帮帮忙吧!。。。火急! 求助彩信群发软件源代码及文件导入功能实现 使用大恒采集卡时单采和实时采集到的图象不一致,是什么原因?
'以下在Class1中
Option ExplicitPublic id As Long
Public data As VariantPublic Property Get Contents() As Variant
Dim objPropBag As PropertyBag
Set objPropBag = New PropertyBag
objPropBag.WriteProperty "id", id, 0
objPropBag.WriteProperty "data", data, Empty
Contents = objPropBag.Contents
Set objPropBag = Nothing
End PropertyPublic Property Let Contents(ByVal vData As Variant)
Dim objPropBag As PropertyBag
Set objPropBag = New PropertyBag
objPropBag.Contents = vData
id = objPropBag.ReadProperty("id", 0)
data = objPropBag.ReadProperty("data", Empty)
Set objPropBag = Nothing
End Property'测试' 把对象持久化并写入文件
Private Sub Command1_Click()
Dim obj As Class1
Dim vData() As Byte
Dim intFileNum As Integer
Set obj = New Class1
obj.id = 123
obj.data = "hello world"
vData() = obj.Contents
intFileNum = FreeFile()
Open "c:\test.dat" For Binary As intFileNum
Put intFileNum, , vData()
Close intFileNum
End Sub' 从文件中读取数据并还原对象
Private Sub Command2_Click()
Dim obj As Class1
Dim vData() As Byte
Dim intFileNum As Integer
Open "c:\test.dat" For Binary As intFileNum
ReDim vData(LOF(intFileNum) - 1)
Get intFileNum, , vData()
Close intFileNum
Set obj = New Class1
obj.Contents = vData
Debug.Print "id = "; obj.id
Debug.Print "data = "; obj.data
End Sub
' 从文件中读取数据并还原对象
Private Sub Command2_Click()
Dim obj As Class1
Dim vData() As Byte
Dim intFileNum As Integer
intFileNum = FreeFile()
Open "c:\test.dat" For Binary As intFileNum
ReDim vData(LOF(intFileNum) - 1)
Get intFileNum, , vData()
Close intFileNum
Set obj = New Class1
obj.Contents = vData
Debug.Print "id = "; obj.id
Debug.Print "data = "; obj.data
End Sub