CallByName 函数

更新:2007 年 11 月

执行对象的方法,或者设置或返回对象的属性。

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

参数

  • ObjectRef
    必需。Object。指向公开属性或方法的对象的指针。

  • ProcName
    必需。String。包含对象的属性名或方法名的字符串表达式。

  • UseCallType
    必需。CallType 枚举 类型的枚举成员,表示所调用过程的类型。CallType 的值可以是 Method、Get 或 Set。

  • Args
    可选。ParamArray。参数数组,包含要传递给所调用的属性和方法的参数。

异常

异常类型

错误号

条件

ArgumentException

5

无效 UseCallType 值;必须为 Method、Get 或 Set。

如果正在升级使用非结构化错误处理方式的 Visual Basic 6.0 应用程序,请参见“错误号”一列。(您可以根据 Number 属性(Err 对象) 比较错误号。) 然而,如果可能,应当考虑用 Visual Basic 的结构化异常处理概述 替换这种错误控制。

备注

CallByName 函数在运行时用来获取属性,设置属性或调用方法。

示例

在下面的示例中,第一行使用 CallByName 设置文本框的 Text 属性,第二行检索 Text 属性的值,第三行调用 Move 方法以移动文本框。

' 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

下一个示例使用 CallByName 函数调用集合对象的 Add 和 Item 方法。

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

智能设备开发人员说明

不支持此函数。

要求

命名空间:Microsoft.VisualBasic

**模块:**Interaction

**程序集:**Visual Basic 运行库(在 Microsoft.VisualBasic.dll 中)

请参见

概念

参数数组

使用字符串名调用属性或方法

参考

CallType 枚举

ArgumentException