1

I'm trying to create a snippet for a C# Property with the method NotifyPropertyChanged().
The result of the snippet should be like this:

private int _myProperty;

    public int MyProperty
    {
        get => _myProperty;
        set
        {
            if (_myProperty == value) return;
            _myProperty = value;
            NotifyPropertyChanged(nameof(MyProperty));
        }
    }

How do I change the case of the first letter of _MyProperty to be _myProperty? See below my current snippet:

<Snippet>
  <Declarations>
    <Literal>
      <ID>type</ID>
      <ToolTip>Property type</ToolTip>
      <Default>int</Default>
    </Literal>
    <Literal>
      <ID>property</ID>
      <ToolTip>Property name</ToolTip>
      <Default>MyProperty</Default>
    </Literal>
  </Declarations>
  <Code Language="csharp">
    <![CDATA[private $type$ _$property$;

public $type$ $property$
{
    get => _$property$;
    set 
    { 
        if (_$property$==value) return;
        _$property$ = value;
        NotifyPropertyChanged(nameof($property$));          
    }
}
$end$]]>
  </Code>
</Snippet>
  • Maybe you can set up naming rules that check the property has init caps, or hen you can enter the snippet variable as lowercase, causing your property to become lower case initial letter, causing vs' code analyser to complain, and then you'll get a single click "fix all in project" light bulb on the margin – Caius Jard May 28 '21 at 07:14
  • I've the same problem. But it's not a big problem: when using snippets you have to enter parameters and while in this snippet you have just one, the necessity to *post-edit* of snippets (to customize them even more) is often required. You can simply hit `F2` (or what is your hotkey for rename function) and change first letter of field name manually. – Sinatr May 28 '21 at 07:23
  • Yes, both links answered my question. So it is not possible to change. I will just do as I've done until now, which is to enter camelCase field name, then on the property press CRTL+. and fix name violation (properties are in PascalCase). Thank you – Mario Todeschini May 29 '21 at 09:54

0 Answers0