Results 1 to 3 of 3

Thread: How do I write recursive structure to file? HELP

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Oct 2008
    Posts
    1,238

    How do I write recursive structure to file? HELP

    I've created a class module with name clsABCD and it contains the below code.
    Code:
    public Data1 as string
    public Data2 as string
    public Data3 as string
    public Data4 as string
    public NodeA as clsABCD
    public NodeB as clsABCD
    public NodeC as clsABCD
    public NodeD as clsABCD
    In a command button on my form I've placed this code.
    Code:
    dim test1 as new clsABCD
    dim test2 as new clsABCD
    dim test3 as new clsABCD
    dim test4 as new clsABCD
    dim test5 as new clsABCD
    
    set test1.nodea=test2
    set test2.nodeb=test3
    set test3.nodec=test4
    set test4.noded=test5
    
    test5.data1="This"
    test5.data2="is"
    test5.data3="a"
    test5.data4="test."
    
    print test1.nodea.nodeb.nodec.noded.data1
    print test1.nodea.nodeb.nodec.noded.data2
    print test1.nodea.nodeb.nodec.noded.data3
    print test1.nodea.nodeb.nodec.noded.data4
    This code works and displays on my form:
    This
    is
    a
    test.
    This proves that "recursiveness" is something that VB6 can handle (I had my doubts originally when I found there's no way to recursively link variables of a user-defined type).

    But writing the complete recursive structure (all nodes and the data they contain) to a file is another problem altogether!

    The below code doesn't work.
    Code:
    Open "c:\temp\test.dat" For Binary As #1
    Put #1, 1, test1
    Close #1
    It gives the error:
    Can't Get or Put an object reference or variable or a user-defined type containing an object reference.

    I tried this alternative, but it didn't work either.
    Code:
    Dim propbag As New PropertyBag
    propbag.WriteProperty "test", test1
    Open "c:\temp\test.dat" For Binary As #1
    Put #1, 1, propbag.Contents
    Close #1
    At the command propbag.WriteProperty it gives the error:
    Illegal Parameter. Can't write object because it does not support persistence.

    Same thing happened when I tried using a Form instead of a Class Module.

    It seems that VB6's class modules (and forms) lack something called "persistence" (whatever that is, I got no clue), and as a result they are UNABLE to be saved to a file. And it's not just because of the recursive nature of what's in the class (or form). ALL forms and class modules in VB6 give this error, under ALL conditions.
    And unfortunately it seems class modules and forms are the ONLY only objects that can support recursive structures.
    Is there ANY WAY AT ALL around this? Or is saving a recursive structure of any kind from from VB6 a 100% IMPOSSIBLE task?

  2. #2
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: How do I write recursive structure to file? HELP

    See Persisting a Component's Data.

    The manual is just full of information.

    Setting Up Class Persistence

    In order to be persistable, a class must meet two conditions: it must be public and creatable. If you think about, this makes sense – after all, persistence wouldn't be useful in a private component. If a class meets both conditions, the Persistable property appears in the Properties window.

    By default, the Persistable property is set to 0 (NotPersistable). By changing this value to 1 (Persistable), three new events are added to the class: ReadProperties, WriteProperties, and InitProperties. As you might guess, these events are use to read, write, and initialize the class's properties.
    Note that public here means published, i.e. you must define such classes in an ActiveX DLL, OCX, or EXE. Otherwise the persistence mechanism has nothing to sink its teeth into.


    Alternatively you can roll your own, whether you use PropertyBags or a custom persistence format such as XML.

  3. #3
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: How do I write recursive structure to file? HELP

    Quote Originally Posted by Ben321 View Post

    I tried this alternative, but it didn't work either.

    Code:
    Dim propbag As New PropertyBag
    
    propbag.WriteProperty "test", test1
    
    Open "c:\temp\test.dat" For Binary As #1
    
    Put #1, 1, propbag.Contents
    
    Close #1



    Works for me


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

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