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?

Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
Paolo Crociati
  • 483
  • 2
  • 9
  • 21
  • 1
    [Possibly related bug report](https://connect.microsoft.com/VisualStudio/feedback/details/1035813/connect-vb-14-compiler-removes-line-continuations-even-when-web-config-specifies-vb-8-as-compiler) and [subsequent fix](https://github.com/dotnet/roslyn/pull/3294) - if you specifically target the old version does it keep the original syntax? – James Thorpe Aug 07 '15 at 10:12
  • 1
    Ah, the fix is marked "approved for next preview" - perhaps it's still not in. – James Thorpe Aug 07 '15 at 10:14
  • A quick look on [Roslyn's site](https://github.com/dotnet/roslyn) doesn't indicate their release schedule. Anyone determine/know when the fix will be available for mass consumption? – wags1999 Sep 30 '15 at 21:13
  • Did you find a fix for this? – mathiasfk Jun 04 '16 at 15:15

3 Answers3

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)

RiptoR
  • 466
  • 7
  • 5
  • Please add a reference to backup the statement that "the Roslyn team says they fixed this" – Joseph Rosson Aug 22 '16 at 19:39
  • 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? – RiptoR Aug 24 '16 at 17:05
  • This was disabled by default for me but I still have the same problem. Any other ideas? – Schoof Dec 20 '16 at 09:24
  • 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? – RiptoR Jan 11 '17 at 12:27
  • 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. – mlhDev Apr 17 '17 at 21:00
  • 3
    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. – David Carta May 01 '18 at 15:36
  • FWIW, it seems they made this "feature" extra tough in VS2019. It's still there, the answer here still fixes it. – JulieC Oct 18 '21 at 19:34
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

-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.

DeveloperDan
  • 4,626
  • 9
  • 40
  • 65
  • 2
    My comment from [here](https://stackoverflow.com/questions/50030662/how-to-prevent-visual-studio-2017-from-removing-underscore-in-vb-code-with-c#comment106470311_60194874): 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? – Lance U. Matthews Feb 12 '20 at 19:15