Rowan Freeman
Rowan Freeman

Reputation: 16358

How do I format my VB.NET methods to look more like C# methods?

C#

Too long

public void AcceptableFunctionName(string variable, int anotherVariable, object variableThree)
{

}

Acceptable

public void AcceptableFunctionName(
    string variable, int anotherVariable, object variableThree)
{

}

Alternative

public void AcceptableFunctionName(
    string variable,
    int anotherVariable,
    object variableThree)
{

}

VB.NET

Too long

Public Sub AcceptableFunctionName(variable As String, anotherVariable As Integer, variableThree As Object)

End Sub

What?

Public Sub AcceptableFunctionName(
                                 variable As String, anotherVariable As Integer, variableThree As Object)

End Sub

What?

Public Sub AcceptableFunctionName(
                                 variable As String,
                                 anotherVariable As Integer,
                                 variableThree As Object)

End Sub

Question

How do I make it so Visual Studio will automatically format my VB.NET methods better?

Acceptable

Public Sub AcceptableFunctionName(
    variable As String, anotherVariable As Integer, variableThree As Object)

End Sub

I've tried

Tools -> Options -> Text Editor -> Basic -> Tabs -> Indenting: None, Block, Smart

None

    Public Sub AcceptableFunctionName(
variable As String, anotherVariable As Integer, variableThree As Object)

    End Sub

Block

Public Sub AcceptableFunctionName(
variable As String, anotherVariable As Integer, variableThree As Object)

End Sub

Upvotes: 0

Views: 152

Answers (3)

Konrad Rudolph
Konrad Rudolph

Reputation: 545885

VB has its own conventions. Don’t import those from other programming languages, that will lead to inconsistencies with other code bases.

Instead, embrace VB’s style. In fact, you rightly observe that the result is weird when you try breaking before the first argument. But if you do it after, everything makes sense:

Public Sub AcceptableFunctionName(variable As String,
                                  anotherVariable As Integer,
                                  variableThree As Object)
    ' …
End Sub

You will observe that this schema of indentation is consistently supported by the IDE, in particular also in method calls and LINQ expressions.

Now, as far as personal preferences go, I also prefer C#’s convention of indenting all subsequent lines by a single indent width but here we go.

Upvotes: 3

Hans Passant
Hans Passant

Reputation: 942020

Line endings are important in VB.NET, they are the statement terminators. The equivalent of the semi-colon ; in C#. Using white space as a syntactic element is not unusual, Python is another example. You cannot expect the IDE to insert line breaks for you, that will change the meaning of the program. You have to use the line continuation character, an underscore _.

Work was done in VB10 (VS2010 version) that made the use of the underscore optional. A feature called "Implicit Line Continuation". You cannot arbitrarily skip the underscore, you have to break the line in the right place. It is documented well in this MSDN page, scroll down to the Implicit Line Continuation section.

Just above that you'll see the underscore usage documented. Above that you'll see how to put more than one statement on a single line, using the : character.

Do avoid assuming that VB.NET is similar to C#, VB.NET syntax rules are fundamentally different from the way you write code in the curly-brace languages.

Upvotes: 2

SysDragon
SysDragon

Reputation: 9888

In Vb.Net, when you want something to continue on another line use _:

Public Sub AcceptableFunctionName( _
variable As String, _
anotherVariable As Integer, _
variableThree As Object)
    '...
End Sub

Same for inside the code:

Dim s As String = "This is my " & _
                       "line"

Notice that you can't use comments inside multiline blocks.
You can read this MSDN Documentation about it. This is useful too.


EDIT:

Looks like, since Visual Studio 2010, the compiler automatically accepts some line breaks implicitly without the _ character. See Implicit Line Continuation from MSDN.

Upvotes: 1

Related Questions