How can I save the original state of my Model and its XAML bindings in C# in a Variable?

Tom Meier 260 Reputation points
2024-06-03T21:06:41.3733333+00:00

I need to save the original state of my viewmodel and XAML bindings in C# when a View is originally loaded, so that I can revert back to the original Model property values if the user chooses to cancel their changes. I attempted to use the code below, but I realized that my variable tempCompanyInfo is still pointing to retModel after the changes, likely because CompanyInfo is a reference variable. Any suggestions on how I can properly save the original state?

CompanyInfo=null;
CompanyInfo=retModel;
tempCompanyInfo=retModel;
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,722 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,117 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 55,476 Reputation points
    2024-06-03T21:22:27.6833333+00:00

    What you're looking for is the Memento pattern. This pattern allows you to "save" and "restore" changes to an object. It is popular with undo/redo situations.

    If you don't need this but in one case then really the best option is to pass to your view model the data you want to modify and have it copy the values it cares about into properties. Then bind the UX to the properties. When the user "cancels" then there is nothing you need to do. If they "save" then you copy the properties from the model back to the original object.

    If you really need a memento implementation then it is not hard to create your own interface and implementation. Because objects can vary wildly and what you may or may not want to save/restore can vary you'll end up having to implement this for each type you want to support it with. There are countless examples online on how to do this in C#. But I wouldn't go that route unless you really need the complexity.

    1 person found this answer helpful.
    0 comments No comments

  2. Bruce (SqlWork.com) 68,311 Reputation points
    2024-06-03T22:45:39.5933333+00:00

    object instance assignments are by ref.

    using System;
    public class Program
    {
    	public class Test
    	{
    		public string Foo {get; set;} = "Foo";
    		public string Bar {get; set;} = "Bar";
    		
    		public Test Clone() => new Test
    		{
    			Foo = this.Foo,
    			Bar = this.Bar
    		};
    	}
    	public static void Main()
    	{
    		var f1 = new Test();
    		var f2 = f1;
      		var f3 = f1.Clone();		
    		Console.WriteLine(f1.Foo); //Foo
    		f2.Foo = "Test";
    		Console.WriteLine(f1.Foo); //Test
    		Console.WriteLine(f3.Foo); //Foo
    	}
    }
    

    you need to implement a clone for view model, or implement change tracking.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.