21

(Descriptions are inside the code samples)

Take a look at this code

Public Sub Show()
    System.Console.WriteLine("Hello")   ' Comment here will fail subsequencing syntax highlighting
    System.Console.WriteLine("This should not be rendered as a comment")
End Sub

Compare to this one

Public Sub Show()
    System.Console.WriteLine("Hello")   ' Ending quote solve the problem for now --> '
    System.Console.WriteLine("This is not rendered as a comment")
    System.Console.WriteLine("Ending quote solve the problem for now")
End Sub
3
  • feature request related to this meta.stackexchange.com/questions/4813 Commented Jul 18, 2009 at 5:29
  • Unless the issue is to be resolved very soon. Some body should tag #4813 with VB.NET
    – Sake
    Commented Jul 18, 2009 at 5:49
  • This is also relevant for vbscript
    – Motti
    Commented Jul 23, 2010 at 11:24

3 Answers 3

16

Until a better solution is found, there are twothree work-arounds:

The correct way to do this now is with a language hint above the code sample, like so:

<!-- language: lang-vb -->

    Public Sub VisualBasicCodeHere()
       'This will be highlighted correctly when used in a real post
    End Sub

Before the language hints were available, you had to use one of the following work-arounds:

  • Put two apostrophes followed by a hash (#) at the front of the comment. This works just like the // method below, but it saves one character and the result look less like a bastardized attempt at C#.

    ''# You're commenting!
    Dim c As New Comment()
    
  • Put an extra apostrophe at the end, plus add more if needed to account for appostrophes in the comment text itself. It's nice because the comment stands out consistently, but not as nice because the comment is still colored like a string literal and you need to watch for the extra apostrophes.

    'You''re commenting!'
    Dim c As New Comment()
    
  • Put two apostrophes followed by two forward slashes at the front of the comment. It's nice because you don't have to worry about extra apostrophes in the comment and because the comment is colored correctly, but bad because it adds a bunch of extra junk to the front that's harder to clean out.

    ''// You're commenting!
    Dim c As New Comment()
    

A lot of old, and even some new, posts still have these tricks to try to get the highlighting right, so it's worth keeping the list around, but anything new you see and any time you edit, the language hint is the way to go.

1
  • +1 for sharing these workarounds, especially the {''#} method
    – Dubs
    Commented May 18, 2010 at 15:48
1

Based on Google Prettify

http://code.google.com/p/google-code-prettify/

I believe there's a suggestion out there somewhere to support the optional language-specific packs that can be used with prettify (essentially overriding the default detection method)

We just deployed the latest trunk of prettify.js ; revision 83

http://code.google.com/p/google-code-prettify/source/browse/trunk/src/prettify.js

2
  • 3
    Yes, support the optional language-specific packs! The guess-method doesn't work.
    – Svante
    Commented Oct 20, 2009 at 20:16
  • 1
    Adding the language packs doesn't override the default detection method, it just gives it more options to choose from. Commented Dec 19, 2009 at 12:25
0

Possible solution: Since most questions are tagged for a specific language. Isn't it possible to use those tags as a hint for the highlighter? In most cases this will suffice.

In some cases there may be some trouble like tags 'mysql' and 'php' on one question, or "Please help me translate this language 'X' code into language 'Y'". But even then, the highlighter should be smart enough to find out which of those two or three languages it is.

I found this question not because of VB, but because of Delphi, which has similar problems and is also always highlighted wrong, especially since it uses { } for block comments. :)

1
  • That already happens. If the language hint for a given tag is missing or incorrect, that can be addressed by requesting it here on Meta. I'm unfamiliar with what support Prettify has for Delphi, though.
    – Tim Stone
    Commented Jan 11, 2012 at 22:26

Not the answer you're looking for? Browse other questions tagged .