VB.NET withevent 自定义事件处理

Module Module1
    Private WithEvents aemp As part2
    Sub Main()
        Dim chen As New part2("chen lili", 20000)
        aemp = chen
        Console.WriteLine(chen.Name & " prevouis salary is " & chen.Salary)
        aemp.AddSalary(0.3D)
        Console.WriteLine(chen.Name & " current salary is " & chen.Salary)
        aemp.AddSalary(0.3D, "hell")
        Console.WriteLine(chen.Name & " now salary is " & chen.Salary)
        Console.ReadLine()

    End Sub
    Public Sub aemp_AddSalaryEvent(ByVal sender As ConsoleApplication2.part2, ByVal e As invalidevent) Handles aemp.AddSalaryEvent
        MsgBox(sender.Name & " want to add salary " & FormatPercent(e.percent) & ", can not be agree !" & " the reason is : " & e.message)
    End Sub

    Public Class invalidevent
        Inherits System.EventArgs   ' inherite system common event class
        Private m_event As String   ' save the reson of raising event
        Private m_per As Decimal    ' save the reason of raising event
        Sub New(ByVal per As Decimal, ByVal info As String)
            MyBase.New()
            m_event = info
            m_per = per
        End Sub
        ReadOnly Property message() As String
            Get
                Return m_event
            End Get
        End Property
        ReadOnly Property percent() As Decimal
            Get
                Return m_per
            End Get
        End Property
    End Class

    Public Class part2

        Private m_name As String
        Private m_salary As Decimal
        Private Const limitvalue As Decimal = 0.07
        Public Event AddSalaryEvent(ByVal sender As part2, ByVal e As invalidevent) 'add declare event code here
        Public Sub New(ByVal sname As String, ByVal salary As Decimal)
            m_name = sname
            m_salary = salary

        End Sub
        Public ReadOnly Property Name() As String
            Get
                Return m_name

            End Get
        End Property
        Public ReadOnly Property Salary() As Decimal
            Get
                Return m_salary

            End Get
        End Property
        Public Overridable Overloads Sub AddSalary(ByVal per As Decimal)
            If per > limitvalue Then
                RaiseEvent AddSalaryEvent(Me, New invalidevent(per, "no password to send!"))
            Else
                m_salary = m_salary * (1 + per)
            End If
        End Sub
        Public Overridable Overloads Sub AddSalary(ByVal per As Decimal, ByVal pass As String)
            If pass = "like" Then
                m_salary = m_salary * (1 + per)
            Else
                RaiseEvent AddSalaryEvent(Me, New invalidevent(per, "password invalid !"))
            End If
        End Sub

    End Class

End Module


 

posted @ 2013-07-25 13:12  MMLoveMeMM  阅读(708)  评论(0)    收藏  举报