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


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


Console.WriteLinet"After raise " & Sally.TheName &_ "has salary "_

& Sally.Salary)

Рис.9.5. Сериализация динамического массива

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

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




- Начало -  - Назад -  - Вперед -



Книжный магазин