定义变量的更改事件 [英] Define the change event for variable

查看:91
本文介绍了定义变量的更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在创建一个变量.

喜欢:

Hi
I''m creating a variable.

Like:

Dim MMM6 as String


现在,我只想为MMM6定义change事件.



例如文本框事件更改.


Now I want just define the change event for MMM6.



such as textbox event change.

Handles TextBox1.TextChanged

推荐答案

A String 没有可以观看的Event.
您最能做的是将它包裹在Property 中,并在设置String时举起Event (或Method).
A String does not have Events you can watch.
The closest thing you can do is wrap it in a Property and raise an Event (or Method) when you set the String.
' A custom Event Handler for your Event.
' Contains the old and the new value of the Property.
Public Class MmmSixChangedEventArgs
    Inherits System.EventArgs
    
    Private _oldValue As String
    Private _newValue As String
    
    Public Sub New(ByVal oldValue As String, ByVal newValue As String)
        MyBase.New()
        _oldValue = oldValue
        _newValue = newValue
    End Sub
    
    Public ReadOnly Property OldValue As String
        Get
            Return _oldValue
        End Get
    End Property
    
    Public ReadOnly Property NewValue As String
        Get
            Return _newValue
        End Get
    End Property
    
End Class

' The delegate that represents your Event Handler.
Public Delegate Sub MmmSixChangedEventHandler(ByVal sender As Object, ByVal e As MmmSixChangedEventArgs)

Public Class TestClass

    ' The Event to raise.
    Public Event MmmSixChanged As MmmSixChangedEventHandler
    
    Private _MMM6 As String
    Public Property MMM6 As String
        Get
            Return _MMM6
        End Get
        Set(ByVal value As String)
            Dim oldValue As String = _MMM6
            _MMM6 = value
            RaiseEvent MmmSixChanged(Me, New MmmSixChangedEventHandler(value, oldValue)
        End Set
    End Property

End Class


如果您不想将Event 公开给其他类,则可以简单地调用Private Method.也就是说,您还可以创建一个Private Property.但是,如果对_MMM6的访问全部为Private ,我想您会知道它何时更改了,并且不需要Event s.
希望对您有所帮助.


为SA创建了一个自定义EventArgs:)


If you do not want to expose the Event to other classes you can simply call a Private Method instead. That said, you could also create a Private Property. However, if access to _MMM6 is all Private I guess you''d know when it changed and wouldn''t need Events.
Hope it helps.


Created a custom EventArgs for SA :)


这篇关于定义变量的更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆