C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
WriteArguments: The Main Sub first calls the WriteArguments subroutine with a single line of arguments.
Note: The WriteArguments sub prints 5 integers on separate lines. Console.WriteLine, too, is a Sub.
Underscore: We can separate a long line when calling or declaring a function. We use an underscore as a line continuation.
Result: This program uses Console.WriteLine and prints ten numbers to the console screen.
ConsoleVB.NET program that uses Sub procedure
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 The Dev Codes.</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
Info: XML comments are used in Visual Studio's interface. They make large programs easier to develop.
VB.NET program that shows XML comments
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: Here we declare a variable with the identifier (name) of "value." We assign it initially to 100.
As: This lets us specify the type of the variable. Sometimes this keyword is not required—the compiler can determine the types itself.
VB.NET program that uses Dim, As for local
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 Module
Output
100
VB.NET program that uses Function
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 Module
Output
10
VB.NET program that calls Shared Sub
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 Module
Output
Hello world
REM: This stands for an explanatory remark. All characters following REM are not code statements.
XML comments: When 3 apostrophes are at the start of the comment, this is an XML comment. These are parsed by Visual Studio.
Apostrophe: The simplest comment form starts with a single apostrophe. This is probably the easiest syntax for comments.
VB.NET program that uses comments
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
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.
VB.NET program that uses long statement
Module Module1
Sub Main()
Dim longerVariableName As Dictionary(Of String, KeyValuePair(Of Boolean, ....
longerVariableName("a") = Nothing
End Sub
End Module
VB.NET program that uses with statement on multiple lines
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
Note: The AddressOf IsMatch syntax returns a suitable function as a Predicate instance.
Note 2: With AddressOf we can change a Function or Sub name into an argument, a variable, that we can pass to other procedures.
VB.NET program that uses AddressOf
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 Module
Output
2