Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.IO
- Imports System.Collections
- Imports System.Runtime.Serialization.Formatters.Binary
- Imports System.Runtime.Serialization
- Module Module1
- Sub Main()
- While True
- Console.WriteLine("s=serialize, r=read:")
- Select Case Console.ReadLine()
- Case "s"
- Dim EmployeeList1 = New List(Of Employee)()
- ' create some objects adding them to a List
- EmployeeList1.Add(New Employee(1, "Elenor", "Scheller"))
- EmployeeList1.Add(New Employee(2, "Kaila", "Wilburn"))
- EmployeeList1.Add(New Employee(3, "Shea", "Wallen"))
- EmployeeList1.Add(New Employee(4, "Elliot", "Shaffer"))
- EmployeeList1.Add(New Employee(5, "Dominica", "Charney"))
- Try
- Using stream As New FileStream("data.bin", FileMode.Create)
- ' create BinaryFormatter
- Dim bin = New BinaryFormatter()
- ' serialize the collection (EmployeeList1) to file (stream)
- bin.Serialize(stream, EmployeeList1)
- End Using
- Catch ex As Exception
- End Try
- Case "r"
- Try
- Using stream As New FileStream("data.bin", FileMode.Open)
- ' create BinaryFormatter
- Dim bin = New BinaryFormatter()
- ' deserialize the collection (Employee) from file (stream)
- Dim EmployeeList2 = DirectCast(bin.Deserialize(stream), List(Of Employee))
- ' display the results
- For Each Employee In EmployeeList2
- Console.WriteLine("{0}, {1}, {2}",
- Employee.id,
- Employee.fName,
- Employee.sName)
- Next
- End Using
- Catch ex As Exception
- End Try
- End Select
- End While
- End Sub
- ' The object we are going to Serializing/De-serializing
- <Serializable()>
- Class Employee
- Property id As Integer
- Property fName As String
- Property sName As String
- Public Sub New(i As Integer, f As String, s As String)
- id = i
- fName = f
- sName = s
- End Sub
- End Class
- End Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement