Results 1 to 8 of 8

Thread: good / evil [good wins :) - Resolved]

  1. #1

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    good / evil [good wins :) - Resolved]

    Is it possible or even good or bad practice to code a class so that it can serialize itself?

    I have a class that currently only serializes one of its data members (an array). But how about this...

    VB Code:
    1. <Serializable()> Public Class X
    2.     Shared Function LoadNewObject(ByVal path As String) As X
    3.  
    4.         Dim binser As BinaryFormatter = New BinaryFormatter
    5.         Dim input As Stream = New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None)
    6.  
    7.         Dim temp As X
    8.         temp = binser.Deserialize(input)
    9.         input.close()
    10.  
    11.         Return temp
    12.  
    13.     End Function
    14.  
    15.     Public Sub SaveSelf(ByVal path As String)
    16.  
    17.         Dim binser As BinaryFormatter = New BinaryFormatter
    18.         Dim output As Stream = New FileStream(Path, FileMode.Create, FileAccess.Write, FileShare.None)
    19.  
    20.         binser.Serialize(output, Me)
    21.  
    22.         output.Close()
    23.  
    24.     End Sub
    25. End Class

    Your thoughts please.
    Last edited by wossname; May 4th, 2004 at 09:06 AM.
    I don't live here any more.

  2. #2
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    I don't see anything wrong with it. In fact, I don't even see any other way of doing it.
    I do that all the time, when making configuration files and it works like a charm.

    BUT, I'm self-thought, so I have no idea what a pro migth have to say, so my humble opinion might not be worth the time it took to write this...

    Edit: BTW, I'm not so clever that I could have thought of this myself , so I'm pretty sure I once saw a sample on MSDN on how to do it. So if my memory serves, MS encourages this way of doing it.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  3. #3
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hello Again.

    I just found this link, so it appears my memory aren't a complete wasteland (yippie )

    http://msdn.microsoft.com/library/de...appsettnet.asp

    This is a few samples from MSDN where MS shows different ways of doing it.
    Scroll down a little, and you should see a header called "Creating a Custom Application Configuration Class".
    That's a sample where they serialize the entire class so I guess it's ok to do it.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  4. #4

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    Would you mind casting your eye over this...

    VB Code:
    1. Shared Function FromFile(ByVal path As String) As BoundaryDataStorage
    2.  
    3.         If File.Exists(path) Then
    4.  
    5.             'deserializes the array of boundaries from a binary file
    6.             Dim binser As BinaryFormatter
    7.             Dim input As Stream
    8.             Dim temp As BoundaryDataStorage
    9.  
    10.             Try
    11.                 'load the file
    12.                 binser = New BinaryFormatter               
    13.                 input = New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None)             
    14.                 temp = binser.Deserialize(input)    [b]'<<<<<<<<<<<<<<<<<<<[/b]
    15.                 input.Close()
    16.  
    17.                 temp.Path = path
    18.                 Return temp
    19.             Catch ex As Exception
    20.                 If Not IsNothing(input) Then input.Close()
    21.                 temp = Nothing
    22.                 Return Nothing
    23.             End Try
    24.  
    25.         End If
    26.  
    27.     End Function
    28.  
    29.     Public Function SaveBoundaryDataBinary(ByVal path As String) As Boolean
    30.         Dim binser As BinaryFormatter
    31.         Dim output As Stream
    32.  
    33.         itsPath = path.Trim
    34.  
    35.         Try
    36.             binser = New BinaryFormatter
    37.             output = New FileStream(itsPath, FileMode.Create, FileAccess.Write, FileShare.None)
    38.  
    39.             Dirty = False           'set flag BEFORE saving because the flag is saved too!
    40.             binser.Serialize(output, Me)    [b]'<<<<<<<<<<<<<<<<<<<[/b]
    41.  
    42.             output.Close()
    43.  
    44.             Return True          'success!
    45.         Catch ex As Exception
    46.             If Not IsNothing(output) Then output.Close()
    47.             Return False
    48.         End Try
    49.  
    50.     End Function

    Does this seem like a reasonable approach?
    Last edited by wossname; May 4th, 2004 at 07:49 AM.
    I don't live here any more.

  5. #5
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    That exactly how I would do it.

    You could set the "NonSerialized" attribute for the "Dirty" variable to prevent it from being serialized.
    VB Code:
    1. <NonSerialized()> Private Dirty As Boolean

    That way, you can have lot's of internal variables, and only have it serialize the ones that are neccesary.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  6. #6

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    OK...I have declared the flag thus:

    <NonSerialized()>Private itsDirty As Boolean = False

    when I have run FromFile(...) and I have my new object, will itsDirty contain false because I have set it as the default value?

    I realise I can't use the constructor to set it when deserializing.
    I don't live here any more.

  7. #7
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    Hi.

    Yes. All variables and properties that are not included in the file (either because they are not serialized or because they have been added after the creation of a file) will use their defaultvalue.
    I wish I could think of something witty to put in my sig...

    ...Currently using VS2013...

  8. #8

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682
    Cool. My programs is a bit more streamlined now.

    Cheers for the help.
    I don't live here any more.

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