Appendix D. Subroutine and Function Declarations

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.

SUBROUTINES

The syntax for writing a subroutine is as follows:

[attribute_list] [interitance_mode] [accessibility]
Sub subroutine_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.

FUNCTIONS

The syntax for writing a function is as follows:

[attribute_list] [interitance_mode] [accessibility] _
Function function_name([parameters]) [As return_type] [ Implements interface.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.

PROPERTY PROCEDURES

The syntax for read/write property procedures is as follows:

Property property_name () As data_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 Property property_name () As data_type
    Get
        ...
    End Get
End Property

The syntax for a write-only property procedure is as follows:

Public WriteOnly Property property_name () As data_type
    Set(ByVal Value As data_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:

Property property_name () As data_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.

LAMBDA FUNCTIONS AND EXPRESSIONS

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
)

EXTENSION METHODS

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

PARTIAL METHODS

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.189.129