ilivewithian
ilivewithian

Reputation: 19702

Can I have a code snippet in VS 2010 where the replacement replaces multiple items

I have a very simple code snitppet that looks like this:

<asp:Label Text="$Name$" runat="server" AssociatedControlID="$txtName$" />
<asp:TextBox runat="server" ID="$txtName$" />
<br />

What I was hoping was that the $txtName$ would be changed in both places, but it doesn't, only the first instance is changed and I can't even tab over to the second instance.

Is there a work around to this?

Upvotes: 0

Views: 1175

Answers (3)

wałdis iljuczonok
wałdis iljuczonok

Reputation: 662

interesting scenario starts when you would like to create surround snippet with $selected$ twice. for instance to create CheckArgumentForNull snippet to check argument for null and in case of null raise ArgumentNullException with parameter name. Only later got replaced.

Any ideas? Of course we can use expansion only snippet and type argument name by ourselves.

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
        <SnippetType>SurroundsWith</SnippetType>
      </SnippetTypes>
      <Title>notnull</Title>
      <Author>Viiar Consulting</Author>
      <Description>Code snippet for checking whether is argument null</Description>
      <HelpUrl>
      </HelpUrl>
      <Shortcut>notnull</Shortcut>
    </Header>
    <Snippet>
      <Imports>
        <Import>
          <Namespace>System</Namespace>
        </Import>
      </Imports>
      <Declarations>
        <Literal Editable="true">
          <ID>argument</ID>
          <ToolTip>
          </ToolTip>
          <Default>argument</Default>
          <Function>
          </Function>
        </Literal>
      </Declarations>
      <Code Language="csharp"><![CDATA[if ($selected$ == null)
{
    throw new ArgumentNullException("$selected$");
}
$end$]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

Upvotes: 1

ilivewithian
ilivewithian

Reputation: 19702

It turns out that the replacement was working, but not as I typed. So I can type what I need and then to get the additional properties changed I have to take the focus away from the snippet, so pressing the down arrow key does the job.

Upvotes: 1

Richard
Richard

Reputation: 109100

This should work and does for me.

Can you show the complete snippet file? Here is one that works, type and fieldName are replaced multiple times:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <Header>
        <Title>Non-automatically implemented property</Title>
        <Author>Richard Cox</Author>
        <Shortcut>propf</Shortcut>
        <Description>Property with exlicit field</Description>
        <SnippetTypes>
            <SnippetType>Expansion</SnippetType>
        </SnippetTypes>
    </Header>
    <Snippet>
        <Declarations>
            <Literal>
                <ID>propName</ID>
                <ToolTip>Property Name</ToolTip>
                <Default>Name</Default>
            </Literal>
            <Literal>
                <ID>fieldName</ID>
                <ToolTip>Field Name</ToolTip>
                <Default>field</Default>
            </Literal>
            <Literal>
                <ID>type</ID>
                <ToolTip>Property type</ToolTip>
                <Default>string</Default>
            </Literal>
        </Declarations>
        <Code Language="csharp">
            <![CDATA[private $type$ $fieldName$;
    public $type$ $propName$ {
        get { return $fieldName$;}
        set { $fieldName$ = value;}
    }

    $end$]]>
        </Code>
    </Snippet>
</CodeSnippet>

Upvotes: 1

Related Questions