Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization.SerializationInfo
<Serializable()> _
Class Class1
Implements ISerializable
Public Event Result(ByVal iRet As Integer)
Private mN1 As Integer
Private mN2 As Integer
Private mNumList() As Class2
Public Property NumList() As Class2()
Get
Return mNumList
End Get
Set(ByVal value() As Class2)
ReDim mNumList(value.Length - 1)
mNumList = value
End Set
End Property
Public Property N1() As Integer
Get
Return mN1
End Get
Set(ByVal value As Integer)
mN1 = value
End Set
End Property
Public Property N2() As Integer
Get
Return mN2
End Get
Set(ByVal value As Integer)
mN2 = value
End Set
End Property
Public Function AddNumber() As Boolean
RaiseEvent Result(mN1 + mN2)
Return True
End Function
Public Function AddNumber(ByVal N1 As Integer, ByVal N2 As Integer) As Boolean
mN1 = N1
mN2 = N2
RaiseEvent Result(N1 + N2)
Return True
End Function
Public x As Int32
Private Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
Dim num() As Class2
x = info.GetInt32("x")
mN1 = info.GetInt32("N1")
mN2 = info.GetInt32("N2")
num = CType(info.GetValue("NumList", num.GetType()), Class2())
NumList = num
End Sub
Public Sub New()
x = 100
End Sub
Sub GetObjectData(ByVal info As SerializationInfo, ByVal context As StreamingContext) Implements ISerializable.GetObjectData
info.AddValue("x", x)
info.AddValue("N1", mN1)
info.AddValue("N2", mN2)
info.AddValue("NumList", mNumList, mNumList.GetType())
End Sub
End Class
=====================================================
<Serializable()> _
Public Class Class2
Private mGrade As Integer
Public Property Grade() As Integer
Get
Return mGrade
End Get
Set(ByVal value As Integer)
mGrade = value
End Set
End Property
End Class
======================================
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization.SerializationInfo
Imports System.Xml.Serialization
Public Class Form1
Dim WithEvents dwe As New Class1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim fs As New System.IO.FileStream("c:\temp\data1.bin", IO.FileMode.OpenOrCreate)
Dim bf As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
bf.Serialize(fs, dwe)
fs.Close()
Me.Text = dwe.NumList.Length - 1
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim fs As New System.IO.FileStream("c:\temp\data1.bin", IO.FileMode.Open)
Dim bf As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
dwe = CType(bf.Deserialize(fs), Class1)
txtN1.Text = dwe.N1
txtN2.Text = dwe.N2
Me.Text = dwe.NumList.Length - 1
fs.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
dwe.AddNumber()
txtN1.Text = dwe.N1
txtN2.Text = dwe.N2
End Sub
Private Sub dwe_Result(ByVal iRet As Integer) Handles dwe.Result
MessageBox.Show("Result Is " & iRet)
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
dwe.AddNumber(5, 10)
txtN1.Text = dwe.N1
txtN2.Text = dwe.N2
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Y() As Class2
Dim I As Integer
ReDim Y(9)
For I = 0 To 9
Y(I) = New Class2
Y(I).Grade = I
Next I
dwe.NumList = Y
End Sub
End Class