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] [inheritance_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] [inheritance_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 by using the Return statement.

PROPERTY PROCEDURES

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

Property property_name() As data_type
    Get
        ...
    End Get
    Set(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(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 the last line, then Visual Basic displays an error indicator telling you that you didn’t include the Get and Set procedures. If you hover the mouse over the error indicator, the error correction options provide a link you can click to generate empty procedures automatically.

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. 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 or 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

An extension method adds a new method to an existing class, even if you don’t have access to the class’s code. For example, you could add a ToFileSize extension method to the Long class to convert a Long value into a formatted file size string as in 1.23 KB or 4.5 GB. Then if size is a Long variable, the program could invoke the method as in the following code:

MessageBox.Show("The file size is " & size.ToFileSize())

To make an extension method, place a method in a code module and decorate it with the Extension attribute. (The Extension attribute is defined in the System.Runtime.CompilerServices namespace. You may want to import that namespace to make your code easier to read.)

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(the_string As String,
     regular_expression As String) As Boolean
        Dim reg_exp As New Regex(regular_expression)
        Return reg_exp.IsMatch(the_string)
    End Function
End Module
..................Content has been hidden....................

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