C#C
C#3y ago
Google

❔ How do I make these two codes less copy pasta?

void Redo()
{
    var thingToUnreverse = Actions[CurrentActionStep + 1];
    foreach (var thing in thingToUnreverse.thingsMoved)
    {
        var bvmToMove = objectCollection.FirstOrDefault(bvm => bvm.Guid == thing.objectViewModel.Guid);
        if (bvmToMove is null) { continue; }
        bvmToMove.Position = thing.NewPosition;
    } //There are also a dozen other operations that occur here.
}

void Undo()
{
    var thingToReverse = Actions[CurrentActionStep];
    foreach (var thing in thingToReverse.thingsMoved)
    {
        var bvmToMove = objectCollection.FirstOrDefault(bvm => bvm.Guid == thing.objectViewModel.Guid);
        if (bvmToMove is null) { continue; }
        bvmToMove.Position = thing.OldPosition;
    }
}

Looking for a way to combine these two to more share code so I don't have to update each every time.
Was this page helpful?