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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//! ## `Array` Function
//!
//! Returns a `Variant` containing an array formed from the comma-delimited arg list of values
//! passed into the function.
//!
//! ## Syntax
//!
//! ```text
//! Array(arglist)
//! ```
//!
//! ## Parameters
//!
//! - `arglist`: Required. A comma-delimited list of values that are assigned to the elements
//! of the array contained within the `Variant`. If no arguments are specified, an array of zero
//! length is created.
//!
//! ## Return Value
//!
//! Returns a `Variant` whose subtype is `Array` containing the specified elements.
//!
//! ## Remarks
//!
//! - `Variant Array`: The `Array` function returns a `Variant` that contains an array. The array
//! elements are `Variants` that can hold any data type.
//! - `Zero-Based`: The array created by the `Array` function is zero-based. The first element
//! has an index of 0.
//! - `Dynamic Size`: The size of the array is determined by the number of arguments provided.
//! - `Mixed Types`: `Array` elements can be of different types since they are stored as `Variants`.
//! - `Assignment`: The result must be assigned to a `Variant` variable, not an array declared
//! with specific dimensions.
//! - `Empty Array`: Calling `Array()` with no arguments creates a zero-length array.
//! - `LBound and UBound`: You can use `LBound` and `UBound` to determine the array bounds.
//! `LBound` always returns 0, `UBound` returns (number of elements - 1).
//! - `Option Base`: The `Array` function is not affected by `Option Base` statements; it always
//! creates zero-based arrays.
//!
//! ## Important Characteristics
//!
//! ### Assignment Requirements
//!
//! ```vb
//! ' Correct - assign to Variant
//! Dim v As Variant
//! v = Array(1, 2, 3) ' OK
//!
//! ' Incorrect - cannot assign to typed array
//! Dim arr(2) As Integer
//! arr = Array(1, 2, 3) ' ERROR: Type mismatch
//! ```
//!
//! ### Zero-Based Indexing
//!
//! ```vb
//! Dim arr As Variant
//! arr = Array("A", "B", "C")
//! Debug.Print LBound(arr) ' Always 0
//! Debug.Print UBound(arr) ' 2 (not 3!)
//!
//! ' First element is arr(0), last is arr(2)
//! ```
//!
//! ### Performance Considerations
//!
//! - `Array()` creates a `Variant` array, which has more overhead than typed arrays
//! - For large arrays with known types, consider using `ReDim` instead
//! - `Array()` is best for small, temporary arrays or mixed-type collections
//! - Each element is a `Variant`, which uses more memory than native types
//!
//! ## Related Functions
//!
//! - `Split`: Splits a string into an array of substrings
//! - `Join`: Concatenates array elements into a string
//! - `LBound`: Returns the lowest available subscript for an array dimension
//! - `UBound`: Returns the highest available subscript for an array dimension
//! - `IsArray`: Determines whether a variable is an array
//! - `Filter`: Returns a zero-based array containing a subset of a string array
//!
//! ## Examples
//!
//! ### Basic Array Creation
//!
//! ```vb
//! Dim myArray As Variant
//! myArray = Array(1, 2, 3, 4, 5)
//! ' myArray contains: [1, 2, 3, 4, 5]
//! ' LBound(myArray) = 0, UBound(myArray) = 4
//! ```
//!
//! ### Mixed Data Types
//!
//! ```vb
//! Dim mixed As Variant
//! mixed = Array("Hello", 42, True, #1/1/2025#, 3.14)
//! ' Array can hold different types
//! ```
//!
//! ### String Array
//!
//! ```vb
//! Dim names As Variant
//! names = Array("Alice", "Bob", "Charlie")
//! Debug.Print names(0) ' Prints: Alice
//! ```
//!
//! ### Empty Array
//!
//! ```vb
//! Dim emptyArr As Variant
//! emptyArr = Array()
//! ' Creates a zero-length array
//! ' UBound(emptyArr) = -1
//! ```
//!
//! ### Using For Each
//!
//! ```vb
//! Dim values As Variant
//! values = Array(10, 20, 30, 40)
//!
//! Dim item As Variant
//! For Each item In values
//! Debug.Print item
//! Next item
//! ```
//!
//! ### Array as Function Return
//!
//! ```vb
//! Function GetColors() As Variant
//! GetColors = Array("Red", "Green", "Blue")
//! End Function
//! ```
//!
//! ### Accessing Elements
//!
//! ```vb
//! Dim data As Variant
//! data = Array("A", "B", "C")
//! Debug.Print data(0) ' A
//! Debug.Print data(1) ' B
//! Debug.Print data(2) ' C
//! ```
//!
//! ## Common Patterns
//!
//! ### Initialize Lookup Table
//!
//! ```vb
//! Function GetMonthName(monthNum As Integer) As String
//! Dim months As Variant
//! months = Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", _
//! "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
//!
//! If monthNum >= 1 And monthNum <= 12 Then
//! GetMonthName = months(monthNum - 1)
//! Else
//! GetMonthName = ""
//! End If
//! End Function
//! ```
//!
//! ### Configuration Data
//!
//! ```vb
//! Sub ProcessFiles()
//! Dim extensions As Variant
//! extensions = Array(".txt", ".doc", ".pdf", ".xls")
//!
//! Dim ext As Variant
//! For Each ext In extensions
//! ProcessFileType CStr(ext)
//! Next ext
//! End Sub
//! ```
//!
//! ### Quick Test Data
//!
//! ```vb
//! Sub TestFunction()
//! Dim testCases As Variant
//! testCases = Array(0, 1, 10, 100, -1, -100)
//!
//! Dim testValue As Variant
//! For Each testValue In testCases
//! Debug.Print "Testing: " & testValue & " -> " & MyFunction(testValue)
//! Next testValue
//! End Sub
//! ```
//!
//! ### Passing Multiple Values
//!
//! ```vb
//! Sub UpdateRecord()
//! SaveData Array("Name", "John"), _
//! Array("Age", 30), _
//! Array("City", "NYC")
//! End Sub
//!
//! Sub SaveData(ParamArray fields())
//! Dim field As Variant
//! For Each field In fields
//! Debug.Print field(0) & ": " & field(1)
//! Next field
//! End Sub
//! ```
//!
//! ### Enumeration Substitute
//!
//! ```vb
//! Function GetStatusText(status As Integer) As String
//! Dim statuses As Variant
//! statuses = Array("Pending", "Processing", "Complete", "Failed")
//!
//! If status >= 0 And status <= 3 Then
//! GetStatusText = statuses(status)
//! Else
//! GetStatusText = "Unknown"
//! End If
//! End Function
//! ```
//!
//! ### Split Alternative (VB6 Early Versions)
//!
//! ```vb
//! ' Before Split function was widely available
//! Function GetHeaderFields() As Variant
//! GetHeaderFields = Array("ID", "Name", "Date", "Status")
//! End Function
//! ```
//!
//! ### Matrix/Grid Data
//!
//! ```vb
//! Sub CreateGrid()
//! Dim row1 As Variant, row2 As Variant, row3 As Variant
//! row1 = Array(1, 2, 3)
//! row2 = Array(4, 5, 6)
//! row3 = Array(7, 8, 9)
//!
//! Dim grid As Variant
//! grid = Array(row1, row2, row3)
//!
//! ' Access: grid(0)(0) = 1, grid(1)(2) = 6, etc.
//! End Sub
//! ```
//!
//! ### Default Values
//!
//! ```vb
//! Function GetDefaults() As Variant
//! GetDefaults = Array(0, "", False, Null, Empty)
//! End Function
//! ```
//!
//! ## Error Handling
//!
//! The `Array` function does not typically raise errors:
//!
//! - An empty argument list returns an empty array (not an error)
//! - Nested arrays are supported (arrays containing arrays)
//! - All argument types are accepted without restriction
//!
//! ## Performance Notes
//!
//! - Fast operation with O(n) complexity where n is the number of elements
//! - Creates a new array each time; consider caching for repeated use
//! - Best suited for small collections (typically < 100 elements)
//! - For larger datasets, consider using `ReDim` or other data structures
//!
//! ## Platform Notes
//!
//! - Available in VB6, VBA, and VBScript
//! - Always creates zero-based arrays regardless of `Option Base` setting
//! - Returns a `Variant` subtype array (not a strongly-typed array)
//! - Behavior is consistent across all VB platforms
use crateArrayValue;
use crateVBType;
use crateVBVariant;
/// Implementation of the `Array` function.
///
/// Creates a zero-based Variant array from the given elements.
///
/// VB6 behavior:
/// - The resulting array is always zero-based (`LBound` = 0)
/// - Elements are stored as-is (no type coercion)
/// - An empty argument list produces an empty array with `UBound` = -1
/// - Nested arrays are supported