Defining Multiple Statements on a Single Line

Sometimes it is convenient to define multiple code statements on a single line of code within the editor. For example, assume you have a set of local variables that need to be assigned to initial values. While you could assign a value to each variable on discrete lines of code:

Sub MyMethod() Dim s As String Dim i As Integer s = "Fred" i = 10 End Sub you can compact the scope of this subroutine using the colon character:

Sub MyMethod() Dim s As String Dim i As Integer s = "Fred" : i = 10 End Sub

Understand that misuse of the colon can easily result in hard-to-read code. As well, when combined with the statement continuation character, you can end up with nasty statements such as the following:

Sub MyMethod() Dim s As String : Dim i As Integer s = "Fred" _ : i = 10 End Sub

To be sure, defining multiple statements on a single line using the colon character should be used sparingly. For the most part, this language feature is most useful when you need to make simple assignments to multiple variables.

Was this article helpful?

0 0

Post a comment