Results 1 to 4 of 4

Thread: How to save this object to file

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    How to save this object to file

    Im my endeavour to create an object framework for a project, I have come to the conclusion that I need to add a "savefile" method to my CDocumentCollection class

    This is a simple class.. it inherits collectionbase and contains an array with simple CDocument objects.

    I want to do something like this

    VB Code:
    1. dim mycollection as new CDocumentCollection
    2.  
    3. mycollection.add(new CDocument("Doc1"))
    4. mycollection.add(new CDocument("Doc3"))
    5. mycollection.add(new CDocument("Doc14"))
    6. mycollection.add(new CDocument("Doc156"))
    7.  
    8. mycollection.SaveToFile("c:\myfile.gps")

    Is there an easy way to save the object hieararchy to binary file and an easy way to load it the same way? Im sure there are many programs in vb.net /C# that perform this simple thing... I just want to learn the "right" way to do it...

    VB Code:
    1. dim mycollection as new CDocumentCollection
    2. mycollection = mycollection.LoadFromFile("c:\myfile.gps")


    I have very little experience in these things, are there any patterns or rules to follow to make this a good design? I know a little about serialization... I have read it is a good way to save/load binary config files... can this help me? How do I proceed?

    kind regards
    Henrik

  2. #2
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    How about Serializing the object?

    http://www.codeproject.com/vb/net/es...Collection.asp
    Tips:
    • Google is your friend! Search before posting!
    • Name your thread appropriately... "I Need Help" doesn't cut it!
    • Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
    • Allways Include the Name and Line of the Exception (if one is occuring!)
    • If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)


    If you think I was helpful, rate my post
    IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602
    Great idea... however I get this error when trying to do just that

    You must implement a default accessor on EasyPrinting.EasyPrinting.CDocumentCollection because it inherits from ICollection.


    here is my class

    VB Code:
    1. public Class CDocumentCollection : Inherits CollectionBase
    2.  
    3.         Public Sub Add(ByVal item As CDocument)
    4.             Me.List.Add(item)
    5.         End Sub
    6.  
    7.         Public ReadOnly Property Item(ByVal index As Integer) As CDocument
    8.             Get
    9.                 Return DirectCast(Me.List.Item(index), CDocument)
    10.             End Get
    11.  
    12.         End Property
    13.  
    14.         Public WriteOnly Property Item(ByVal index As Integer, ByVal Document As CDocument) As CDocument
    15.             Set(ByVal Value As CDocument)
    16.                 Me.List.Item(index) = Document
    17.             End Set
    18.         End Property
    19.  
    20.     End Class


    Serializing is one of the areas of .net Im not so familiar with

    kind regards
    Henrik

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Make sure that class has a default constructor (one with no parameters).

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