Microsoft KB Archive/186143

= How To Use the CallByName Function to Run a Procedure =

Article ID: 186143

Article Last Modified on 7/13/2004

-

APPLIES TO


 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q186143



SUMMARY
The CallByName function provides Visual Basic 6.0 with the ability to call a property or method of an object using a string at run-time. Unlike the previous versions of Visual Basic in which methods and properties had to be known at design-time, the CallByName function allows for a great deal of flexibility at run-time. The use of this function is limited to OLE servers and Visual Basic class modules.



MORE INFORMATION
The following sample demonstrates how to use the CallByName function to call a method, a Property Let, and a Property Get. For more information on the CallByName function, please refer to the Visual Basic Help Files and the Books On-Line.

Steps to Create Sample Project
 Start a new Standard EXE project in Visual Basic. Form1 is created by default. Add a Class Module (Class1) to the project.  Add the following code to the Class Module: Option Explicit Private MyPropValue As Integer

Public Function Multiply(x As Integer, y As Integer) As Integer Multiply = x * y     End Function

Public Property Get MyProperty As Variant MyProperty = MyPropValue End Property

Public Property Let MyProperty(ByVal vNewValue As Variant) MyPropValue = vNewValue End Property

  Add the following code to the Form Module (Form1): Option Explicit Private Sub Form_Click Dim myclass As New Class1 Dim sum As Integer Dim prop As Integer

' Example of calling a method with CallByName ' equivalent to -- sum = myclass.Multiply(12, 12) sum = CallByName(myclass, "Multiply", VbMethod, 12, 12) MsgBox sum

' Example of a property let with CallByName ' equivalent to -- myclass.MyProperty = 5 CallByName myclass, "MyProperty", VbLet, 5

' Example of a property get with CallByName ' equivalent to -- prop = myclass.MyProperty prop = CallByName(myclass, "MyProperty", VbGet) MsgBox prop End Sub

 Run the Project and click on Form1. A message box displaying the number 144 and a message box displaying the number 5 are shown in succession.

Additional query words: kbDSupport kbDSD kbVBA kbVBp600 kbVBp

Keywords: kbhowto KB186143

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.