C# Custom Code Snippet Functions
Asked Answered
M

4

7

I have a code snippet structures like so:

    private $type$ $lowercaseName$;
    public $type$ $uppercaseName$
    {
        get { return $lowercaseName$; }
        set { $lowercaseName$ = value; }
    }

It generates stuff like:

    private string randomValue;
    public string RandomValue
    {
        get { return randomValue; }
        set { randomValue = value; }
    }

Thats a very oversimplified version... its really a lot more complicated... and its just that much more information to type in. It would be nice if you could type in $uppercaseName$, and then a custom snippet function would assign a value to $lowercaseName$....

But is it even possible to write custom snippet functions? I don't see anything about this in the documentation...

If it is possible... how?

Monocycle answered 14/4, 2011 at 12:56 Comment(2)
I wish that were possible... AFAIK, it isn't.Benita
Encapsulating fields in VS2017 can make lowercase to uppercase, but I did not figure out how..Lorrin
G
7

It is not yet possible. See this suggestion for 2010 that was denied.

http://connect.microsoft.com/VisualStudio/feedback/details/523601/allow-custom-code-snippet-functions

It seems this feature was suggested when snippets were introduced, and has been re-suggested every version, and shot down saying they don't have enough time.

Giacopo answered 29/4, 2011 at 22:9 Comment(1)
Poor Microsoft, such a shame, not enough money for implementing simple features.Kao
B
4

Rather than using the convention of having camelCase fields and PascalCase properties, I have adopted a convention of a using leading underscore for backing fields. The following code snippet works for me.

public $type$ $property$
{
    get { return _$property$;}
    set { _$property$ = value;}
}
private $type$ _$property$;
$end$

And in attempt to snub the "underscore is a prefix and prefixes are bad" holy war, I believ it to be more a convention than a prefix much like upper v. lower initial letters for properties v. parameters. Any use of a field with a leading underscore outside of it's associated property implementation should be code smell.

Boer answered 13/12, 2012 at 16:45 Comment(0)
P
1

My answer is Resharper. Live Templates.

It is very annoying having this limitation, but like many other Visual Studio limitations, Resharper nails it. It is a performance hog, so I operate with the code analysis turned off. The speed is then acceptable, and live templates save a lot of time.

There are about twenty other good reasons to have a tool LIKE Resharper to make you a fantastic coder.

I can't write "hello world" without Resharper any more... and I can't debug it without Reflector =P

Also, go to the Extension Manager and click Online Gallery. Type "snippet" in the Search box, and there are a few tools there that look like they might help for free :). The extension gallery is pure productivity gold.

Posthaste answered 27/10, 2011 at 3:59 Comment(0)
Y
-1

I also use and recommend the use of the _ prefix for the private variable of properties. Sometimes I wish Microsoft allow scoping of the private variable inside the property, as below to prevent access outside the property.

public string RandomValue
{
    private string randomValue;
    get { return randomValue; }
    set { randomValue = value; }
}
Yaws answered 12/12, 2013 at 7:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.