© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
16 replies
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;
    }
}
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.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ how do i join two lists?
C#CC# / help
2y ago
How do I split these strings?
C#CC# / help
4y ago
How do i make these Readlines not interfere with each other
C#CC# / help
3y ago