In VB.NET, Subs and Functions contain behavior. A Sub
procedure returns no value. Functions return a value, often based on a variable.
Procedures optionally have argument lists and formal parameters. A parameter influence a procedure's behavior. Parameters can be passed ByVal
or ByRef
.
We declare and call a Sub
procedure. We use different syntaxes to call the procedure. The program also shows XML comments. Subroutines return no value to the caller.
Main
Sub
first calls the WriteArguments
subroutine with a single line of arguments.WriteArguments
sub prints 5 integers on separate lines. Console.WriteLine
, too, is a Sub
.Console.WriteLine
and prints ten numbers to the console screen.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 Module1 2 3 4 5 1000 2000 3000 4000 5000
To add an XML-based comment to a function, type three single-quote characters. Then insert the text inside the XML tags that appear.
interface
. They make large programs easier to develop.Module Module1 ''' <summary> ''' Type 3 apostrophes on top of a Function. ''' </summary> ''' <returns></returns> Function Example() As Integer Return 100 End Function Sub Main() End Sub End Module
Dim
, AsWe use the Dim
and As keywords within a method body. Dim
begins a variable declaration. And with As we specify that variable's type.
Module Module1 Sub Main() ' The Dim keyword is used to declare a variable. ' ... With As we specify its type. Dim value As Integer = 100 Console.WriteLine(value) End Sub End Module100
Let us contrast the Function keyword to the Sub
keyword. The Sub
does not return a value to its caller. But a Function returns a value.
Module Module1 Function MultiplyByTwo(ByVal test As Integer) As Integer Return test * 2 End Function Sub Main() ' Call the Function with an argument of 5. Console.WriteLine(MultiplyByTwo(5)) End Sub End Module10
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.
Class Utility ''' <summary> ''' Displays a helpful message. ''' </summary> Public Shared Sub Message() Console.WriteLine("Hello world") End Sub End Class Module Module1 Sub Main() ' We do not use a Utility instance to call the shared Sub. Utility.Message() End Sub End ModuleHello world
A comment is best written at the level of intention. We explain what the code is meant to accomplish. If a computation is important, a comment makes it more prominent.
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 Module15
This language does not allow line breaks everywhere. A newline signals the end of a statement. But an underscore allows a statement to span many lines.
Module Module1 Sub Main() Dim longerVariableName As Dictionary(Of String, KeyValuePair(Of Boolean, .... longerVariableName("a") = Nothing End Sub End ModuleModule 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
AddressOf
This operator allows us to reference a Function or Sub
by its name. We can then pass the result to another Function. Here, Array.FindIndex
requires a Predicate
argument.
AddressOf
IsMatch
syntax returns a suitable function as a Predicate
instance.AddressOf
we can change a Function or Sub
name into an argument, a variable, that we can pass to other procedures.Module Module1 Function IsMatch(ByVal item As String) As Boolean Return item.Length = 4 End Function Sub Main() Dim items() As String = {"cat", "apple", "baby"} ' Use AddressOf to specify IsMatch as the Predicate. Dim index As Integer = Array.FindIndex(items, AddressOf IsMatch) Console.WriteLine(index) End Sub End Module2
The Sub
procedure syntax is often used in VB.NET programs. Subroutines (Subs) return no value. We compared Subs with Functions.