Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio's code snippet: how to add logic in it?

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?

like image 709
JiBéDoublevé Avatar asked Jul 26 '12 15:07

JiBéDoublevé


People also ask

How do I add a custom snippet in VS Code?

To create or edit your own snippets, select User Snippets under File > Preferences (Code > Preferences on macOS), and then select the language (by language identifier) for which the snippets should appear, or the New Global Snippets file option if they should appear for all languages.

How do I use code snippet in Visual Studio?

Code snippets can be accessed in the following general ways: On the menu bar, choose Edit > IntelliSense > Insert Snippet. From the right-click or context menu in the code editor, choose Snippet > Insert Snippet.

How do I add a template to VS Code?

In VSCode, open a folder that will contain your new project. Use the Command Palette to execute the command "Project: Create Project From Template". A list of available templates should appear. Select the desired template.

How do I add a snippet?

There are two ways to add a snippet: Type the # symbol into the text editor. Start typing the snippet shortcut, then select the snippet from the dropdown menu. The snippet will automatically populate in the text editor.


1 Answers

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"

like image 51
thornhill Avatar answered Sep 18 '22 12:09

thornhill