Reputation: 17233
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";
}
Upvotes: 0
Views: 1775
Reputation: 2185
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:
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.
Upvotes: 1