Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any shorthand in C# to make a setter additionally set a dirty flag

Tags:

c#

setter

Currently in order to make a setter also set a dirty property I have to do something like this:

private bool _isDirty;
private int _lives;
public int Lives{
    get { return _lives; }
    set {
        if (_lives != value){
            _lives = value;
            _isDirty = true;
        }
    }
}

It's not a huge pain to write but it's a very vertically spacious and repetitive piece of code to write if I use quite a lot of this pattern in my project.

Is there any shorthand or alternative, shorter syntax to do that in C#?

What I am specifically trying to accomplish is that certain variables changing should trigger a dirty flag, which on the render phase of the code can be used to refresh the properties of the rendered object.

like image 752
Maurycy Avatar asked Feb 05 '23 21:02

Maurycy


2 Answers

Create a Class where you implement a helper method.

class DirtyClass
{
  protected bool IsDirty { get; set;}

  protected void ChangeProperty<T>(ref T backing, T Value)
  { 
      if(!backing.Equals(value))
      {
           backing = value;
           IsDirty = true;
      }
  }
}

USe the helper method in the setter

class LivesCounter : DirtyClass
{
   private int _lives;
   public int Lives  
   {
      get { return _lives; }
      set { ChangeProperty(ref _lives, value); }
   }
}

Handling null elements is left as an exercise.

As jdl134679 has mentioned, look into the INotifyPropertyChanged interface.

like image 56
ReneA Avatar answered Feb 08 '23 16:02

ReneA


If you don't like the classes way you could also create a snippet. info: https://msdn.microsoft.com/en-us/library/ms165396.aspx

create a snippet by hand: https://msdn.microsoft.com/en-us/library/ms165394.aspx

OR

install snippet designer https://visualstudiogallery.msdn.microsoft.com/B08B0375-139E-41D7-AF9B-FAEE50F68392

create a snippet and name it propisd (property shortcut)

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
    xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>Properties with Dirty</Title>
            <Author>Myself</Author>
            <Description>Creates a property which ties to isDrty</Description>
            <Shortcut>propisd</Shortcut>
        </Header>
        <Snippet>
            <Code Language="C#">
                <![CDATA[private $TYPE$ $PRIVATENAME$;
                    public $TYPE$ $PROPERTYNAME$
                    {
                        get { return $PRIVATENAME$; }
                        set 
                        {
                            if ($PRIVATENAME$ != value)
                            {
                                $PRIVATENAME$ = value;
                                _isDirty = true;
                            }
                        }
                    }]]>
            </Code>
            <Declarations>
                <Literal>
                    <ID>TYPE</ID>
                    <ToolTip>replace with the type</ToolTip>
                    <Default>"TYPE"</Default>
                </Literal>
                <Literal>
                    <ID>PRIVATENAME</ID>
                    <ToolTip>replace with the private name</ToolTip>
                    <Default>"PRIVATENAME"</Default>
                </Literal>
                <Literal>
                    <ID>PROPERTYNAME</ID>
                    <ToolTip>replace with the property name</ToolTip>
                    <Default>"PROPERTYNAME"</Default>
                </Literal>
            </Declarations>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

save this file as propisd.snippet and add it via the snippet manager.

Whenever you type propisd followed by TAB you will see the snippet. By using Tab you will go through the replacements. Like you would type 'prop' and then do TAB.

like image 31
Schuere Avatar answered Feb 08 '23 14:02

Schuere