Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I'm creating a variable.

Like:

VB
Dim MMM6 as String

Now I want just define the change event for MMM6.



such as textbox event change.
Handles TextBox1.TextChanged
Posted
Updated 27-Nov-11 15:03pm
v2

1 solution

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.
VB
' 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

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.

Edit:
Created a custom EventArgs for SA :)
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 27-Nov-11 15:44pm    
I knew you would know this sophisticated technique :-)

My 4, as you really would need to improve it greatly. You would create a custom EventArgs-based class with properties OldValue (null if never assigned) and NewValue. These two properties will be extremely handy when implementing the handler of MMM6Changed. Also, more correct naming meeting naming rules would be rather "WwwSixChanged".
--SA
Sander Rossel 27-Nov-11 15:59pm    
Sophisticated? Can't tell if you're serious or joking... Didn't seem all that sophisticated to me ;)
Custom EventArgs sounds like a good idea. Alternatively I could have raised an MmmSixChangING Event. And while I am at it Implement IPropertyChanging and IPropertyChanged (that both have their custom EventArgs that do exactly the same).
Sander Rossel 27-Nov-11 16:10pm    
Added the custom EventHandler. I hope it meets your high solution standards ;p
Now don't go telling me I should have made the custom EventArgs generic or anything like that :)
LanFanNinja 27-Nov-11 21:31pm    
+5
Sander Rossel 28-Nov-11 2:28am    
Thanks LanFan :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900