Most of the time, users can discover the properties and methods of an object, and can write code to handle these methods. In some cases, however, it is not possible to know in advance the attributes and methods of an object, or simply to have the flexibility to specify attributes and run different methods when running the program.
For example, a client-run program computes an expression by manipulating an operation on a COM component. Suppose you are now adding a function to the server that requires a new description of the operation. Unfortunately, the client software must be recompiled and published before the new operation can be used. To avoid this, a user can use the CallByName function to load a new operation as a character to the server without needing to change the application.
The CallByName function allows a user to specify a property or method using a character when running a program, using the following methods:
Result=callbyname (Object,procedurename,calltype,arguments ())
The first Parameter object represents the object that the user needs to manipulate, and the second parameter procedurename refers to the character representation of the method, property, or procedure name to be manipulated by the user, CallType is a constant option, and when a method is manipulated, the value is Vbmethod When the property is set, the value is Vblet, and the value is vbget when the property is obtained, and the value is vbset when the value is set for the object property, and the last option is optional. It includes an array of variables that contain all the parameters of the procedure.
Suppose there is a COM component called Mathserver that contains functions of some square root operations. The program has two TextBox controls: The first control is used to enter an expression that needs to be evaluated, and the second control year is used to enter the calculation method that you want, and you can add the following code to the click event for the calculation button:
Private Sub Command1_Click ()
Text1.text=callbyname (Mathserver,text2.text,vbmethod,text1.text)
End Sub
If you enter "64/4" in the first text and "SquareRoot" in the second text, the above code calls the square root function and returns "4" in the first text. However, if you enter an illegal character in the first text, or if you do not enter a method in the second text and a property is entered in, or if the method requires a parameter, a run-time error occurs. As is speculated, if you need to use this character name to invoke a property or method, you must do a very good job of error handling.
CallByName is useful in some cases, but must consider some of its drawbacks, such as using CallByName to invoke a procedure that is slower than late binding, but if the user wants to invoke a function that is often repeated, such as an internal loop, Then it will be quicker to use callbyname.