user81993
user81993

Reputation: 6613

Is there an opposite of the underscore( _ ) line continuation?

The underscore allows me to do things like this

    Public Sub derp _
        (x As Integer)
        MsgBox(x)
    End Sub

Is there any opposite notation for this? For example, if it was ¯ then I could do

Public Sub derp(x as Integer) ¯ Msgbox(x) ¯ End Sub

Upvotes: 5

Views: 12844

Answers (2)

Han
Han

Reputation: 3072

You can try using a colon. But you can't put the function/sub body in the same line as the declaration of the function/sub.

Public Sub derp(x As Integer)
    MsgBox(x) : MsgBox("Hello, world") : End Sub

You can also try using an action delegate. But it can only have 1 statement if you want to put them in 1 line.

Public herp As Action(Of Integer) = Sub(x) MsgBox(x)

If you want to have multiple line, you write it like this (you can use colons, if you want):

Public herp As Action(Of Integer) = Sub(x)
                                        MsgBox(x)
                                        MsgBox("Hello, world")
                                    End Sub

Use Func delegate if you want to return a value instead of Action delegate.

Upvotes: 1

Cody Gray
Cody Gray

Reputation: 244782

Sure—the colon:

Public Sub derp(x as Integer) : MsgBox(x) : End Sub

But don't abuse this. The compiler doesn't charge by the line.

About the only time I use it is when I'm establishing an inheritance relationship:

Public Class Rectangle : Inherits Shape
   ...
End Class

Somehow, that just seems more logical to me than putting the Inherits in the class body. And you can't even blame it on my C++ roots, because VB was my first language.

Upvotes: 0

Related Questions