I'm trying to follow MVVM pattern in a VB.net WPF application, and I would like to implement IEditableObject.

Is this code acceptable for an MVVM application? In particular is it okay to have the IEditableObject implemented here on the Model rather than the ViewModel?

Public Class Component : Implements IEditableObject
    Property Name As String
    Property Part_Number As String
    Property Position_Count As Integer
    Property Stage as Integer
    Private _stored_properties As Hashtable = Nothing

    ReadOnly Property Full_Name As String
            Return Name + " " + Stage.ToString()
        End Get
    End Property

    Public Sub BeginEdit() Implements IEditableObject.BeginEdit
        Dim properties = Me.GetType.GetProperties
        _stored_properties = New Hashtable(properties.Length - 1)
        For Each prop In properties
            If prop.GetSetMethod IsNot Nothing Then _stored_properties.Add(prop.Name, prop.GetValue(Me, Nothing))
    End Sub

    Public Sub EndEdit() Implements IEditableObject.EndEdit
        _stored_properties = Nothing
    End Sub

    Public Sub CancelEdit() Implements IEditableObject.CancelEdit
        If _stored_properties Is Nothing Then Return
        Dim properties = Me.GetType.GetProperties
        For Each prop In properties
            If prop.GetSetMethod IsNot Nothing Then prop.SetValue(Me, _stored_properties(prop.Name))
    End Sub
End Class
  • \$\begingroup\$ Do you want a code review, or just an answer to your question? If you just want an answer it might be better to ask on Stack Overflow. \$\endgroup\$
    – pacmaninbw
    Oct 21 '19 at 14:18
  • \$\begingroup\$ @pacmaninbw Sorry quite inexperienced on this part of SE, I take it both isn't an acceptable answer? I can edit it to just be code review if required? \$\endgroup\$ Oct 21 '19 at 14:25
  • \$\begingroup\$ No, now that you've clarified what you are looking for it's fine. \$\endgroup\$
    – pacmaninbw
    Oct 21 '19 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.