Implicit Line Continuation in Visual Basic 10

Posted: April 6th, 2010 | Author: | Filed under: Uncategorized | No Comments »

In C# you end a statement of code with a semicolon. If that statement spans multiple lines there is no worry because the compiler knows it is one statement until it sees the semicolon. This is not the case with VB. VB assumes that each statement is on one line of code. If your line of code is too long then you could use the underscore character to tell the compiler that the line of code continues onto the next line. In VB 10 the compiler is smarter and it tries to figure out where your statements end so you don’t have to worry about the underscore in all cases. There will still be instances where you have to use the underscore though. In my opinion, Visual Basic code looks less elegant than C# code. This may sound like a small feature, however, it is a step in the right direction of making VB code more readable and simple.

Here is an example of a piece of code using VB 9.  Notice the serialization attributes.  Those attributes are technically the same line as the class line and property line.  Because of this you had to add an underscore.

Imports System.Xml
Imports System.Xml.Serialization

Namespace BusinessObjects
 _
Public Class File

 _
Dim _path As String

Public Sub New()

End Sub

Public Sub New(ByVal path As String)
_path = path
End Sub

Public Property Path()
Get
Return _path
End Get
Set(ByVal value)
_path = value
End Set
End Property
End Class
End Namespace

Here is an example in VB 10 that compiles.   Notice that you don’t need the underscore.  I gave you a rather simple example, but you can imagine that in a larger class it was possible for it to look very messy in VB 9.

Imports System.Xml
Imports System.Xml.Serialization

Namespace BusinessObjects

Public Class File

Public Property Path As String

Public Sub New()

End Sub

Public Sub New(ByVal path As String)
_Path = path
End Sub

End Class
End Namespace


Leave a Reply