VB and VBA Users Source Code: Using Nested CallbyName's
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Using Nested CallbyName's
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Tuesday, October 10, 2000
Hits:
866
Category:
Visual Basic General
Article:
One of the introductions to VB6 that rarely receives any press is the CallByName function. It is not immediately obvious where you would use this function. However, suppose you had a server application called MathServer, with the functions SquareRoot and Square. Your application has two TextBox controls. Text1 contains the expression to be evaluated upon and Text2 containing the name of the function to call. You could use the following code in the Click event of a command button to invoke the either the SquareRoot or Square function on the expression in Text1: Private Sub Command1_Click() Text1.Text = CallByName(MathServer, Text2.Text, vbMethod, Text1.Text) End Sub But, it is not immediately obvious what to do if you have an array of objects and you want to set the a property for any object in the array given its index. The following code demonstrates how to do this: Dim lIndex as Long Dim aoClass1(1 to 5) as New Class1 'Create a object array lIndex = 1 'Call the property "MyProperty" of aoClass1(1) CallByName CallByName(Me, "aoClass1", vbGet, lIndex), "MyProperty", vbLet, 0 In the above, the inner CallByname references the first item in the class array and the outer CallByname calls the property let on "MyProperty".
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder