Udostępnij za pośrednictwem


CallByName Function

Executes a method on an object, or sets or returns a property on an object.

Public Function CallByName( _
   ByVal ObjectRef As System.Object, _
   ByVal ProcName As String, _
   ByVal UseCallType As CallType, _
   ByVal Args() As Object _
) As Object

Parameters

  • ObjectRef
    Required. Object. A pointer to the object exposing the property or method.

  • ProcName
    Required. String. A string expression containing the name of the property or method on the object.

  • UseCallType
    Required. An enumeration member of type CallType Enumeration representing the type of procedure being called. The value of CallType can be Method, Get, or Set.

  • Args
    Optional. ParamArray. A parameter array containing the arguments to be passed to the property or method being called.

Exceptions

Exception type

Error number

Condition

ArgumentException

5

Invalid UseCallType value; must be Method, Get, or Set.

See the "Error number" column if you are upgrading Visual Basic 6.0 applications that use unstructured error handling. (You can compare the error number against the Number Property (Err Object).) However, when possible, you should consider replacing such error control with Structured Exception Handling Overview for Visual Basic.

Remarks

The CallByName function is used at runtime to get a property, set a property, or invoke a method.

Example

In the following example, the first line uses CallByName to set the Text property of a text box, the second line retrieves the value of the Text property, and the third line invokes the Move method to move the text box.

' Imports statements must be at the top of a module. 
Imports Microsoft.VisualBasic.CallType
Sub TestCallByName1()
    'Set a property.
    CallByName(TextBox1, "Text", CallType.Set, "New Text")

    'Retrieve the value of a property.
    MsgBox(CallByName(TextBox1, "Text", CallType.Get))

    'Call a method.
    CallByName(TextBox1, "Hide", CallType.Method)
End Sub

The next example uses the CallByName function to invoke the Add and Item methods of a collection object.

Public Sub TestCallByName2()
    Dim col As New Collection()

    'Store the string "Item One" in a collection by  
    'calling the Add method.
    CallByName(col, "Add", CallType.Method, "Item One")

    'Retrieve the first entry from the collection using the  
    'Item property and display it using MsgBox().
    MsgBox(CallByName(col, "Item", CallType.Get, 1))
End Sub

Smart Device Developer Notes

This function is not supported.

Requirements

Namespace: Microsoft.VisualBasic

Module: Interaction

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

See Also

Concepts

Parameter Arrays

Calling a Property or Method Using a String Name

Reference

CallType Enumeration

ArgumentException