Array Class Collections File String Console Data Directive Enum Exception If Interface Lambda Loop Nothing Number Process Property Regex Sort StringBuilder Structure Sub Time Windows
Sub procedures return no values. We implement and call a Sub procedure. Subs optionally have argument lists and formal parameters. And these formal 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.
Result:This program uses Console.WriteLine and 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
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.
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.
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.Function
Note:In other terminology, a Sub is equivalent to a void method in C-like languages such as the C# language.
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.Shared
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.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.With
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.Const
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. Console.WriteLine(value) End Sub End Module Output 15
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.
XML comments:These are used by Visual Studio to help programmers navigate and discover your code.
And the simplest comment form starts with a single apostrophe. This is probably the most common syntax for comments. It requires the least amount of effort to add this kind of comment.
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.
The first program uses a statement on a single line. The statement uses generic types. It is so long that I had to omit it for the code example. It would be hard to edit in Visual Studio.
Next: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.
Also:The level of indentation on a new line doesn't matter to the compiler. We have more freedom here.
We saw the Sub procedure syntax in an example program that uses a custom method. We noted some of the syntax you can use when coding. This includes line continuations and XML comments on the method signature.
Review:Subroutines return no value.
Functions always return a value in normal execution.