1

I have migrated a vb6 control to vb.net which references FARPoint Spread and below is the VB6 code.

  Public Property Let RetainSelBlock(ByVal New_RetainSelBlock As Boolean)
     sprSpread.RetainSelBlock() = New_RetainSelBlock
     PropertyChanged "RetainSelBlock"
 End Property 

The below code is the wizard generated VB.Net code from the above Vb6 code.

 Public Property RetainSelBlock() As Boolean
    Get
        RetainSelBlock = sprSpread.RetainSelBlock
    End Get
    Set(ByVal Value As Boolean)
        sprSpread.RetainSelBlock = Value
         RaiseEvent RetainSelBlockChange()
    End Set
End Property

 Public Event RetainSelBlockChange()

as you can see, VB6 PropertyChanged method got changed to RaiseEvent. Is this correct ?

vandy
  • 63
  • 8
  • 1
    Sounds like it's correct way to use `RaiseEvent` based on this post: https://stackoverflow.com/questions/1996051/variable-property-changed-event-in-vb-net. – Tetsuya Yamamoto Sep 28 '18 at 02:07
  • You may find this migration topic helpful in deciding how to proceed, [CanPropertyChange and PropertyChanged methods](http://www.vbmigration.com/detknowledgebase.aspx?Id=464). My interpretation is that implementing the .Net [INotifyPropertyChanged interface](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.inotifypropertychanged?view=netframework-4.7.2) would be the most appropriate way forward. However that is something that you need to decide based on the intended use. – TnTinMn Sep 28 '18 at 13:26

2 Answers2

2

That is basically correct but the proper way to implement such an event is like this:

Public Property RetainSelBlock() As Boolean
    Get
        Return sprSpread.RetainSelBlock
    End Get
    Set
        If sprSpread.RetainSelBlock <> value Then
            sprSpread.RetainSelBlock = value
            OnRetainSelBlockChanged(EventArgs.Empty)
        End If
    End Set
End Property

Public Event RetainSelBlockChanged As EventHandler

Protected Overridable Sub OnRetainSelBlockChanged(e As EventArgs)
    RaiseEvent RetainSelBlockChanged(Me, e)
End Sub

Note that, in this case, the event is only raised if the property value actually changes. If you assign the same value to the property again, the event will not be raised again. Also, this code follows the intended pattern of using the EventHandler delegate so that event handlers have the intended consistent signature, as well as raising the event only via a dedicated method that can be overridden in derived classes to provide custom behaviour on the inherited event.

Note that you could also implement the INotifyPropertyChanged interface in the class that has that property and then raise the PropertyChangedevent. That might actually be closer to the VB6 way of doing things although I'm not sure. The nice thing about that is that you only have one event so you write less code in the producer and the consumer, so it's nice if you have lots of properties. The downside is that you then have to write conditional code in the consumer to determine which property changed, rather than having distinct event handlers for each property.

EDIT: Also not the more correct name of 'RetainSelBlockChanged' rather than RetainSelBlockChange. It is convention to use a suffix of "Changed" for an event that is raised after a property value changes and "Changing" for an event that is raised before a property changes and allows you to cancel the change.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
0

Thanks jmcilhinney. based on your input i have implemented INotifyPropertyChanged interface. below is the code if any body wants to refer in future.

Public Property RetainSelBlock() As Boolean
    Get
        Return sprSpread.RetainSelBlock
    End Get
    Set
        If sprSpread.RetainSelBlock <> Value Then
            sprSpread.RetainSelBlock = Value
            OnRetainSelBlockChanged("RetainSelBlock")
        End If
    End Set
End Property

Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

Private Sub OnRetainSelBlockChanged(ByVal info As String)
    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
End Sub
vandy
  • 63
  • 8