Module Module1
Public Class Employee
'私有字段
Private EmpName As String
Private EmpTel As String
Private EmpAddr As String
Private Postalcode As Integer
'共享字段
Shared InstanceCount As Integer
'构造函数
Public Sub New()
InstanceCount += 1
End Sub
'属性
Public Property Name As String
Get
Return EmpName
End Get
Set(ByVal value As String)
EmpName = value
End Set
End Property
Public Property Tel As String
Get
Return EmpTel
End Get
Set(ByVal value As String)
EmpTel = value
End Set
End Property
Public Property Addr As String
Get
Return EmpAddr
End Get
Set(ByVal value As String)
EmpAddr = value
End Set
End Property
Public Property pCode As Integer
Get
Return Postalcode
End Get
Set(ByVal value As Integer)
Postalcode = value
End Set
End Property
'共享方法
Public Shared Function GetInstancdCount() As Integer
Return InstanceCount
End Function
'共享事件
Public Event sharedEvent()
Public Sub doSharedEvent()
Console.WriteLine("共享事件")
RaiseEvent sharedEvent()
End Sub
'事件
Public Event InstanceEvent()
Public Sub doSomething()
Console.WriteLine("普通事件")
RaiseEvent InstanceEvent()
Console.WriteLine("共享事件")
RaiseEvent sharedEvent()
End Sub
'返回值的方法
Public Function EmpInfo() As String
Dim result As String
result = "姓名:" & Name & vbCrLf
result &= "电话:" & Tel & vbNewLine
result &= "地址:" & Addr & vbCrLf
result &= "邮编:" & pCode
Return result
End Function
'不返回值的方法
Public Sub Output(ByVal emp As Employee)
Dim res As String
res = EmpInfo()
Console.WriteLine(res)
End Sub
End Class
Dim WithEvents eObj As New Employee
Sub Main()
eObj.doSomething()
eObj.doSharedEvent()
Console.Read()
End Sub
Public Sub run_EventHandle() Handles eObj.InstanceEvent, eObj.sharedEvent
Console.WriteLine("处理了一个事件")
End Sub
End Module