0

Here is a simple example of a one-lined IF statement.

if(condition)
    something = "blah";

I find myself using them often for simple things, but later on while expanding the application, expanding them to a multi-lined if statements

if(condition)
{
    something = "blah;
    somethingElse = "blar";
}

But I find this quite frustrating because I have to go through several steps to do so. When I enter the first brace, my code formatter (Visual Studio? Resharper?) proceeds to enter a second (closing) brace, and formats it like so:

if(condition) { }
something = "blah";

I then have to select and remove the closing brace, and do my own formatting to get it to look correct.

Is there a way that I can conveniently have resharper or visual studio automatically enter the braces around the one-lined IF statement? e.g: I entered an opening brace after the condition and it automatically formatted it like

if(condition)
{
    something = "blah";
}
caesay
  • 16,932
  • 15
  • 95
  • 160
  • http://stackoverflow.com/a/3622563/1583 – Oded May 20 '12 at 19:11
  • possible duplicate of [ReSharper - force curly braces around single line](http://stackoverflow.com/questions/3622236/resharper-force-curly-braces-around-single-line) – Oded May 20 '12 at 19:12
  • Not a duplicate, i dont want to force curly braces around single lined statements. I want to make it easier to switch between the two. – caesay May 20 '12 at 19:13
  • It is not clear that you want to go back from `{}` to none as well. – Oded May 20 '12 at 19:14
  • I did not see that as being feasible, therefore I didn't include it in the question. – caesay May 20 '12 at 19:15

1 Answers1

1

What you can do is define a new snippet that is exactly like the if code snippet without the "if ($expression)" part:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>multiif</Title>
            <Shortcut>multiif</Shortcut>
            <Description>Code snippet for multiline if statement</Description>
            <Author>Microsoft Corporation</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
                <SnippetType>SurroundsWith</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>expression</ID>
                    <ToolTip>Expression to evaluate</ToolTip>
                    <Default>true</Default>
                </Literal>
            </Declarations>
            <Code Language="csharp"><![CDATA[
    {
        $selected$ $end$
    }]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

Then you can import this snippet into visual studio by:

  1. Select Code Snippet Manager... from the Tools menu.
  2. Click on the Import... button.
  3. Navigate to your new snippet file.

or: Copy your new snippet file into C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Visual C#

You can then select the body of your single line if statement in your c# file, right click and select Surround With... then select your new snippet.

Maciej
  • 2,175
  • 1
  • 18
  • 29