简体   繁体   中英

How do I put comments on each line in VB.NET?

I used to be a C# developer so this commenting style was very easy in C#. This is driving me crazy but how do you do this in VB.NET without getting a syntax error?:

Private ReadOnly Property AcceptableDataFormat(ByVal e As System.Windows.Forms.DragEventArgs) As Boolean
    Get
        Return e.Data.GetDataPresent(DataFormats.FileDrop) _          'this is a file that a user might manipulate
                OrElse e.Data.GetDataPresent("FileGroupDescriptor")   'this is a typical Outlook attachment
    End Get
End Property

Unfortunately you can't. VB.Net doesn't allow comments to appear in the middle of statements which span multiple lines (including both explicit and implicit line continuations). The comments must go above the statement or on it's final line.

The exception to this rule is statement lambdas. It is fine for comments to appear within a statement lambda even though it's technically one statement.

Remove that comment from the line continuation and it should be fine. Comments cannot be used with the line continuation. The line continuation has to be the last character on the line so your comment cannot appear after it.

Here is a similar question: Why must VB.Net line continuation character be the last one on line

So like this:

 Private ReadOnly Property AcceptableDataFormat(ByVal e As System.Windows.Forms.DragEventArgs) As Boolean
        Get
            'Return a file that a user has dragged from the file system 
            'or a typical Outlook attachment
            Return e.Data.GetDataPresent(DataFormats.FileDrop) OrElse   
                   e.Data.GetDataPresent("FileGroupDescriptor")         

        End Get
    End Property

This is a limitation that has been bothering VB developers for a long time.

Apparently this is now possible in Visual Studio 2017

Public Function foobarbaz(ByVal foo As Integer,   '' qux
                          ByVal bar As String,    '' quux
                          ByVal baz As DateTime)  '' garply

  Return foo.ToString &         '' Concatenate foo
         bar &                  '' with bar
         baz.ToString           '' and baz

End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM