TryParse Code Snippets in Visual Studio

Perhaps I am not especially observant or maybe, just maybe, Visual Studio 2019 lacks code snippets for TryParse. I created two for use with Visual Basic .NET.

<?xml version="1.0" encoding="utf-8" ?>

<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Integer Try Parse</Title>
      <Author>Seth G. R. Herendeen</Author>
      <Description>Integer try parse</Description>
      <Shortcut>itryp</Shortcut>
    </Header>
    <Snippet>
      <Code Language="VB">
        <![CDATA[
        If Integer.TryParse($String$, $Integer$) = False Then
            MessageBox.Show("Failed to parse")
        End If
        ]]>
      </Code>
      <Declarations>
        <Literal>
          <ID>Integer</ID>
          <ToolTip>Choose the variable you would like to convert to.</ToolTip>
          <Default>output</Default>
        </Literal>
		<Literal>
          <ID>String</ID>
          <ToolTip>Choose the variable you would like to convert from.</ToolTip>
          <Default>input</Default>
        </Literal>
      </Declarations>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>
<?xml version="1.0" encoding="utf-8" ?>

<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>decimal Try Parse</Title>
      <Author>Seth G. R. Herendeen</Author>
      <Description>decimal try parse</Description>
      <Shortcut>dectryp</Shortcut>
    </Header>
    <Snippet>
      <Code Language="VB">
        <![CDATA[
        If Decimal.TryParse($String$, $Decimal$) = False Then
            MessageBox.Show("Failed to parse")
        End If
        ]]>
      </Code>
      <Declarations>
        <Literal>
          <ID>Decimal</ID>
          <ToolTip>Choose the variable you would like to convert to.</ToolTip>
          <Default>output</Default>
        </Literal>
		<Literal>
          <ID>String</ID>
          <ToolTip>Choose the variable you would like to convert from.</ToolTip>
          <Default>input</Default>
        </Literal>
      </Declarations>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

I imported these via the code snippet manager, which may be accessed either through the View menu or by pressing the following key combination in rapid succession: CTRL + K, CTRL + B

For more information on code snippets in Visual Studio, see this article.

For a list of pre-existing code snippets, see this different article.

Leave a comment