This appendix provides information about subroutine, function, and generic declarations. A property procedure includes a subroutine and function pair, so they are also described here.
The syntax for writing a subroutine is as follows:
[attribute_list
] [interitance_mode
] [accessibility
] Subsubroutine_name
[(parameters
)] [Implements interface.procedure
] [statements
] End Sub
The inheritance_mode can be one of the following values: Overloads, Overrides, Overridable, NotOverridable, MustOverride, Shadows, or Shared. These values determine how a subroutine declared within a class inherits from the parent class or how it allows inheritance in derived classes.
The accessibility clause can take one of the following values: Public, Protected, Friend, Protected Friend, or Private. These values determine which pieces of code can invoke the subroutine.
The syntax for writing a function is as follows:
[attribute_list
] [interitance_mode
] [accessibility
] _ Functionfunction_name
([parameters
]) [Asreturn_type
] [ Implementsinterface.function
] [statements
] End Function
This is the same as the syntax used for declaring a subroutine, except that a function includes a return type and ends with End Function.
The inheritance_mode can be one of the values Overloads, Overrides, Overridable, NotOverridable, MustOverride, Shadows, or Shared. These values determine how a subroutine declared within a class inherits from the parent class or how it allows inheritance in derived classes.
The accessibility clause can take one of the following values: Public, Protected, Friend, Protected Friend, or Private. These values determine which pieces of code can invoke the subroutine.
A function assigns its return value either by setting its name equal to the value or by using the Return statement. Using the Return statement may allow the compiler to optimize the code more, so it is generally preferred.
The syntax for read/write property procedures is as follows:
Propertyproperty_name
() Asdata_type
Get ... End Get Set(ByVal Value As data_type) ... End Set End Property
The syntax for a read-only property procedure is as follows:
Public ReadOnly Propertyproperty_name
() Asdata_type
Get ... End Get End Property
The syntax for a write-only property procedure is as follows:
Public WriteOnly Propertyproperty_name
() Asdata_type
Set(ByVal Value Asdata_type
) ... End Set End Property
In all three of these cases, you don't need to remember all the declaration details. If you type the first line (including the ReadOnly or WriteOnly keywords if you want them) and press Enter, Visual Basic creates blank property procedures for you.
The Property Get procedures should all assign return values, as in property_name = return_value
or by using the Return statement, as in Return return_value
.
Auto-implemented properties let you create simple read/write properties without providing Get and Set. The following code shows the syntax:
Propertyproperty_name
() Asdata_type
[=initial_value
]
Visual Basic automatically makes a backing variable to hold the property's value, and Get and Set routines to access the value.
Note that Visual Basic cannot provide auto-implemented ReadOnly or WriteOnly properties.
A lambda function (also called an inline function) is a function declared within another routine. You can use lambda functions to initialize a delegate or to pass the function to a method that takes a delegate as a parameter.
For example, the following code creates an inline delegate named F
. It then displays the value of F(12)
.
Dim F = Function(x As Integer) Sin(x / 2) + 2 * Cos(x / 3) Debug.WriteLine(F(12))
The following code calls subroutine ApplyFunction. This function takes as parameters an array of values and a function that it should apply to each of the values. The code passes an inline delegate that doubles a number into ApplyFunction to double each of the values.
ApplyFunction(values, Function(x As Single) 2 * x)
A lambda subroutine is similar to a lambda function except it doesn't return a value. The syntax is similar to the syntax for lambda functions except you use the type Action instead of Function. You would also use a lambda subroutine where no return value is required. The following code creates and invokes a lambda subroutine:
Dim echo As Action(Of Integer) = Sub(x As Integer) Debug.WriteLine(x) echo(123)
The following code creates a lambda subroutine inline as a parameter to a call to the Array.ForEach method:
Dim states() As String ? {"CO", "UT", "KS", "WY"} Array.ForEach(Of String)(states, Sub(str As String) MessageBox.Show(str))
You can make multiline lambda functions or subroutines. Start a new line after the Sub or Function statement, include the lines of code that you need, and finish with End Sub of End Function.
The following code shows a call to Array.ForEach that uses a multiline lambda subroutine:
Array.ForEach(Of String)(states, Sub(str As String) Debug.WriteLine(str) MessageBox.Show(str) End Sub )
To make an extension method, place a method in a code module and decorate it with the Extension attribute. The first parameter to the method determines the class that the method extends. For example, the following code gives the String class a MatchesRegexp method that returns True if the String matches a regular expression:
Module StringExtensions <Extension()> Public Function MatchesRegexp(ByVal the_string As String, ByVal regular_expression As String) As Boolean Dim reg_exp As New Regex(regular_expression) Return reg_exp.IsMatch(the_string) End Function End Module
A partial method is a private subroutine that is declared in one place and implemented in another. The following code defines the signature of the RecordException subroutine and then later defines its body:
Public Class PathAlgorithm Partial Private Sub RecordException(ByVal ex As Exception) End Sub ... Private Sub RecordException(ByVal ex As Exception) Debug.WriteLine("Error: " & ex.Message) End Sub ... End Class
Partial methods are mainly intended for use by code generators. In your code, you can usually use events instead. It's useful to understand what they do in case you need to read generated code.