Results 1 to 1 of 1

Thread: Serializing array of objects in PropertyBag

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2015
    Location
    Netherlands
    Posts
    24

    Serializing array of objects in PropertyBag

    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 Attached Files

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width