Array Class Collections File String Cast Console Data Directive Enum Exception If Interface Lambda LINQ Loop Nothing Number Process Property Regex Sort StringBuilder Structure Sub Time Windows


A Sub procedure returns no value. We implement and call this kind of method. Subs optionally have argument lists and formal parameters. These parameters can be separated with line continuations for readability.


We declare and call a Sub procedure in the VB.NET language, using different syntaxes for calling the procedure. The program also shows XML comments. Subroutines in VB.NET return no value to the caller.

Main method

Result:This program uses Console.WriteLine and prints ten numbers to the console screen.

Tip:Please notice how the underscore character is used. It allows a line break to occur in a Sub call.

Based on:

.NET 4.0

Program that uses Sub procedure: VB.NET

Module Module1

    Sub Main()
	' Call the Sub Procedure, which returns no value.
	' ... You can put all the arguments on one line.
	WriteArguments(1, 2, 3, 4, 5)
	' Call the Sub Procedure again.
	' ... You can use the line continuation marker to break up the lines.
	WriteArguments(1000, _
	    2000, _
	    3000, _
	    4000, _
    End Sub

    ''' <summary>
    ''' Prints out the five arguments.
    ''' </summary>
    ''' <param name="param1">Description 1.</param>
    ''' <param name="param2">Description 2.</param>
    ''' <param name="param3">Description 3.</param>
    ''' <param name="param4">Description 4.</param>
    ''' <param name="param5">Description 5.</param>
    ''' <remarks>Made by Dot Net Perls.</remarks>
    Sub WriteArguments(ByVal param1 As Integer, _
		       ByVal param2 As Integer, _
		       ByVal param3 As Integer, _
		       ByVal param4 As Integer, _
		       ByVal param5 As Integer)
    End Sub

End Module



We see a custom method called WriteArguments. The Main method body first calls the WriteArguments subroutine using a single line of parameters. The WriteArguments sub prints five integers on separate lines.

Next, we can separate a long line when calling or declaring a function. We use the underscore at the end of the line as a line continuation. This sequence indicates the line break can be safely ignored by the language processor.

Note:This syntax does not affect the instruction-level code or intermediate language.

Extensible markup language: XML

XML comments. To add an XML-based comment to a function, type three single-quote characters. Then, insert the text inside the XML tags that appear. XML comments are used in Visual Studio's interface.


Return keyword

Let us contrast the Function type to the Sub type. The Sub type does not return a value to its caller. It uses a Return statement with no variable. The Function type returns a value to its caller.


Note:In other terminology, a Sub is equivalent to a void method in C-like languages such as the C# language.

ByVal, ByRef

ByVal and ByRef operators

What is the difference between ByVal and ByRef in argument passing? An Integer passed ByVal can be changed in a function—and this change is not reflected anywhere else. An Integer passed ByRef will be changed outside of the function.

ByVal, ByRef



An extension method is not an instance method. But it is called with the same syntax as one. We create an extension method with the <Extension()> syntax. A separate module must also be used.

Extension Method


A Function or Sub can be Shared throughout all instances of the enclosing Class. An instance reference is not needed to call a Shared Function. The Shared keyword also influences performance.



Collection abstraction: squares

When a Function calls itself, recursion occurs. The VB.NET language supports recursion. The ByRef keyword can be useful when implementing recursive algorithms. We show how to use recursion.


Tip:Recursion is used to solve complex problems or puzzles. It often is not the most efficient approach to simpler problems.



The VB.NET language provides some syntactic sugar that can be used inside subroutines and functions. One example is the With statement. This reduces the typing necessary in some contexts.


However:I found that "with" carries no performance benefits. It is syntactic sugar.


Const keyword

A Const value is used with syntax similar to a field or local variable. But it cannot be reassigned. It can only be initialized. It promotes self-documenting code. It improves performance over using fields or arguments.




There are several ways to add comments. A comment is best written at the level of intention. You explain what the code is meant to accomplish. Comments that simply translate the code to English are less effective.

But:Comments sometimes help emphasize certain aspects of programs. If a computation is important, a comment makes it more prominent.

Program that uses comments: VB.NET

Module Module1
    REM This is Module1.

    ''' <summary>
    ''' This is an XML comment.
    ''' </summary>
    ''' <remarks>Called at program start.</remarks>
    Sub Main()
	REM Get square root of 225.
	Dim value As Double = Math.Sqrt(225)

	' Print value. (Other comment syntax.)
	' ... Sometimes it helps to indent comments.
    End Sub
End Module



In this program we use three comma syntax forms. The REM statement stands for an explanatory remark. All characters following REM are not code statements. They are ignored during compilation.

The other syntax forms start with the apostrophe character. When three apostrophes are at the start of the comment, this is an XML comment. Certain special XML tags can be added to it.

Visual Studio logo

XML comments:These are used by Visual Studio to help programmers navigate and discover your code.

Finally:The simplest comment form starts with a single apostrophe. This is probably the easiest syntax for comments.



This language does not allow line breaks everywhere. How can we continue a VB.NET statement on the next line? In VB.NET a newline signals the end of a statement. This can be changed with the underscore character.

First:The first program uses a statement on a single line.
This statement uses generic types.

Second:The second program uses the underscore syntax at the end of lines. This means the statement can continue on the next line.

Program with long statement: VB.NET

Module Module1
    Sub Main()
	Dim longerVariableName As Dictionary(Of String, KeyValuePair(Of Boolean, ....
	longerVariableName("a") = Nothing
    End Sub
End Module

Program with statement on multiple lines: VB.NET

Module Module1
    Sub Main()
	Dim longerVariableName _
	    As Dictionary(Of String, KeyValuePair(Of Boolean, Integer)) = _
	    New Dictionary(Of String, KeyValuePair(Of Boolean, Integer))(5000)
	longerVariableName("a") = Nothing
    End Sub
End Module

In Visual Studio, continued statements typically are indented four spaces, or at the most recent parenthesis. In long statements, just use four spaces at the start instead of letting the multiple-line statement become too wide.

Tip:You must leave a space character before the underscore character. Otherwise a compiler error occurs.

Tip 2:The level of indentation on a new line doesn't matter to the compiler. We have more freedom here.


The VB.NET programming language

The Sub procedure syntax is often used in VB.NET programs. Its syntax is straightforward and clear. We used line continuations and XML comments in our Sub methods. Subroutines (Subs) return no value.