Thursday, March 31, 2011

Is there a way to get Visual Studio 2008 to stop formatting my AutoProperties?

In Visual Studio 2008's Options > Text Editor > C# > Formatting, I have the following settings ticked.

  • Automatically format completed statement on ;

  • Automatically format completed block on }

This is really helpful for when I'm writing a method or a for/foreach/while/if statement.

ie if I write

void MyMethod(){}

I want it to reformat it to the following when I type the last }

void MyMethod()
{
}

However, when I write:

bool MyProperty { get; set; }

I want it to stay that way instead of getting changed to:

bool MyProperty 
{ 
    get; 
    set; 
}

Is there anyway that I can get this to happen or am I dreaming? The only way I can think of is turning those two settings off....but that just results in the annoyance getting shifted to when I write methods and if statements etc.

Update: Just thought I'd mention that I use Resharper...so if there's a way to accomplish it through there I would be interested in knowing that too.

From stackoverflow
  • ReSharper might have such feature, though I'm not absolutely sure.

    mezoid : I do use Resharper too...so even suggestions for doing it that way would help
    Anton Gogolev : In VS, ReSharper menu item - Options - C# Formatting Style in the leftmost treeview, see what it's got there.
  • Are you sure this is a problem in VS2008? Because my auto properties stay put...

    given...

    bool MyProperty { get; set; }
    

    it stays as ...

    bool MyProperty { get; set; }
    

    Even after I apply the format selection command.

    Note: I have the same VS formatting options selected as you. Though I am using VS2008 SP1.

    Ray : Mine stay put too
    mezoid : I'm using VS2008 too... but I'm also using Resharper which seems to be part of the problem.
  • Awesome! I managed to figure it out. Thanks to Anton I had a closer look at the settings available in Resharper and I was able to figure something out.

    My solution involves a combination of settings in Resharper and VS2008.

    In VS2008, I turn off Automatically format completed statement on ;

    I then turn on the setting in Resharper for IntelliSense Completion Behavior to Automatically insert parentheses after completion.

    This allows me to type:

    bool MyProperty {
    

    To get:

    bool MyProperty {}
    

    Which then allows me to add the get; and set; lines without them formatting...

0 comments:

Post a Comment