Prevent Visual Studio 2015 from removing line continuation characters (_) in VB.NET files
Asked Answered
B

3

24

I'm opening some old VB.NET projects in Visual Studio 2015 and when I edit the code, VS changes the syntax:

It removes "_" in concatenations:

'Before
myString = "ABC" & _
           "DEF"

'After
myString = "ABC" & 
           "DEF"

or add a space before !:

'Before
myDatatable.Rows(0)!myColumn

'After
myDatatable.Rows(0) !myColumn

This syntax isn't compatible with Visual Studio 2010 or 2013.

How can I disable this changes?

Bituminous answered 7/8, 2015 at 9:40 Comment(4)
Possibly related bug report and subsequent fix - if you specifically target the old version does it keep the original syntax?Stereophonic
Ah, the fix is marked "approved for next preview" - perhaps it's still not in.Stereophonic
A quick look on Roslyn's site doesn't indicate their release schedule. Anyone determine/know when the fix will be available for mass consumption?Backus
Did you find a fix for this?Koto
B
40

I had the same problem, and I was able to fix it by disabling the "Pretty listing" option in the editor. You can find this option here:

Tools > Options > Text Editor > Basic > Advanced > Editor Help > Pretty listing (reformatting) of code

I'm not sure what other auto-reformatting this option disables, but at least the editor stopped removing the line continuation characters in old code/projects.

PS: While the Roslyn team says they fixed this (see links below), this bug is still present in the latest version of Visual Studio 2015.

edit Link to bug report - Link to merged fix (copied from first comment on original question)

Biquadrate answered 28/7, 2016 at 13:30 Comment(7)
Please add a reference to backup the statement that "the Roslyn team says they fixed this"Schmitt
Edited my answer to copy-paste the links mentioned in the comment of the original question. Also, is this really a good reason to downvote a possible fix?Biquadrate
This was disabled by default for me but I still have the same problem. Any other ideas?Yokum
Weird, this should work (still does on my system). This may be a long shot, but have you tried enabling the option, applying the change, and then disabling it again?Biquadrate
This fixes the auto reformatting, but just a reminder if you explicitly reformat (Edit > Advanced > Format * or the equivalent keyboard shortcut) you will lose the endings again.Devolve
We have code that we use still deploy in VS2008, so the underscore is unfortunately still required. However, if you like the "Pretty Listing" feature, you can keep it on and just CTRL-Z to undo the removal of the underscores right after VS2015+ "fixes" it for you.Grape
FWIW, it seems they made this "feature" extra tough in VS2019. It's still there, the answer here still fixes it.Stockinet
L
2

The official way to address this is modifying the .vbproj file to include

<PropertyGroup>
   <LangVersion>latest</LangVersion>
</PropertyGroup>

10 is for VS2010 as described at https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/configure-language-version

Linked answered 7/4, 2020 at 18:36 Comment(0)
O
-1

Just CTRL-Z to undo the removal of the underscores right after Visual Studio (2015-19) "fixes" it for you. This leaves the "Pretty Listing" feature turned on but restores the missing underscores. Thanks David Carta for the answer left as a comment.

Overuse answered 12/2, 2020 at 18:29 Comment(1)
My comment from here: I don't know how prior versions may differ, but Visual Studio 2019 removes the underscores on every save, whenever the code editor pane loses focus (e.g. opening a menu or the project/type/member navigation bar), or even just upon moving the cursor outside the code with the underscores or right-clicking anywhere. Does it really seem feasible to have to perform an undo after you do pretty much anything in the editor?Apoenzyme

© 2022 - 2024 — McMap. All rights reserved.