2010年12月7日 星期二

vb.net 帶有事件的類的編寫

Public Class Draw ''' ''' 卡號 ''' ''' Private _id As Integer Public Property CardID() As Integer Get Return Me._id End Get Set(ByVal value As Integer) Me._id = value End Set End Property ''' ''' 姓名 ''' ''' Private _name As String Public Property Name() As String Get Return _name End Get Set(ByVal value As String) Me._name = value End Set End Property ''' ''' 金額 ''' ''' Private _money As Double Public Property Money() As Double Get Return Me._money End Get Set(ByVal value As Double) Me._money = value End Set End Property ''事件申明 Public Event DrawEvent(ByVal sender As Object, ByVal e As EventArgs) Public Sub PlayMoney() ' Console.WriteLine(Me.CardID.ToString()) If (CStr(Me.CardID)).Length = 1 Then RaiseEvent DrawEvent(Me, Nothing) ''觸發事件 Console.ReadLine() Else Console.WriteLine("{0}的錢是:{1}", Me.Name, Me.Money) End If End Sub End Class Public Class Test Public Sub drawException(ByVal sender As Object, ByVal e As EventArgs) Console.WriteLine("this is a wrong") End Sub Public Sub Main() Dim draw As New Draw() draw.Name = "hutinghu" draw.Money = 1000 AddHandler draw.DrawEvent, AddressOf drawException ''調用事件及綁定事件方法 draw.PlayMoney() Console.ReadLine() End Sub End Class

沒有留言:

張貼留言