|
-
Jan 29th, 2008, 12:24 PM
#1
Thread Starter
Hyperactive Member
[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.
-
Jan 29th, 2008, 12:27 PM
#2
Re: [2008] Saving a class
Serialization would be the solution. XML or Binary serialization, whichever you prefer.
Read this.
-
Jan 29th, 2008, 01:04 PM
#3
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
 
-
Jan 29th, 2008, 01:17 PM
#4
Thread Starter
Hyperactive Member
Re: [2008] Saving a class
Thanks for the replys. Ill have a look at them.
-
Jan 29th, 2008, 02:30 PM
#5
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
 
-
Jan 29th, 2008, 04:18 PM
#6
Thread Starter
Hyperactive Member
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.
-
Jan 29th, 2008, 04:21 PM
#7
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.
-
Jan 29th, 2008, 04:53 PM
#8
Thread Starter
Hyperactive Member
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.
-
Jan 29th, 2008, 04:59 PM
#9
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.
-
Jan 29th, 2008, 05:00 PM
#10
Thread Starter
Hyperactive Member
Re: [2008] Saving a class
Right, i understand.
Thank you for your help.
-
Jan 29th, 2008, 06:36 PM
#11
Thread Starter
Hyperactive Member
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.
-
Jan 29th, 2008, 06:43 PM
#12
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:
Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim fs As New IO.FileStream("C:\myFileName.bin", IO.FileMode.Open)
L = CType(formatter.Deserialize(fs), Global.System.Collections.Generic.List(Of Person))
fs.Close()
-
Jan 29th, 2008, 06:54 PM
#13
Thread Starter
Hyperactive Member
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
-
Jan 29th, 2008, 06:59 PM
#14
Thread Starter
Hyperactive Member
Re: [2008] Saving a class
Sorry, i needed to declare a new person inside the loop.
Thank you.
-
Jan 29th, 2008, 07:02 PM
#15
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:
Dim P As Person
For i As Integer = 1 To 5
P = New Person
P.Forename = "Firstname" & i.ToString
P.Surname = "Surname" & i.ToString
L.Add(P)
Next
Edit: Oh should've refreshed before replying
-
Jan 29th, 2008, 07:08 PM
#16
Thread Starter
Hyperactive Member
Re: [2008] Saving a class
You have been a great help, now to saving and retrieving list of person using XmlSerializer
Thanks Atheist
-
Jan 29th, 2008, 07:38 PM
#17
Re: [2008] Saving a class
Just out of curiosity, why are you doing both?
My usual boring signature: Nothing
 
-
Jan 29th, 2008, 07:46 PM
#18
Thread Starter
Hyperactive Member
Re: [2008] Saving a class
 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.
-
Jan 29th, 2008, 07:51 PM
#19
Re: [2008] Saving a class
 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.
-
Jan 29th, 2008, 08:05 PM
#20
Thread Starter
Hyperactive Member
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.
-
Jan 29th, 2008, 08:07 PM
#21
Re: [2008] Saving a class
Take a look at what youre passing to the XMLSerializers constructor:
VB.Net Code:
Dim mySerializer As New Xml.Serialization.XmlSerializer(GetType(Person))
You're passing GetType(Person), but you're trying to serialize a List(Of Person).
-
Jan 29th, 2008, 08:15 PM
#22
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|