CallByName Function

   
CallByName Function

Class

Microsoft.VisualBasic.Interaction

Named Arguments

Yes, if Args ( ) is omitted

Syntax

 CallByName(   Object, ProcName, UseCallType, Args   (  )) 
Object (required; Object)

A reference to the object containing the procedure being called.

ProcName (required; String)

The name of the procedure to call.

UseCallType (required; CallType constant)

A constant of the type CallType indicating what type of procedure is being called. CallType constants are listed in the following table.

Constant

Value

Description

Method

1

The called procedure is a method.

Get

2

The called procedure retrieves a property value.

Let

4

The called procedure sets the value of a property.

Args (optional; any)

A ParamArray argument representing the arguments required by the procedure being called.

Return Value

Depends on the return value (if any) of the called procedure

Description

Provides a method for calling a class member by name.

Since ProcName is a string expression, rather than the literal name of a routine, it is possible to call routines dynamically at runtime using a string variable to hold the various procedure names .

Rules at a Glance

  • The return type of CallByName is the return type of the called procedure.

  • ProcName is not case sensitive.

  • UseCallType can either be a numeric value or a constant of the CallType enumeration. In the latter case, the enumeration name must be specified along with the constant name, as in CallType.Method.

  • Args ( ) must be a parameter array. A parameter array is an array used to contain function, procedure, or property arguments that can have a variable number of elements.

Programming Tips and Gotchas

  • Since the member to be called is not known at compile time, the performance of CallByName is inferior to calling members directly by literal name.

  • Using CallByName does not necessarily require that Option Strict be set Off.

Example

The following example uses a parameter array to call the Multiply method of a class named Math:

 Imports Microsoft.VisualBasic Imports System Module modMain Public Sub Main(  ) Dim oMath As New Math Dim dArr(  ) As Double = {1,2,3} ' Call using ParamArray MsgBox(CallByName(oMath, "Multiply", CallType.Method, dArr)) End Sub End Module Public Class Math Public Function Multiply(a(  ) As Double) As Double Dim result as double = 1.0 Dim intCtr As Integer Dim intIndex As Integer = 0 for intIndex = 0 to ubound(a)    result = result * a(intIndex) next Multiply = result    End Function End Class 

VB.NET/VB 6 Differences

In VB 6, you don't have to specify VbCallType as the name of the enumeration to access its constants. In VB.NET, you must specify CallType as the name of the enumeration to access its constants.

See Also

Call Statement

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net