Results 1 to 9 of 9

Thread: Persistence of Classes

  1. #1

    Thread Starter
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Persistence of Classes

    I want to create a COM for my project which has one creatable class and more subclasses which are not creatable, all of them being public.

    Code:
    Class Parent:  Public/Creatable
       |
       |---- Class Child1     Public/Not Creatable
       |             |
       |             |---- Class Child2     Public/Not Creatable
    The hierarchy operates somewhat like the ListView control, where the ListItems collection exposes the ListItem objects. The Parent class will have a collection that stores Child1 class objects, and each Child1 class will have a collection that holds the Child2 objects.

    Since the Persistable property is available only for public creatable classes, I can use this property only for the Parent class. But I actually want to store the properties of the Child1 and Child2 classes.

    Any ideas?

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You can use a property bag to easily write the data to disk and persist it that way.

  3. #3

    Thread Starter
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    No, you didn't get the question or I was not clear enough.

    I know how I can persist a class, but I am facing a problem. The classes which I want to persist are PublicNotCreatable. Since a class has to be public and creatable in order to be persistable, I cannot directly make these classes persist.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Can't you just have the parent that is creatable Load/Save the Non Createable classes?

  5. #5

    Thread Starter
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    And how can I do that? Unless the child classes are persistable, I cannot use the PropertyBag object in them, can I?

    I have an idea: In my Parent class, I have a property which is a collection of Child1 objects:

    Parent.Child1Objects

    If I make the Parent class persistable, do you think it would allow me to save this property, and in turn the objects in the collection Child1Objects be saved?

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I've never actually used the Persistable property of a class before. Hardly ever have to persist a class unless it is saving the data to some form of media. BUT does the Persistable property actually do the work of persisting the object or just give you the extra Events like a control to have you do it yourself (i.e. InitProperties, Read/WriteProperties)?

    Also you can use a propertybag anywhere so if it turns out that is what you need to do then it'll work fine. You could do it pretty much just like a control using the events that the Public class gives you since its persistable.

  7. #7

    Thread Starter
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    In an ActiveX control, you can use the PropertyBag almost all the time. Not so in ordinary classes.

    Also I think you need the ReadProperties, WriteProperties methods and the PropertyChanged event to be available in your class, for which it has to be publicly creatable, which an ActiveX "control" is.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Although a PropertyBag is used a lot in controls it can be used anywhere, its just another object. I use it all the time to serialize objects. Also since the Parent class is persistable you can use its Read/Write events and the property bag to serialize and persist the items in the collections. You would do it pretty much just like an activeX control, only you may need to store the data somewhere between uses (i.e. a file, database, registry, etc...)

    Even without the class being persistable you can use the Initialize and Terminate events if you need to.

    If you need code for the property bag part then let me know but it works the same as when it is used in an activeX control only you want to store the PropertyBag.Contents somewhere. If you try it I bet it'll work.

  9. #9
    Junior Member
    Join Date
    Aug 2002
    Location
    Brazil
    Posts
    29
    I had a situation like that previously and solved making a loop inside the subclasses' objects and writing the their properties in the parent class PropertyBag.
    VB Code:
    1. Private Sub UserControl_WriteProperties(ByRef PBag As PropertyBag)
    2. Dim Index1 as Integer,Index2 as Integer
    3. With PBag
    4.         'First save the Parent class properties
    5.         .WriteProperty "Property1",Property1
    6.              .
    7.         .WriteProperty "Propertyn",Propertyn
    8.         'Then save the ChildClass1 collection object's number
    9.         .WriteProperty "ChildClass1Count",ChildClass1.Count
    10.         For Index1=1 To ChildClass1.Count
    11.                  'Save the properties of each Object in the ChildClass1 Collection
    12.                 .WriteProperty "ChildClass1[" & Index1 & "]Property1",ChildClass1(Index1).Property1
    13.                      .
    14.                 .WriteProperty "ChildClass1[" & Index1 & "]Propertyn",,ChildClass1(Index1).Propertyn
    15.                  'Then save the ChildClass2 collection object's number of the current object in ChildClass1 collection
    16.                 .WriteProperty "ChildClass1[" & Index1 & "].ChildClass2Count",ChildClass1(Index1).ChildClass2.Count
    17.                 For Index2=1 To ChildClass2.Count
    18.                          'Save the properties of each Object in the ChildClass2 Collection of the current object in ChildClass1 collection
    19.                         .WriteProperty "ChildClass1[" & Index1 & "]ChildClass2[" & Index2 & "]Property1",ChildClass2(Index1).ChildClass2(Index2).Property1
    20.                              .
    21.                         .WriteProperty "ChildClass1[" & Index1 & "]ChildClass2[" & Index2 & "]Propertyn",ChildClass1(Index1).ChildClass2(Index2).Propertyn
    22.                 Next Index2
    23.         Next Index1
    24. End With
    25. End Sub
    26.  
    27. Private Sub UserControl_ReadProperties(ByRef PBag As PropertyBag)
    28. Dim Index1 as Integer,Index2 as Integer
    29. Dim Max1 as Integer, Max2 as Integer
    30. Dim oCCO1 as ChildClassObject1,oCCO2 as ChildClassObject2
    31. With PBag
    32.         'First read the Parent class properties
    33.         Property1=.ReadProperty("Property1")
    34.                   .
    35.         Propertyn=.ReadProperty("Propertyn")
    36.         'Then get the ChildClass1 collection object's number into Max1 variable
    37.         Max1=.ReadProperty("ChildClass1Count")
    38.         'Clears the ChildClass1 properties
    39.         Set ChildClass1=Nothing
    40.         Set ChildClass1 = New ChildClass1
    41.         For Index1=1 To Max1
    42.                 'Create a object contained in ChildClass1 Collection
    43.                 Set oCCO1=New ChildClassObject1
    44.                 'Reads its properties
    45.                 oCCO1.Property1=.ReadProperty("ChildClass1[" & Index1 & "]Property1")
    46.                      .
    47.                 oCCO1.Propertyn=.ReadProperty("ChildClass1[" & Index1 & "]Propertyn")
    48.                 'Then get the ChildClass2 collection object's number for current object "oCCO1" into Max2 variable
    49.                 Max2=.ReadProperty("ChildClass1[" & Index1 & "].ChildClass2Count")
    50.                 'Clears the ChildClass2 collection inside oCCO1
    51.                 Set oCCO1.ChildClass2=Nothing
    52.                 Set oCCO1.ChildClass2=New ChildClass2
    53.                 For Index2=1 To Max2
    54.                         'Create a object contained in ChildClass2 Collection
    55.                         Set oCCO2= New ChildClassObject2
    56.                         'Reads its properties  
    57.                         oCCO2.Property1=.ReadProperty("ChildClass1[" & Index1 & "]ChildClass2[" & Index2 & "]Property1")
    58.                              .
    59.                         oCCO2.Propertyn=.ReadProperty("ChildClass1[" & Index1 & "]ChildClass2[" & Index2 & "]Propertyn")
    60.                         'Save the object in ChildClass2 inside oCCO1
    61.                         oCCO1.ChildClass2.AddObject oCCO2
    62.                 Next Index2
    63.                 'Save the object in ChildClass1
    64.                 ChildClass1.AddObject oCCO1
    65.         Next Index1
    66. End With
    67. End Sub

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