locked
C# and assignment operator code formatting

    Question

  • When I write code, I usually line up all the "=" operators and I like it that way.
    However, the VS2005 wouldn't let me keep the "=" the way they are.
    It will automatically remove the additional whitespaces it has on either side of the "=" operator.
    Is there a setting that I could turn off to tell the VS to not format my "=" operator?

    This is what I like.

    int myInteger1          = 1234;
    string myStringVariable = "Testing 1234";
    MyObject obj            = new MyObject(1234, "TEsting");

     

    And I hate this.

    int myInteger1 = 1234;
    string myStringVariable = "Testing 1234";
    MyObject obj = new MyObject(1234, "TEsting");

    Friday, March 17, 2006 1:47 PM

Answers

  • Soe,

    This is something that we've heard feedback on before and will likely address in a future version.  At the moment, your option is to disable automatic formatting when you type semicolon and close brace.  This will allow you to keep your locals aligned as you have in your post; however, it will also require explicitly invoking formatting on selections.  It's not a great story unfortunately.  You can track progress on the issue via this MSDN Feedback bug.

    By the way, it's very useful that you posted the way you would like your code to be formatted.  That helps a lot when we're defining the set of options that the formatting engine will understand.  Thank you!

    Anson Horton
    C# IDE PM

    Friday, March 17, 2006 8:06 PM
  • I found something that "fixes" this problem.

    1) Tools --> Options
    2) Text Editor --> C# --> Formatting --> Spacing --> Set spacing for operators --> "Ignore spaces around binary operators" radio button checked.

    It doesn't fix the spacing for my "=" operators anymore.
    The ONLY thing that we will have to worry is the other binary operators like + - etc now.

    But we programmers usually keep those binary expressions fairly clean anyway.
    So, selecting this option doesn't really effect us much.

    I hope this partial solution I found is a great help for others.

    Cheers,
    Soe

    Friday, March 17, 2006 9:25 PM

All replies

  • Soe,

    This is something that we've heard feedback on before and will likely address in a future version.  At the moment, your option is to disable automatic formatting when you type semicolon and close brace.  This will allow you to keep your locals aligned as you have in your post; however, it will also require explicitly invoking formatting on selections.  It's not a great story unfortunately.  You can track progress on the issue via this MSDN Feedback bug.

    By the way, it's very useful that you posted the way you would like your code to be formatted.  That helps a lot when we're defining the set of options that the formatting engine will understand.  Thank you!

    Anson Horton
    C# IDE PM

    Friday, March 17, 2006 8:06 PM
  • I found something that "fixes" this problem.

    1) Tools --> Options
    2) Text Editor --> C# --> Formatting --> Spacing --> Set spacing for operators --> "Ignore spaces around binary operators" radio button checked.

    It doesn't fix the spacing for my "=" operators anymore.
    The ONLY thing that we will have to worry is the other binary operators like + - etc now.

    But we programmers usually keep those binary expressions fairly clean anyway.
    So, selecting this option doesn't really effect us much.

    I hope this partial solution I found is a great help for others.

    Cheers,
    Soe

    Friday, March 17, 2006 9:25 PM
  •  

    Hi Anson

     

    There is another spacing option that could be improved.  It's where you get a sequence of closing or even opening brackets (braces). 

    If you are calling a method, within a method, within a method, you can often get a run of say 4 brace symbols in a row.  Although the space adds legibility to the start of the method calls, when you get ) ) ) ) at the other end, I'd like to be able to say, "remove spaces between closing braces".  And even "...opening braces" as options.  With fixed font editing, the spaces make the statements longer than needed IMO.

     

    decimal euros =( ECurrency.Euro )Currency.Convert( myCurrency, TicketClass.GetCost( Flight.GetMileage( Airport.Nearest( myDestCity.AsGPSCoord ), AirPort.Nearest( myStartCity.AsGPSCoord ) ) ) );

     

    would become...

     

    decimal euros =( ECurrency.Euro )Currency.Convert( myCurrency, TicketClass.GetCost( Flight.GetMileage( Airport.Nearest( myDestCity.AsGPSCoord ), AirPort.Nearest( myStartCity.AsGPSCoord ))));

     

    Thanks.

    Friday, July 06, 2007 10:44 AM