1

Possible Duplicate:
Formatting Literal parameters of a C# code snippet

EDIT: This can be closed. Found an exact duplicate, seems there is no solution. =(

Exact Duplicate: Formatting Literal parameters of a C# code snippet

Is there any way to parse a replacement literal when writing a snippet? I'd like to do something like the following:

<Literal>
    <ID>PropertyName</ID>
</Literal>

User replaces PropertyName with 'MyProperty' and the results are such:

private object _myProperty;

public object MyProperty
{get;set;}

Note the capitalization. I need a way to parse the replacement literal and manipulate it. The underscore is trivial, simply a matter of hard coding that in.

Any chance here?

Edit; full snippet:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>MVVM Public Property</Title>
      <Author>Michael Leide</Author>
      <Description>Adds a public property with private backing and property changed event support.</Description>
      <Shortcut>propvm</Shortcut>
    </Header>

    <Snippet>
      <Declarations>
        <Literal Editable="true">
          <ID>PropertyType</ID>
          <Default>object</Default>          
        </Literal>

        <Literal Editable="true">
          <ID>PropertyName</ID>
          <Default>PropertyName</Default>
        </Literal>
      </Declarations>

      <Code Language="csharp" Kind="" Delimiter="$">
        <![CDATA[
        $PropertyType$ _$PropertyName$;

        public $PropertyType$ $PropertyName$ {
            get {
                if (_$PropertyName$ == null)
                    _$PropertyName$ = new $PropertyType$();
                return _$PropertyName$;
            } set {
                _$PropertyName$ = value;
                this.OnPropertyChanged("$PropertyName$");
        }   }
        ]]>      
      </Code>

    </Snippet>
  </CodeSnippet>
</CodeSnippets>
Community
  • 1
  • 1
Michael
  • 1,803
  • 1
  • 17
  • 26

1 Answers1

1

Unfortunately you can't transform your PropertyName into propertyName directlly. The best way to go about this is to include a third literal to specify the backing field. This prevents the ambiguity in your current literal and only adds an extra couple keystrokes.

The Declarations block looks like this:

  <Declarations>
    <Literal Editable="true">
      <ID>PropertyType</ID>
      <Default>object</Default>          
    </Literal>

    <Literal Editable="true">
      <ID>PropertyName</ID>
      <Default>PropertyName</Default>
    </Literal>

    <Literal Editable="true">
      <ID>BackingPropertyField</ID>
      <Default>backingPropertyField</Default>
    </Literal>
  </Declarations>

And the code block becomes:

    public $PropertyType$ $PropertyName$ {
        get {
            if (_$BackingPropertyField$ == null)
                _$BackingPropertyField$ = new $PropertyType$();
            return _$BackingPropertyField$;
        } set {
            _$BackingPropertyField$ = value;
            this.OnPropertyChanged("$PropertyName$");
    }   }
    ]]>      
  </Code>

You can then just tab through and specify the desired names.

Chris
  • 2,885
  • 18
  • 25
  • Seems you're absolutely correct, although that's a shame. Part of the snippet 'requirements' was that it would synchronize the backing field and property names. It may be ambiguous from the purest of perspectives, but the snippet is just for me, and I'm well aware of what's going on. I guess my backing fields are just going to have to get a capital letter after their underscores =) – Michael Nov 27 '12 at 16:55