VB.NET Sub Procedure Example

Sub keyword

Sub procedures return no values. You are interested in how you can use the VB.NET language to declare, implement and call a Sub procedure, which is a function that returns no value. Sub procedures can have different formal parameters, and these formal parameters can be separated with line continuations for a more readable source text.

This VB example program shows the Sub keyword. Sub stands for subroutine.

Example

First, this example program text demonstrates how you can declare and call a Sub procedure in the VB.NET language, using different syntaxes for calling the procedure. The program also demonstrates XML comments in the VB.NET language. Subroutines in the VB.NET language do not return a value to the caller. This program prints ten numbers to the console screen.

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, _
	    5000)
    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)
	Console.WriteLine(param1)
	Console.WriteLine(param2)
	Console.WriteLine(param3)
	Console.WriteLine(param4)
	Console.WriteLine(param5)
    End Sub

End Module

Output

1
2
3
4
5
1000
2000
3000
4000
5000

Calling Sub procedure defined in Module. This example program introduces one custom method called WriteArguments, which is invoked from the Main entry point. The Main method body first calls the WriteArguments subroutine using a single line of parameters. The WriteArguments subroutine prints five integers on separate lines.

Using line continuations to break up long lines. The program next shows how you can separate a long line in the VB.NET language when calling or declaring a function. In some programs, function types have many parameters and you can use the "space _" syntax at the end of the line as a line continuation. This sequence indicates that the line break can be safely ignored by the language processor. This syntax does not affect the instruction-level code or intermediate language.

Extensible markup language (XML)

XML comments. This example program also demonstrates the XML comment syntax that is available in the .NET Framework languages. To add an XML-based comment to a function in the VB.NET language, please type three ' characters, and then insert the text inside the XML tags that appear automatically.

Functions

Let's contrast the Function type in the VB.NET language to the Sub type. The Sub type, as shown in this example program text, does not return a value to its caller. The Function type returns a value to its caller. In other terminology, a Sub is equivalent to a void method in C-like languages such as the C# language.

Summary

The VB.NET programming language

Here we looked at the Sub procedure syntax in the VB.NET language in an example program that uses a custom method. We noted some of the syntax tricks you can use when coding in the VB.NET language, including line continuations and XML comments on the method signature. Subroutines return no value. Functions always return a value in normal execution.

VB.NET Tutorials
.NET