21

I've build some snippets to generate a fields for a setting class. I'm now using 2 variables - $setting$ and $Setting$ - to generate names for the property and the backing field. I like to use a single variable, because the only difference is that the backing field is always a lower-cased version.

Current code:

string $setting$;

/// <summary>
/// Gets the $bigComment$.
/// </summary>
/// <value>The $smallComment$.</value>
public string $Setting$
{
    get
    {
        if (String.IsNullOrEmpty($setting$))
        {
            $setting$ = CFW.Common.Configuration.GetAppSetting("$Setting$", $defaultValue$);
        }

        return $setting$;
    }
}

Is this possible?

Kees C. Bakker
  • 32,294
  • 27
  • 115
  • 203

2 Answers2

6

It is not possible to change literals in a Code Snippets. There are some functions available:

GenerateSwitchCases - Generates a switch statement and a set of case statements for the members of the enumeration specified by the EnumerationLiteral parameter. The EnumerationLiteral parameter must be either a reference to an enumeration literal or an enumeration type.

ClassName - Returns the name of the class that contains the inserted snippet.

SimpleTypeName - Reduces the TypeName parameter to its simplest form in the context in which the snippet was invoked.

But they cannot modify literals.

Source: http://msdn.microsoft.com/en-us/library/ms242312(v=vs.110).aspx

Community
  • 1
  • 1
Kees C. Bakker
  • 32,294
  • 27
  • 115
  • 203
-4

According to the official MSDN docs, default values for snippet variables should be defined in the XML of the snippet, not using the variable names.

So you'd have something like this:

<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/CodeSnippet">
    <CodeSnippet>
        <Header>
            <!-- Add header information here -->
        </Header>
        <Snippet>
            <!-- Add additional Snippet information here -->
            <Declarations>
                <Literal>
                    <ID>SettingsField</ID>
                    <ToolTip>The settings field.</ToolTip>
                    <Default>settings</Default>
                </Literal>
                <Object>
                    <ID>SettingsProperty</ID>
                    <ToolTip>The settings property.</ToolTip>
                    <Default>Settings</Default>
                </Object>
            </Declarations>
            <Code Language="CSharp">
                <![CDATA[
                    Snippet code with $SettingsField$ and $SettingsProperty$
                ]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>
Solal Pirelli
  • 1,199
  • 9
  • 21
  • 4
    Yeah, I know. The thing is, I want to use a single literal for both $SettingsField$ and $SettingsProperty$ - because of the only thing that's different is the casing of the first letter. – Kees C. Bakker Feb 26 '14 at 12:25
  • If you'd like Visual Studio to automatically make one variable be the camelCase version of the other, I'm afraid that's not possible. Otherwise... how would the generated code look? Different members cannot have the same name. – Solal Pirelli Feb 26 '14 at 12:27
  • 4
    @SolalPirelli there are plenty of examples where you want an camel case version as well as lowercase. – Ben Racicot Jan 11 '17 at 21:05