Tracking change of control variable.

  • Thread starter Thread starter tc
  • Start date Start date
T

tc

I'm trying to put together a small control. It's not, but for arguments
sake, let's say it's a progress bar.

The user will make a change to a setting, lets call it 'value'.

I want to trap the change of the public variable in the controls code so I
can act on that change.

How would I do that?
 
Use a property instead of a public variable :http://msdn2.microsoft.com/en-us/library/zzh9ha57(VS.80).aspx

You may want to read the whole spec at least once so that you know what is
available :http://msdn2.microsoft.com/en-us/library/ms234437(vs.80).aspx

To build on Patrice's statements:

I would change the public variable (which should almost always be
avoided imo) into a public property. In the Set accessor I would
inspect the value to see if it is different than the current value and
if so I would fire off the necessary events.

Something like (untested):

//////////////////////
Public Class TheClass

Public Property MyProperty() As String
Get
Return _MyProperty
End Get
Set(ByVal value As String)
Dim isDifferent As Boolean = (value <> _MyProperty)
If isDifferent Then OnMyPropertyChanging(EventArgs.Empty)
_MyProperty = value
If isDifferent Then OnMyPropertyChanged(EventArgs.Empty)
End Set
End Property
Private _MyProperty As String = ""

Public Event MyPropertyChanging As EventHandler
Public Event MyPropertyChanged As EventHandler

Protected Overridable Sub OnMyPropertyChanging(ByVal e As
EventArgs)
RaiseEvent MyPropertyChanging(Me, e)
End Sub

Protected Overridable Sub OnMyPropertyChanged(ByVal e As
EventArgs)
RaiseEvent MyPropertyChanged(Me, e)
End Sub

End Class
////////////////////

That way you have events that can notify any listeners both before and
after the change to the property occurs.

Thanks,

Seth Rowe
 
Thanks for your input peeps.

Use a property instead of a public variable
:http://msdn2.microsoft.com/en-us/library/zzh9ha57(VS.80).aspx

You may want to read the whole spec at least once so that you know what is
available :http://msdn2.microsoft.com/en-us/library/ms234437(vs.80).aspx

To build on Patrice's statements:

I would change the public variable (which should almost always be
avoided imo) into a public property. In the Set accessor I would
inspect the value to see if it is different than the current value and
if so I would fire off the necessary events.

Something like (untested):

//////////////////////
Public Class TheClass

Public Property MyProperty() As String
Get
Return _MyProperty
End Get
Set(ByVal value As String)
Dim isDifferent As Boolean = (value <> _MyProperty)
If isDifferent Then OnMyPropertyChanging(EventArgs.Empty)
_MyProperty = value
If isDifferent Then OnMyPropertyChanged(EventArgs.Empty)
End Set
End Property
Private _MyProperty As String = ""

Public Event MyPropertyChanging As EventHandler
Public Event MyPropertyChanged As EventHandler

Protected Overridable Sub OnMyPropertyChanging(ByVal e As
EventArgs)
RaiseEvent MyPropertyChanging(Me, e)
End Sub

Protected Overridable Sub OnMyPropertyChanged(ByVal e As
EventArgs)
RaiseEvent MyPropertyChanged(Me, e)
End Sub

End Class
////////////////////

That way you have events that can notify any listeners both before and
after the change to the property occurs.

Thanks,

Seth Rowe
 
Back
Top