vendredi 3 juin 2016

Creating An Undo Button for application

I am currently trying to create an undo feature that allows a user of the application to revert the work that they have done on their object. I need some way that I can create a data table on the fly that generates columns based on the class type's members(Yes I could use a giant if statement to sift through the 30 different class types but where's the challenge in that). This UndoRedo class will receive a before and after object and should save them both into sed data table^. when user clicks undo, the object gets reverted. My guess is reflection (i already have a function that can convert from a list to a data table), but I am not sure. Using Newest version of VB.NET.

Here is my new item function that should add objects to the data table, and if it is first object, generate the data table based on the objects class.

Function NewItem(Before As IEnumerable(Of Object), After As IEnumerable(Of Object))

    If (Before Is Nothing And After Is Nothing) Then
        'Both objects are empty

    ElseIf (Before Is After) Then
        'Objects are the same

    ElseIf Before Is Nothing Or After Is Nothing Then
        'A new Object was created and therefore there was no object before it

    Else
        'Just convert to data row


    End If



    Return Nothing
End Function

Also here is my function to convert from a list to a data table:

 ''' <summary>
''' Converts a list to a datatable
''' </summary>
''' <typeparam name="T"></typeparam>
''' <param name="list"></param>
''' <returns></returns>
Public Shared Function ConvertToDataTable(Of T)(ByVal list As IList(Of T)) As DataTable

    Dim table As New DataTable()
    Dim fields() As FieldInfo = GetType(T).GetFields()
    For Each field As FieldInfo In fields
        table.Columns.Add(field.Name, field.FieldType)
    Next
    For Each item As T In list
        Dim row As DataRow = table.NewRow()
        For Each field As FieldInfo In fields
            row(field.Name) = field.GetValue(item)
        Next
        table.Rows.Add(row)
    Next
    Return table
End Function

End Class





Aucun commentaire:

Enregistrer un commentaire