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?

8
  • 1
    I'd call it bad form to distinguish public/private based only on casing. Commented Jul 26, 2012 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. Commented Jul 26, 2012 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". Commented Jul 26, 2012 at 15:11
  • 3
    @Ben Voigt: It is the Microsoft recommended naming guidelines as described here: msdn.microsoft.com/en-us/library/x2dbyw72(v=vs.71) Commented Jul 26, 2012 at 15:19
  • 1
    @Cody Gray: How do I add logic in a home made code snippet? Commented Jul 26, 2012 at 15:21

1 Answer 1

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"

Sign up to request clarification or add additional context in comments.

1 Comment

the cheap solution is to just have two properties in your snippet, one starting lowercase and one uppercase (stackoverflow.com/a/164729/1072869)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.