|
-
May 4th, 2004, 06:14 AM
#1
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:
<Serializable()> Public Class X
Shared Function LoadNewObject(ByVal path As String) As X
Dim binser As BinaryFormatter = New BinaryFormatter
Dim input As Stream = New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None)
Dim temp As X
temp = binser.Deserialize(input)
input.close()
Return temp
End Function
Public Sub SaveSelf(ByVal path As String)
Dim binser As BinaryFormatter = New BinaryFormatter
Dim output As Stream = New FileStream(Path, FileMode.Create, FileAccess.Write, FileShare.None)
binser.Serialize(output, Me)
output.Close()
End Sub
End Class
Your thoughts please.
Last edited by wossname; May 4th, 2004 at 09:06 AM.
I don't live here any more.
-
May 4th, 2004, 07:12 AM
#2
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...
-
May 4th, 2004, 07:26 AM
#3
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...
-
May 4th, 2004, 07:46 AM
#4
Would you mind casting your eye over this...
VB Code:
Shared Function FromFile(ByVal path As String) As BoundaryDataStorage
If File.Exists(path) Then
'deserializes the array of boundaries from a binary file
Dim binser As BinaryFormatter
Dim input As Stream
Dim temp As BoundaryDataStorage
Try
'load the file
binser = New BinaryFormatter
input = New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None)
temp = binser.Deserialize(input) [b]'<<<<<<<<<<<<<<<<<<<[/b]
input.Close()
temp.Path = path
Return temp
Catch ex As Exception
If Not IsNothing(input) Then input.Close()
temp = Nothing
Return Nothing
End Try
End If
End Function
Public Function SaveBoundaryDataBinary(ByVal path As String) As Boolean
Dim binser As BinaryFormatter
Dim output As Stream
itsPath = path.Trim
Try
binser = New BinaryFormatter
output = New FileStream(itsPath, FileMode.Create, FileAccess.Write, FileShare.None)
Dirty = False 'set flag BEFORE saving because the flag is saved too!
binser.Serialize(output, Me) [b]'<<<<<<<<<<<<<<<<<<<[/b]
output.Close()
Return True 'success!
Catch ex As Exception
If Not IsNothing(output) Then output.Close()
Return False
End Try
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.
-
May 4th, 2004, 07:58 AM
#5
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:
<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...
-
May 4th, 2004, 08:13 AM
#6
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.
-
May 4th, 2004, 08:33 AM
#7
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...
-
May 4th, 2004, 09:05 AM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|