Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1529

Serializing array of objects in PropertyBag

$
0
0
There is an interesting example of implementing a custom PropertyBag from Edanmo.
Unfortunately, it cannot save arrays of objects. So I modified it a little. You can now save arrays of objects.

Code:

Option Explicit

Dim PropBag As PropBag.PropertyBag
Dim PropBag2 As PropBag.PropertyBag

Sub Main()
'--------------------------------------------------------------------------------
'Saving an array of objects in PropertyBag

  Dim array_obj(2) As New Class
  Dim content() As Byte


  Set PropBag = New PropBag.PropertyBag
 
  array_obj(0).Text = "HELLO"
  array_obj(0).Number = 11111
 
  array_obj(1).Text = "WORLD"
  array_obj(1).Number = 22222
 
  array_obj(2).Text = "EXAMPLE"
  array_obj(2).Number = 33333
 
 
  PropBag.WriteProperty "Array_of_objects", array_obj()
  content() = PropBag.Contents
   
  Open App.Path & "\pb.dat" For Binary As #1
    Put #1, , content()
  Close #1
 
  '--------------------------------------------------------------------------------
  'Reading an array of objects from PropertyBag
 
  Dim array_obj2() As New Class
  Dim content2() As Byte
 
  Set PropBag2 = New PropBag.PropertyBag
 
  Open App.Path & "\pb.dat" For Binary As #2
    ReDim content2(LOF(2))
    Get #2, , content2()
  Close #2
 
  PropBag2.Contents = content2()
  array_obj2() = PropBag2.ReadProperty("Array_of_objects")
 
  Dim Item As Variant
 
    For Each Item In array_obj2()
      Debug.Print Item.Text & " " & Item.Number
  Next Item

  Stop
 
End Sub

Attached Files

Viewing all articles
Browse latest Browse all 1529

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>