OptionStrictOnImports System.IOImports System.Runtime.SerializationImports System.Runtime.Serialization.Formatters.SoapPublicClass Form1Inherits System.Windows.Forms.Form
Private mFilename AsString = "PInfo.txt"
PrivateSub saveMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles saveMenuItem.Click
'Save the informationTryDim aBook AsNew Book
Dim informationList AsNew ArrayList
aBook.a = TextBox1.Text
aBook.b = TextBox2.Text
aBook.c = TextBox3.Text
aBook.d = TextBox4.Text
aBook.e = TextBox5.Text
aBook.f = TextBox6.Text
informationList.Add(aBook)
Dim infoFS AsNew FileStream(mFilename, FileMode.Append)
Dim infoSF AsNew SoapFormatter
infoSF.Serialize(infoFS, informationList)
infoFS.Close()
MessageBox.Show("Information Saved")
Call clearMenuItem_Click(sender, e)
Catch ex As Exception
MessageBox.Show("Could not save the file")
EndTryEndSubPrivateSub exitMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitMenuItem.Click
'Closes applicationMe.Close()
EndSubPrivateSub clearMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearMenuItem.Click
'Clear textboxes
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
TextBox6.Text = ""
TextBox1.Focus()
EndSubEndClass
I have been looking for info and am so lost something like i have to do dll, reference my assembly i do not know where to start?? Please any help
Hi,
I am assuming Book is some class of yours.
try following code for deserilization in the same/different project . PrivateSub openMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles openMenuItem.Click Try Dim aBook As New book Dim informationList As New ArrayList Dim infoFS As New FileStream(mFilename, FileMode.Open) Dim infoSF As New SoapFormatter 'we serialized arraylist, so we will get arraylist object back informationList = infoSF.Deserialize(infoFS) 'if it doesn't work try informationList = CType(infoSF.Deserialize(infoFS), ArrayList) infoFS.Close() 'arraylist contained only one item at index=0 and that item was an object of Book type aBook = CType(informationList(0), book) 'I am assuming book is some class of yours TextBox1.Text = aBook.a TextBox2.Text = aBook.b TextBox3.Text = aBook.c TextBox4.Text = aBook.d TextBox5.Text = aBook.e TextBox6.Text = aBook.f MessageBox.Show("Information retrieved") Catch ex As Exception MessageBox.Show("Could not open the file") End Try End Sub
PrivateSub Form1_Load(ByVal sender AsObject, ByVal e As System.EventArgs) HandlesMyBase.Load
Dim restoredlist AsNew ArrayList
Dim fs AsNew FileStream(mFilename, FileMode.Open)
Dim sf AsNew SoapFormatter
restoredlist = CType(sf.Deserialize(fs), ArrayList)
fs.Close()
Dim objx AsNew Book
ForEach objx In restoredlist
informationListBox.Items.Add(objx)
Next
EndSub
If i place this code in the first project works fine but once i put it in another project it does not, something like i have to reference the assembly but am lost on how to do that? By my understanding i can only reference a dll but i am working with 2003, so am quite lost as i will need to reference the class Book and the text file that is storing the information
Hi,
u have to have the same class in the other project also.
when we serialized arraylist , it had only one item namely a book object.
so what we will get back is ,an arraylist containing one item which will be of book type.
so
objx=ctype(restoredlist(0),book)
should give u that object.
Dim str AsNew string ForEach str In objx
informationListBox.Items.Add(str) Next should work.
Hi,
u have to have the same class in the other project also.
when we serialized arraylist , it had only one item namely a book object.
so what we will get back is ,an arraylist containing one item which will be of book type.
so
objx=ctype(restoredlist(0),book)
should give u that object.
Dim str AsNew string ForEach str In objx
informationListBox.Items.Add(str) Next should work.
Yeah i had included the Book object (add existing item) but got this error
Additional information: Could not find file "C:\Documents and Settings\owner\My Documents\Visual Studio Projects\Exercise1\bin\PInfo.txt".
So i put the PInfo.txt file in the bin folder and got this error
An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in system.runtime.serialization.formatters.soap.dll
Additional information: Parse Error, no assembly associated with Xml key a3:http://schemas.microsoft.com/clr/nsa...eyToken%3Dnull Book.
So i then tried your code and that did not work got the same error as above.
I even tried writing an override toString method in the book class and that did not help, the closest thing i have got to work is commas placed in the listbox.
Hi NSta,
I will try the code myself and tell u if it gets running or aprise u of errors.
meanwhile u can download this 101 solutions from microsoft.
serilization with soap and binary formatters is one of the projects .this thing is a treasure trove of working code. http://www.microsoft.com/downloads/d...displaylang=en
Last edited by safalmittal; May 4th, 2007 at 2:54 am.
Hi NSta,
I will try the code myself and tell u if it gets running or aprise u of errors.
meanwhile u can download this 101 solutions from microsoft.
serilization with soap and binary formatters is one of the projects .this thing is a treasure trove of working code. http://www.microsoft.com/downloads/d...displaylang=en
No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.