none
VB.NET引发事件问题 RRS feed

  • 问题

  • Module Module1
        Public Interface MyInterface
            Property stuName() As String                    ' 声明属性
            Function GetScores(ByVal x As Single) As Single ' 声明Function过程
            Event CalComplete()                             ' 声明事件
        End Interface
        Public Class stuInfo                                ' 用stuInfo类实现接口
            Implements MyInterface                          ' 用Implements语句指定接口
            Private studentScore As Single                  ' 声明类中的变量
            Private studentName As String                   ' 声明类中的变量
            Public Property Score() As Single               ' 声明类中的属性
                Get
                    Return studentScore
                End Get
                Set(ByVal Value As Single)
                    studentScore = Value
                End Set
            End Property
            Public Property stuName() As String Implements MyInterface.stuName
                Get
                    Return studentName
                End Get
                Set(ByVal Value As String)
                    studentName = Value
                End Set
            End Property
            Public Event CalComplete() Implements MyInterface.CalComplete    ' 声明事件 
            Public Function GetScore(ByVal x As Single) As Single Implements MyInterface.GetScores
                Return x * 0.8
                RaiseEvent CalComplete()                    ' 引发一个CalComplete事件
            End Function
        End Class
    End Module

    Public Class Form1
        Private Sub HandleCalComplete()
            MsgBox("成绩修改完成", , "成绩更新")
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim studentInfo As New stuInfo()                  ' 定义类实例
            '建立与studentInfo对象引发的事件关联的处理程序
            AddHandler studentInfo.CalComplete, AddressOf HandleCalComplete
            studentInfo.stuName = "张三"
            studentInfo.Score = 80
            MsgBox("学生" & studentInfo.stuName & "当前成绩是" & studentInfo.Score, , "当前成绩")
            '调用能引发CalComplete事件的GetScore方法
            studentInfo.GetScore(80)
            'MsgBox("学生" & studentInfo.GetScore(80))
        End Sub
    End Class

    为什么不能引发事件?

    2010年4月9日 6:49

答案

  • Public Function GetScore(ByVal x As Single) As Single Implements MyInterface.GetScores
                Return x * 0.8
                RaiseEvent CalComplete()                    ' 引发一个CalComplete事件
            End Function

    引发之前你都return了


    http://feiyun0112.cnblogs.com/
    2010年4月9日 8:33
    版主