Иллюстрированный самоучитель по Visual Basic .NET

Практический пример: динамический список с поддержкой сериализации

Ниже приведена остальная часть кода этого примера.

Sub SerializeToSoap(ByVal myEmployees As ArrayList._
ByVal fName As String)
Dim fStream As FileStream
Dim mySoapFormatter As New Formatters.Soap.SoapFormatter()
Try
fStream = New FileStreamtfName. FileMode.Create.FileAccess.Write)
mySoapFormatter.Serialize(fStream .myEmployees)
Catch
Throw Finally
If Not (fStream Is Nothing) Then fStream.Close()
End Try
End Sub
Function DeSerializeFromSoap(ByVal fName As String) As ArrayList
Dim fStream As New FileStream(fName. Fi1eMode.Open. FileAccess.Read)
Dim mySoapFormatter As New Formatters.Soap.SoapFormatter()
Try
fStream = New FileStream(fName, FileMode.Open. FileAccess.Read)
Return
CType(mySoapFormatter.Deserialize(fStream), ArrayList)
Catch
Throw Finally
If Not (fStream Is Nothing) Then fStream.Close()
End Try
End Function
End Module
<Serializable() >Public Class Employee
Private m_Name As String
Private m_Salary As Decimal
Private Const LIMIT As Decimal = 0.1D
Private Shared m_EmployeeId As Integer = 1000
Private m_myID As Integer
Public Sub New(ByVal sName As String. ByVal curSalary As Decimal)
m_Name = sName
m_Salary = curSalary
m_myID = m_EmployeeId
m_EmployeeId = m_EmployeeId + 1
End Sub
Readonly Property TheIDO As Integer
Get
Return mjnyID
End Get
End Property Readonly Property TheName()As String
Get
Return m_Name
End Get
End Property Readonly Property Salary()As Decimal
Get
Return MyClass.m_Salary
End Get
End Property Public Overridable Overloads
Sub RaiseSalary(ByVal Percent As Decimal)
If Percent > LIMIT Then
' Недопустимая операция
Console.WriteLineC'MUST HAVE PASSWORD " & _
"TO RAISE SALARY MORE THAN LIMIT!!!!") Else
m_Salary = (1 + Percent) * m_Salary
End If
End Sub
Public Overridable Overloads
Sub RaiseSa1ary(ByVal Percent As Decimal._
ByVal Password As String) If Password = "special" Then
m_Salary = (1 + Percent) * m_Salary
End If
End Sub
End Class
<Serializable() >Public Class Manager
Inherits Employee
Private m_Sec As Secretary
Private m_Salary As Decimal
Public Sub New(ByVal sName As String,_
ByVal curSalary As Decimal)
MyBase.New(sName .curSalary)
End Sub
Public Sub New(ByVal sName As String.ByVal curSalary As Decimal.
ByVal mySec As Secretary)
MyBase.New(sName.curSalary)
m_Sec = mySec
End Sub
Property MySecretary()As Secretary Get
Return m_Sec End Get Set(ByVal Value As Secretary)
m_Sec = Value
End Set
End Property
Public Overloads Overrides
Sub RaiseSalary(ByVal percent As Decimal)
MyBase.RaiseSalary(2 * percent, "special")
End Sub
End Class
<Serializable()>
Public Class Secretary Inherits Employee
Private m_Boss As Manager
Public Sub New(ByVal sName As String. ByVal curSalary As Decimal,
ByVal myBoss As Manager) MyBase.New(sName, curSalary)
m_Boss = myBoss
End Sub
Property MyManager() As Manager Get
Return m_Boss
End Get Set(ByVal Value As Manager)
m_Boss = Value
End Set
End Property
End Class
Если Вы заметили ошибку, выделите, пожалуйста, необходимый текст и нажмите CTRL + Enter, чтобы сообщить об этом редактору.