7

I've made a small code snippet to create a property for WPF data bingings:

private string property;
public string Property
{
    get { return this.property; }
    set
    {
        this.property = value;
        this.OnPropertyChanged(() => Property);
    }
}

It is pretty cumbersome to create the field name in Camel Case and rewrite the property in Pascal Case. Is there a way to only write the field and let the snippet writes the property using the name of the field with the first character in upper case?

Chris Ledet
  • 11,458
  • 7
  • 39
  • 47
JiBéDoublevé
  • 4,124
  • 4
  • 36
  • 57
  • 1
    I'd call it bad form to distinguish public/private based only on casing. – BNL Jul 26 '12 at 15:09
  • How about naming the field `backingProperty`, which is camel-cased and can be constructed through simple string concatenation? This additionally makes it less likely other code in the class will accidentally use the field instead of the property. – Ben Voigt Jul 26 '12 at 15:09
  • 1
    Yes, it would be nice if there were a way to add custom logic to automatic properties, but there is not. You mention Visual Studio's code snippet—you could always create a custom code snippet that contains the logic you desire. Personally, I type fast enough that it isn't worth it to worry about such minor annoyances. – Cody Gray - on strike Jul 26 '12 at 15:09
  • 5
    @BNL Really? That's fairly common form in case-sensitive languages like C#. What would you recommend instead? Prefixing with an `m_` or a single underscore? Ironically, lots of people would say that such ornamentation is similarly "bad form". – Cody Gray - on strike Jul 26 '12 at 15:11
  • @Cody: I've seen styles which suffix an underscore, etc. But having names which collide except for case goes against every style recommendation I've ever seen. (And that becomes *really* bad in the presence of technical writers who capitalize the first word of a sentence even if it's an identifier, e.g. [Apple](https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man2/send.2.html)) – Ben Voigt Jul 26 '12 at 15:12
  • 3
    @Ben Voigt: It is the Microsoft recommended naming guidelines as described here: http://msdn.microsoft.com/en-us/library/x2dbyw72(v=vs.71) – JiBéDoublevé Jul 26 '12 at 15:19
  • 1
    @Cody Gray: How do I add logic in a home made code snippet? – JiBéDoublevé Jul 26 '12 at 15:21
  • @JiBéDoublevé: Your link broke, should be [this](http://msdn.microsoft.com/en-us/library/x2dbyw72(v=vs.71)). And no, that only recommends the use of camelCase and PascalCase for private fields vs public properties/fields, respectively. It does NOT recommend using the same name with different case. – Ben Voigt Jul 26 '12 at 15:21

1 Answers1

9

Unfortunately this type of logic is not available in the Visual Studio snippets functionality. Having to type out both names is the best you can do.

Here are the only "functions" available to you when creating a code snippet. MSDN Code Snippet Functions

Products like Resharper provide excellent code snippet (called Templates in Resharper) functionality, with the ability to change the casing of other replacements within the snippet, among many other useful functions. Resharper Template Info

For example, you would be interested in this macro:

"Value of another variable with the first character in lower case"

thornhill
  • 632
  • 7
  • 17
  • the cheap solution is to just have two properties in your snippet, one starting lowercase and one uppercase (http://stackoverflow.com/a/164729/1072869) – Aralox Aug 25 '14 at 00:52