CallByName Function


CallByName Function

Class

Microsoft.VisualBasic.Interaction

Syntax

     Dim result As Object = CallByName  (objectRef, procName, _       useCallType, args(  )) 


objectRef (required; Object)

A reference to the object containing the property or method being accessed.


procName (required; String)

The name of the property or method to call or access.


useCallType (required; CallType enumeration)

The type of procedure being accessed. One of the following Microsoft.VisualBasic.CallType enumeration values. Each member also has an equivalent Visual Basic intrinsic constant.

Value

VB constant

Description

Method

vbMethod

The called procedure is a method.

Get

vbGet

The called procedure retrieves a property value.

Set

vbSet

The called procedure sets the value of a property.



args (optional; any)

One or more comma-delimited arguments (a ParamArray argument) representing the arguments required by the procedure being called. This may also be an array of argument objects.

Description

The CallByName function provides a method for calling a class member by name. It calls any property accessor (get or set) or method and returns that member's return value, if available. The member name is sent as a string instead of as a design-time compiled member.

Since procName is a string expression, it is possible to call routines dynamically at runtime using the name of the members.

Usage at a Glance

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

  • procName is not case-sensitive.

  • The performance of CallByName is inferior to calling members through direct object member access.

Example

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

     Module GeneralCode        Public Sub TestCallByName(  )           Dim mathTester As New EasyMath           Dim testArguments(  ) As Double = {1.0#, 2.0#, 3.0#}           MsgBox(CallByName(mathTester, "Multiply", _              CallType.Method, testArguments))     ' Displays "6"        End Sub     End Module     Public Class EasyMath        Public Function Multiply(ByVal sourceValues(  ) As Double) _              As Double           Dim operationResult As Double = 1.0#           Dim counter As Integer           For counter = 0 To UBound(sourceValues)              operationResult *= sourceValues(counter)           Next counter           Return operationResult        End Function     End Class 

See Also

Call Statement




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

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