okay i have a simple program that has a class of objects some textboxes etc. What i do is enter information in and it saves an object to a text file.

Now i open a new project and want to use the text file that was previously created and read the objects into the new project, am so lost any hlep??

Thanks

r u using textwriter or serialize to save to text file. If u could provide some code , it would be better.

r u using textwriter or serialize to save to text file. If u could provide some code , it would be better.

OptionStrictOn
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Soap
 
PublicClass Form1
Inherits System.Windows.Forms.Form
Private mFilename As String = "PInfo.txt"
 
Private Sub saveMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles saveMenuItem.Click
'Save the information
Try
Dim aBook As New Book
Dim informationList As New 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 As New FileStream(mFilename, FileMode.Append)
Dim infoSF As New 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")
End Try
End Sub
Private Sub exitMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitMenuItem.Click
'Closes application
Me.Close()
End Sub
Private Sub 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()
End Sub
EndClass

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

OptionStrictOn
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Soap
 
PublicClass Form1
Inherits System.Windows.Forms.Form
Private mFilename As String = "PInfo.txt"
 
Private Sub saveMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles saveMenuItem.Click
'Save the information
Try
Dim aBook As New Book
Dim informationList As New 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 As New FileStream(mFilename, FileMode.Append)
Dim infoSF As New 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")
End Try
End Sub
Private Sub exitMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitMenuItem.Click
'Closes application
Me.Close()
End Sub
Private Sub 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()
End Sub
EndClass

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

Hi,
I am assuming Book is some class of yours.
try following code for deserilization in the same/different project .

Yeah i had already set that up in another project which is as follows

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim restoredlist As New ArrayList
Dim fs As New FileStream(mFilename, FileMode.Open)
Dim sf As New SoapFormatter
restoredlist = CType(sf.Deserialize(fs), ArrayList)
fs.Close()
Dim objx As New Book
For Each objx In restoredlist
informationListBox.Items.Add(objx)
Next
 
End Sub

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 As New string
For Each 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 As New string
For Each 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/nsassem/Exercise1Exercise0.1%2C%20Version%3D1.0.4678.271641%2C%20Culture%3Dneutral%2C%20PublicKeyToken%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.

why dont you create a class and build it as a DLL then add it up as a component in your other project.. Just a suggestion

Is it possible to create a dll in vb 2003 i cant seem to find where to when i start a new project or try and add something.

I have been looking around and found this post in another found this is this right and how would i do it. Thanks.

A type is considered to be part of the assembly it is defined in. One
solution is to add a reference to App1 to the project that created App2 and
deploy App1 along with App2. Another solution is to redefine the object
definition in App2, and then implement a serialization binder to map the
type as defined in App1 to a type defined in App2.

To do this implement a serialization binder. During deserialization this
gives you a chance to
tell the binary formatter which System.[U]Type[/U] to use for a given typename. You
need to define a class that derives from SeriazationBinder and in that class
override the BindToType method. When you create the [U]BinaryFormatter[/U] object
set its Binder property to an instance of your [U]SerializationBinder[/U] class. As
the binary formatter deserializes if it hits a typename for which it does
not already have a System.[U]Type[/U] defined for it, it asks the binder for that
System.[U]Type[/U]. This gives you a chance to treat a type as defined in one
assembly to
a type as defined in some other assembly. It is up to you to make sure the
types really
are compatible and convertible.
This article has been dead for over six months. Start a new discussion instead.