Results 1 to 22 of 22

Thread: [2008] Saving a class

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    [2008] Saving a class

    I f i had a simple class with just a few propertys, What would be the best approach to saving that (List Of (class_name)) to a textfile and retrieving it aswell ?

    Thanks.
    Last edited by user name; Jan 29th, 2008 at 06:27 PM.

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] Saving a class

    Serialization would be the solution. XML or Binary serialization, whichever you prefer.
    Read this.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [2008] Saving a class

    I would suggest binary serialization in that case, since it is very easy to implement and provides some safety from casual editing of the file. If you actually want to be able to edit the file by hand, then XML is the way to go.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Re: [2008] Saving a class

    Thanks for the replys. Ill have a look at them.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [2008] Saving a class

    I think I described how to do binary serialization within the last month or two, so you could search this forum on that info. However, once you have the filename, binary serialization takes two lines, and one of them may not be entirely necessary. It's really that simple.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Re: [2008] Saving a class

    Ok, i decided to do a test with both just for future reference. With the binary serialization i tried this
    Code:
    Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
    
            Dim myWriter As New IO.FileStream("C:\myFileName.bin", IO.FileMode.Create)
                                                   
              'P is an instance of my class
            formatter.Serialize(myWriter, P)
    
            myWriter.Close()
    It gave me an error about not be serializable so i set the attribute in the class but i have two questions.

    1. Is that code ok for doing binary serialization ?

    2. By setting the <Serializable()> attribute, does it do only that to the class, by that i mean, if it was set by default, you wouldnt change any code at all when creating classes ?

    Thanks.

  7. #7
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] Saving a class

    1. Yes that code is perfect

    2. How do you mean? You just need to make sure everything inside the class is serializable. All classes and structures should be marked with the serializable attribute.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Re: [2008] Saving a class

    You just need to make sure everything inside the class is serializable
    What do you mean by that ?, i just declared it at the top of the class.
    Code:
    <Serializable()> Public Class Person
    
    'all propertys
    
    End Class
    Is there more i need to do ?

    Plus, while im typing the reply, i might aswell ask. Is there much difference if i needed to save a (List Of Person) ?

    Thank you for your replys.

  9. #9
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] Saving a class

    What I ment was, if your class contains a structure for example, it too needs to be marked as serializable.
    And you need to make sure that every class you're using inside the class is serializable. If they arent, you'll notice

    The List(Of T) is serializable so there wont be any problem serializing a List(Of Person) as long as Person is serializable.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Re: [2008] Saving a class

    Right, i understand.

    Thank you for your help.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Re: [2008] Saving a class

    I have been trying to add a list of person and retrieve it back but i cant get my head around it.

    This only seems to add the last item of the list.
    Code:
    Dim L As New List(Of Person)
            Dim P As New Person
    
            For i As Integer = 1 To 5
    
                P.Forename = "Firstname" & i.ToString
                P.Surname = "Surname" & i.ToString
                L.Add(P)
    
            Next
    
            Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
    
            Dim myWriter As New IO.FileStream("C:\myFileName.bin", IO.FileMode.Create)
    
            formatter.Serialize(myWriter, L)
    
            myWriter.Close()
    How do we read the list back aswell ?

    Thank you.

  12. #12
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] Saving a class

    You use the Deserialize method. Its return type is Object so you need to cast it to List(Of Person).

    VB.Net Code:
    1. Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
    2.  
    3.         Dim fs As New IO.FileStream("C:\myFileName.bin", IO.FileMode.Open)
    4.  
    5.         L = CType(formatter.Deserialize(fs), Global.System.Collections.Generic.List(Of Person))
    6.         fs.Close()
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Re: [2008] Saving a class

    That looks perfect for retrieving but how about the code i have for saving, the retrieving part shows that i am saving 5 items with L.Count.ToString but every item from L(0).Forename to L(4).Forename shows the same thing(the last item added to the list.

    Is it something to do with this line

    formatter.Serialize(myWriter, L)

    Thank you for your patience Atheist

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Re: [2008] Saving a class

    Sorry, i needed to declare a new person inside the loop.

    Thank you.

  15. #15
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] Saving a class

    It might be your loop, you're setting the Forename and Surname property of the same Person instance everytime. Change the loop to something like this:

    vb Code:
    1. Dim P As Person
    2.  
    3.         For i As Integer = 1 To 5
    4.             P = New Person
    5.             P.Forename = "Firstname" & i.ToString
    6.             P.Surname = "Surname" & i.ToString
    7.             L.Add(P)
    8.  
    9.         Next

    Edit: Oh should've refreshed before replying
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Re: [2008] Saving a class

    You have been a great help, now to saving and retrieving list of person using XmlSerializer

    Thanks Atheist

  17. #17
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [2008] Saving a class

    Just out of curiosity, why are you doing both?
    My usual boring signature: Nothing

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Re: [2008] Saving a class

    Quote Originally Posted by Shaggy Hiker
    If you actually want to be able to edit the file by hand, then XML is the way to go
    When i read that i decided to create a test project for future use so i wanted to learn both methods.

    I have just read that you cant use an arraylist or a listof type for xml, do you have to convert to an array ?

    Thank you.

  19. #19
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] Saving a class

    Quote Originally Posted by user name
    When i read that i decided to create a test project for future use so i wanted to learn both methods.

    I have just read that you cant use an arraylist or a listof type for xml, do you have to convert to an array ?

    Thank you.
    Where did you read that? You can serialize a List(Of T) using XML serialization, though I'm not so sure about the ArrayList..But dont worry about that, because you shouldnt be using the ArrayList in any version of .Net higher than 1.1.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Re: [2008] Saving a class

    I tried the same as the binary method but using xml but i get an error.
    There was an error generating the XML document.
    Code:
    Dim L As New List(Of Person)
    
            For i As Integer = 1 To 5
    
                Dim P As New Person
                P.Forename = "forename" & i.ToString
                P.Surname = "surname" & i.ToString
                L.Add(P)
    
            Next
            
            Dim mySerializer As New Xml.Serialization.XmlSerializer(GetType(Person))
    
            Dim myWriter As New IO.FileStream("C:\myFileName.xml", IO.FileMode.Create)
    
            mySerializer.Serialize(myWriter, L)
    
            myWriter.Close()
    ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/fxref_system.xml/html/92d10c1b-a2dd-1911-fc1c-b790b994b5af.htm

    Not sure if i am reading that correctly but it does say under Note.

    The XmlSerializer cannot serialize the following: arrays of ArrayList and arrays of List<(Of <(T>)>).

    Thank you.

  21. #21
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2008] Saving a class

    Take a look at what youre passing to the XMLSerializers constructor:
    VB.Net Code:
    1. Dim mySerializer As New Xml.Serialization.XmlSerializer(GetType(Person))

    You're passing GetType(Person), but you're trying to serialize a List(Of Person).
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  22. #22

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    499

    Re: [2008] Saving a class

    Oh Man, I deserve that through being lazy, copying code and just changing a couple of things from the single save and obviously missing that.

    Good find Atheist, you are the man

    Thank you very much.

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