1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
//! # `CallByName` Function
//!
//! Executes a method, sets or returns a property, or sets or returns a field of an object.
//!
//! ## Syntax
//!
//! ```vb
//! CallByName(object, procname, calltype, [args()])
//! ```
//!
//! ## Parameters
//!
//! - `object` - Required. Object expression on which the function will be executed.
//! - `procname` - Required. String expression containing the name of the property, method, or field member of the object.
//! - `calltype` - Required. Member of the `VbCallType` enumeration representing the type of procedure being called.
//! - `args()` - Optional. `Variant` array containing the arguments to be passed to the property, method, or field being called.
//!
//! ## `VbCallType` Constants
//!
//! | Constant | Value | Description |
//! |----------|-------|-------------|
//! | `VbMethod` | 1 | A method is being called |
//! | `VbGet` | 2 | A property value is being retrieved |
//! | `VbLet` | 4 | A property value is being set |
//! | `VbSet` | 8 | A reference to an object is being set |
//!
//! ## Return Value
//!
//! Returns a `Variant` containing the result of the called property or method. For `VbLet` and `VbSet`,
//! the return value is not meaningful.
//!
//! ## Remarks
//!
//! The `CallByName` function allows you to execute object members by name at run time, providing
//! a form of late binding. This is particularly useful for:
//!
//! - Creating generic routines that work with multiple object types
//! - Implementing dynamic property access based on user input
//! - Building reflection-like functionality in VB6
//! - Simplifying repetitive property access code
//!
//! ### Important Notes
//!
//! 1. **Late Binding**: `CallByName` always uses late binding, even if the object variable is early bound
//! 2. **Performance**: Slower than direct method/property calls due to name lookup overhead
//! 3. **Case Insensitive**: The `procname` parameter is case-insensitive
//! 4. **Error Handling**: Raises run-time error if the member doesn't exist
//! 5. **Type Safety**: No compile-time checking of member existence or argument types
//!
//! ### Call Type Details
//!
//! **`VbMethod` (1)**:
//! - Calls a Sub or Function
//! - Returns the function's return value (or Empty for Subs)
//! - Passes arguments in the args array
//!
//! **`VbGet` (2)**:
//! - Retrieves a property value or field value
//! - Can be used with property procedures (Property Get) or public fields
//! - For parameterized properties, pass indices in args array
//!
//! **`VbLet` (4)**:
//! - Sets a `property` value or field value
//! - For simple data types (numbers, strings, etc.)
//! - The new value must be the last element in the args array
//! - For parameterized properties, indices come before the value
//!
//! **`VbSet` (8)**:
//! - Sets an object reference property
//! - Similar to `VbLet` but for object references
//! - Used when you would normally use the Set keyword
//!
//! ## Examples
//!
//! ### Basic Method Call
//!
//! ```vb
//! Dim obj As Object
//! Set obj = CreateObject("Scripting.FileSystemObject")
//!
//! ' Call the GetFolder method
//! Dim folder As Variant
//! folder = CallByName(obj, "GetFolder", VbMethod, "C:\Temp")
//! ```
//!
//! ### Property Get
//!
//! ```vb
//! Dim fs As Object
//! Set fs = CreateObject("Scripting.FileSystemObject")
//!
//! ' Get the Drives property
//! Dim drives As Variant
//! drives = CallByName(fs, "Drives", VbGet)
//! ```
//!
//! ### Property Let
//!
//! ```vb
//! Dim txt As Object
//! Set txt = CreateObject("Scripting.TextStream")
//!
//! ' Set a property value
//! CallByName txt, "Line", VbLet, 10
//! ```
//!
//! ### Property Set (Object Reference)
//!
//! ```vb
//! Dim form As Form
//! Set form = New Form1
//!
//! Dim btn As CommandButton
//! Set btn = New CommandButton
//!
//! ' Set an object property
//! CallByName form, "ActiveControl", VbSet, btn
//! ```
//!
//! ### Dynamic Property Access
//!
//! ```vb
//! Function GetPropertyValue(obj As Object, propName As String) As Variant
//! GetPropertyValue = CallByName(obj, propName, VbGet)
//! End Function
//!
//! Function SetPropertyValue(obj As Object, propName As String, value As Variant)
//! CallByName obj, propName, VbLet, value
//! End Function
//! ```
//!
//! ## Common Patterns
//!
//! ### 1. Generic Property Copier
//!
//! ```vb
//! Sub CopyProperties(source As Object, dest As Object, propNames() As String)
//! Dim i As Integer
//! Dim value As Variant
//!
//! For i = LBound(propNames) To UBound(propNames)
//! value = CallByName(source, propNames(i), VbGet)
//! CallByName dest, propNames(i), VbLet, value
//! Next i
//! End Sub
//! ```
//!
//! ### 2. Form Field Population
//!
//! ```vb
//! Sub PopulateFormFromRecordset(frm As Form, rs As Recordset)
//! Dim fld As Field
//! Dim ctl As Control
//!
//! For Each fld In rs.Fields
//! On Error Resume Next
//! Set ctl = frm.Controls(fld.Name)
//! If Not ctl Is Nothing Then
//! CallByName ctl, "Text", VbLet, fld.Value & ""
//! End If
//! On Error GoTo 0
//! Next fld
//! End Sub
//! ```
//!
//! ### 3. Dynamic Method Invocation
//!
//! ```vb
//! Function InvokeMethod(obj As Object, methodName As String, _
//! ParamArray args() As Variant) As Variant
//! Dim argArray() As Variant
//! Dim i As Integer
//!
//! If UBound(args) >= 0 Then
//! ReDim argArray(LBound(args) To UBound(args))
//! For i = LBound(args) To UBound(args)
//! argArray(i) = args(i)
//! Next i
//! InvokeMethod = CallByName(obj, methodName, VbMethod, argArray)
//! Else
//! InvokeMethod = CallByName(obj, methodName, VbMethod)
//! End If
//! End Function
//! ```
//!
//! ### 4. Property Name Validation
//!
//! ```vb
//! Function HasProperty(obj As Object, propName As String) As Boolean
//! On Error Resume Next
//! Dim temp As Variant
//! temp = CallByName(obj, propName, VbGet)
//! HasProperty = (Err.Number = 0)
//! On Error GoTo 0
//! End Function
//! ```
//!
//! ### 5. Bulk Property Setting
//!
//! ```vb
//! Sub SetMultipleProperties(obj As Object, propNames As Variant, _
//! propValues As Variant)
//! Dim i As Integer
//!
//! For i = LBound(propNames) To UBound(propNames)
//! CallByName obj, propNames(i), VbLet, propValues(i)
//! Next i
//! End Sub
//!
//! ' Usage:
//! SetMultipleProperties myControl, _
//! Array("Left", "Top", "Width", "Height"), _
//! Array(100, 100, 200, 50)
//! ```
//!
//! ### 6. Parameterized Property Access
//!
//! ```vb
//! ' Access a property with parameters (like an indexed property)
//! Sub SetIndexedProperty(obj As Object, propName As String, _
//! index As Integer, value As Variant)
//! CallByName obj, propName, VbLet, index, value
//! End Sub
//!
//! Function GetIndexedProperty(obj As Object, propName As String, _
//! index As Integer) As Variant
//! GetIndexedProperty = CallByName(obj, propName, VbGet, index)
//! End Function
//! ```
//!
//! ### 7. Configuration-Driven Object Initialization
//!
//! ```vb
//! Sub InitializeFromConfig(obj As Object, configFile As String)
//! Dim fs As Object
//! Dim ts As Object
//! Dim line As String
//! Dim parts() As String
//!
//! Set fs = CreateObject("Scripting.FileSystemObject")
//! Set ts = fs.OpenTextFile(configFile)
//!
//! Do While Not ts.AtEndOfStream
//! line = ts.ReadLine
//! If InStr(line, "=") > 0 Then
//! parts = Split(line, "=")
//! CallByName obj, Trim(parts(0)), VbLet, Trim(parts(1))
//! End If
//! Loop
//!
//! ts.Close
//! End Sub
//! ```
//!
//! ### 8. Error-Safe Property Access
//!
//! ```vb
//! Function SafeGetProperty(obj As Object, propName As String, _
//! Optional defaultValue As Variant) As Variant
//! On Error Resume Next
//! SafeGetProperty = CallByName(obj, propName, VbGet)
//! If Err.Number <> 0 Then
//! If Not IsMissing(defaultValue) Then
//! SafeGetProperty = defaultValue
//! Else
//! SafeGetProperty = Empty
//! End If
//! End If
//! On Error GoTo 0
//! End Function
//! ```
//!
//! ## Advanced Usage
//!
//! ### Calling Methods with Multiple Arguments
//!
//! ```vb
//! Dim obj As Object
//! Set obj = CreateObject("SomeLibrary.SomeClass")
//!
//! ' Call a method with multiple arguments
//! Dim result As Variant
//! result = CallByName(obj, "Calculate", VbMethod, 10, 20, "sum")
//! ```
//!
//! ### Working with Collections
//!
//! ```vb
//! Sub EnumerateCollection(coll As Collection, methodName As String)
//! Dim item As Variant
//! For Each item In coll
//! CallByName item, methodName, VbMethod
//! Next item
//! End Sub
//! ```
//!
//! ### Building a Simple ORM
//!
//! ```vb
//! Sub SaveObjectToDatabase(obj As Object, tableName As String, _
//! propNames() As String)
//! Dim sql As String
//! Dim values As String
//! Dim i As Integer
//! Dim value As Variant
//!
//! sql = "INSERT INTO " & tableName & " ("
//! values = " VALUES ("
//!
//! For i = LBound(propNames) To UBound(propNames)
//! If i > LBound(propNames) Then
//! sql = sql & ", "
//! values = values & ", "
//! End If
//!
//! sql = sql & propNames(i)
//! value = CallByName(obj, propNames(i), VbGet)
//! values = values & "'" & value & "'"
//! Next i
//!
//! sql = sql & ")" & values & ")"
//! ' Execute SQL...
//! End Sub
//! ```
//!
//! ## Error Handling
//!
//! Common errors when using `CallByName`:
//!
//! - **Error 438**: Object doesn't support this property or method
//! - The specified member doesn't exist
//! - Check spelling and case (though `CallByName` is case-insensitive)
//!
//! - **Error 450**: Wrong number of arguments or invalid property assignment
//! - Incorrect number of arguments in the args array
//! - Using `VbLet` for an object (should use `VbSet`)
//! - Using `VbSet` for a value type (should use `VbLet`)
//!
//! - **Error 13**: Type mismatch
//! - Argument types don't match what the member expects
//!
//! ```vb
//! On Error Resume Next
//! result = CallByName(obj, "PropertyName", VbGet)
//! If Err.Number <> 0 Then
//! MsgBox "Error accessing property: " & Err.Description
//! End If
//! On Error GoTo 0
//! ```
//!
//! ## Performance Considerations
//!
//! - `CallByName` is significantly slower than direct member access
//! - Name resolution happens at runtime, not compile time
//! - Consider caching frequently accessed members
//! - Use early binding and direct calls in performance-critical code
//! - `CallByName` is best for scenarios where dynamic access is necessary
//!
//! ## Limitations
//!
//! - Cannot call private members
//! - Cannot call Friend members from outside the project
//! - No `IntelliSense` support for the member being called
//! - No compile-time type checking
//! - Cannot call default members by passing empty string
//! - More difficult to debug than direct calls
//!
//! ## Related Functions
//!
//! - `Eval`: Evaluates an expression (only in VBA, not VB6)
//! - `Execute`: Executes a statement (only in VBA, not VB6)
//! - `GetObject`: Returns a reference to an object
//! - `CreateObject`: Creates an instance of an object
//! - `TypeName`: Returns the type name of a variable
//! - `VarType`: Returns the variant subtype of a variable
//!
//! ## Parsing Notes
//!
//! The `CallByName` function is not a reserved keyword in VB6. It is parsed as a regular
//! function call (`CallExpression`). This module exists primarily for documentation
//! purposes and to provide a comprehensive test suite that validates the parser
//! correctly handles `CallByName` function calls in various contexts.