vb6runtime 0.2.0

VB6 runtime library - value system, type conversions, and standard library implementations
Documentation
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
//! ## `LBound` Function
//!
//! Returns a `Long` containing the smallest available subscript for the indicated dimension of an array.
//!
//! ## Syntax
//!
//! ```text
//! LBound(arrayname, [dimension])
//! ```
//!
//! ## Parameters
//!
//! - **arrayname** (Required): Name of the array variable
//! - **dimension** (Optional): `Integer` specifying which dimension's lower bound to return
//!   - If omitted, defaults to 1 (first dimension)
//!   - Must be between 1 and the number of dimensions in the array
//!
//! ## Return Value
//!
//! - Returns a `Long` of the smallest available subscript for the specified dimension.
//! - By default, arrays start at 0 unless `Option Base 1` is specified.
//! - Returns 0 for standard arrays (`Option Base 0`).
//! - Returns 1 for arrays when `Option Base 1` is specified.
//! - For arrays declared with explicit bounds, returns the specified lower bound.
//! - Dynamic arrays preserve their lower bound across `ReDim` operations.
//!
//! ## Remarks
//!
//! The `LBound` function is essential for array processing:
//!
//! - Returns the lower bound (minimum index) of an array dimension
//! - Counterpart to `UBound` (which returns upper bound)
//! - Critical for correctly iterating through arrays
//! - Default lower bound is 0 (unless `Option Base 1`)
//! - Can specify explicit lower bounds:
//!
//! ```vb6
//! Dim arr(5 To 10) 'LBound = 5
//! ```
//!
//! - Works with multi-dimensional arrays using dimension parameter
//! - Omitting dimension parameter returns bound of first dimension
//! - Dynamic arrays must be dimensioned before calling `LBound`
//! - Fixed-size arrays always have bounds available
//! - `ParamArray` parameters always have an `LBound` value of 0.
//! - Essential for writing dimension-agnostic code
//! - Use with `UBound` to determine array size = `UBound` - `LBound` + 1
//! - Safer than assuming arrays start at 0
//! - `ReDim Preserve` maintains lower bounds
//! - Common in For loops:
//!
//! ```vb6
//! For i = LBound(arr) To UBound(arr)
//!
//! Next
//! ```
//!
//! ### Common Errors
//!
//! - **Error 9** (Subscript out of range): If dimension exceeds array dimensions
//! - **Error 9** (Subscript out of range): If array has not been dimensioned (for dynamic arrays)
//!
//! ## Performance Considerations
//!
//! - **Fast Operation**: `LBound` is a very fast intrinsic function
//! - **No Overhead**: Direct access to array metadata
//! - **Cache Results**: If using in loops, cache `LBound`/`UBound` values
//! - **Bounds in Loops**: Better to cache than call repeatedly
//!
//! ### Performance Optimization
//!
//! #### Less efficient - Calls `LBound` Every Iteration
//!
//! ```vb6
//! For i = LBound(arr) To UBound(arr)
//!     ' process arr(i)
//! Next i
//! ```
//!
//! #### More Efficient For Very Large Loops
//!
//! ```vb6
//! Dim lb As Long, ub As Long
//! lb = LBound(arr)
//! ub = UBound(arr)
//! For i = lb To ub
//!     ' process arr(i)
//! Next i
//! ```
//!
//! ## Typical Uses
//!
//! - **Array Iteration**: Loop through arrays with correct starting index
//! - **Array Size Calculation**: Determine number of elements
//! - **Bounds Validation**: Check if index is within valid range
//! - **Array Copying**: Copy elements with proper bounds
//! - **Multi-dimensional Arrays**: Access correct dimension bounds
//! - **Dynamic Arrays**: Verify array has been dimensioned
//! - **Generic Functions**: Write functions that work with any array bounds
//! - **`Option Base` Handling**: Code that works regardless of `Option Base` setting
//!
//! ## Limitations
//!
//! - Cannot modify array bounds (use `ReDim` for that)
//! - Raises error for undimensioned dynamic arrays
//! - Dimension parameter must be valid (1 to number of dimensions)
//! - Cannot determine if array is fixed-size or dynamic
//! - No way to get all bounds at once (must call separately for each dimension)
//!
//! ## Platform and Version Notes
//!
//! - Available in all VB6 versions
//! - Part of VBA core functions
//! - Returns Long type
//! - Works with all array types (Variant, typed, object arrays)
//! - `ReDim` Preserve maintains lower bounds
//! - `ParamArray` always has `LBound` = 0
//!
//! ## Related Functions
//!
//! - `UBound`: Get upper bound of array dimension
//! - `IsArray`: Check if variable is an array
//! - `ReDim`: Redimension dynamic array
//! - `Array`: Create Variant array
//! - `Split`: Create array from delimited string
//!
//! ## Comparison With Related Functions
//!
//! | Function | Purpose | Returns | Use Case |
//! |----------|---------|---------|----------|
//! | `LBound` | Get lower bound | `Long` | Minimum valid index |
//! | `UBound` | Get upper bound | `Long` | Maximum valid index |
//! | `IsArray` | Check if array | `Boolean` | Validate array type |
//! | `Array` | Create array | `Variant` | Initialize arrays |
//! | `ReDim` | Resize array | N/A | Dynamic array sizing |
//!
//! ## Best Practices
//!
//! - **Always Use `LBound`**: Don't assume arrays start at 0
//! - **Dimension Parameter**: Specify dimension for multi-dimensional arrays
//! - **Error Handling**: Handle undimensioned dynamic arrays
//! - **Array Size**: Use ```UBound - LBound + 1``` for element count
//! - **Cache Values**: Store `LBound`/`UBound` in variables for repeated use
//! - **Generic Code**: Write functions that work with any array bounds
//! - **Validate Bounds**: Check if indices are within `LBound` to `UBound` range
//! - **Document Assumptions**: Note expected array bounds in comments
//!
//! ## `LBound` and `Option Base`
//!
//! ### Option Base 0 (default)
//!
//! ```vb6
//! Dim arr1(5) As Integer
//! Debug.Print LBound(arr1)         ' 0
//! Debug.Print UBound(arr1)         ' 5
//! ```
//!
//! ### Option Base 1
//!
//! ```vb6
//! Option Base 1
//! Dim arr2(5) As Integer
//! Debug.Print LBound(arr2)         ' 1
//! Debug.Print UBound(arr2)         ' 5
//! ```
//!
//! ### Explicit Bounds (Overrides Option Base)
//!
//! ```vb6
//! Dim arr3(10 To 20) As Integer
//! Debug.Print LBound(arr3)         ' 10
//! Debug.Print UBound(arr3)         ' 20
//! ```
//!
//! ### Array Size Calculation
//!
//! ```vb6
//! ' Correct way to get array size
//! Function GetArraySize(arr As Variant) As Long
//!     If Not IsArray(arr) Then
//!         GetArraySize = 0
//!     Else
//!         GetArraySize = UBound(arr) - LBound(arr) + 1
//!     End If
//! End Function
//!
//! ' Examples
//! Dim a(0 To 10) As Integer       ' Size = 11
//! Dim b(1 To 10) As Integer       ' Size = 10
//! Dim c(5 To 15) As Integer       ' Size = 11
//!
//! Debug.Print GetArraySize(a)     ' 11
//! Debug.Print GetArraySize(b)     ' 10
//! Debug.Print GetArraySize(c)     ' 11
//! ```
//!
//! ## Examples
//!
//! ### Example 1: Basic Array Iteration
//!
//! ```vb6
//! Dim arr(5) As Integer
//! Dim i As Long
//!
//! For i = LBound(arr) To UBound(arr)
//!     arr(i) = i * 2
//! Next i
//! ```
//!
//! ### Example 2: Explicit Lower Bound
//!
//! ```vb6
//! Dim months(1 To 12) As String
//!
//! Debug.Print LBound(months)           ' 1
//! Debug.Print UBound(months)           ' 12
//! ```
//!
//! ### Example 3: Multi-Dimensional Array
//!
//! ```vb6
//! Dim grid(1 To 10, 1 To 20) As Integer
//!
//! Debug.Print LBound(grid, 1)          ' 1 - first dimension
//! Debug.Print LBound(grid, 2)          ' 1 - second dimension
//! Debug.Print LBound(grid)             ' 1 - defaults to first dimension
//! ```
//!
//! ### Example 4: Calculate Array Size
//!
//! ```vb6
//! Dim values(10 To 50) As Double
//! Dim size As Long
//!
//! size = UBound(values) - LBound(values) + 1
//! Debug.Print size                     ' 41 elements
//! ```
//!
//! ## Common Patterns
//!
//! ### Pattern 1: Safe Array Iteration
//!
//! ```vb6
//! Sub ProcessArray(arr As Variant)
//!     Dim i As Long
//!     
//!     If Not IsArray(arr) Then Exit Sub
//!     
//!     For i = LBound(arr) To UBound(arr)
//!         Debug.Print arr(i)
//!     Next i
//! End Sub
//! ```
//!
//! ### Pattern 2: Array Size Function
//!
//! ```vb6
//! Function ArraySize(arr As Variant, Optional dimension As Long = 1) As Long
//!     If Not IsArray(arr) Then
//!         ArraySize = 0
//!     Else
//!         ArraySize = UBound(arr, dimension) - LBound(arr, dimension) + 1
//!     End If
//! End Function
//! ```
//!
//! ### Pattern 3: Copy Array With Correct Bounds
//!
//! ```vb6
//! Function CopyArray(source As Variant) As Variant
//!     Dim dest As Variant
//!     Dim i As Long
//!     
//!     If Not IsArray(source) Then Exit Function
//!     
//!     ReDim dest(LBound(source) To UBound(source))
//!     For i = LBound(source) To UBound(source)
//!         If IsObject(source(i)) Then
//!             Set dest(i) = source(i)
//!         Else
//!             dest(i) = source(i)
//!         End If
//!     Next i
//!     
//!     CopyArray = dest
//! End Function
//! ```
//!
//! ### Pattern 4: Check If Array Is Empty
//!
//! ```vb6
//! Function IsArrayEmpty(arr As Variant) As Boolean
//!     On Error Resume Next
//!     IsArrayEmpty = (UBound(arr) < LBound(arr))
//!     If Err.Number <> 0 Then IsArrayEmpty = True
//!     On Error GoTo 0
//! End Function
//! ```
//!
//! ### Pattern 5: Array Contains Value
//!
//! ```vb6
//! Function ArrayContains(arr As Variant, value As Variant) As Boolean
//!     Dim i As Long
//!     
//!     If Not IsArray(arr) Then Exit Function
//!     
//!     For i = LBound(arr) To UBound(arr)
//!         If arr(i) = value Then
//!             ArrayContains = True
//!             Exit Function
//!         End If
//!     Next i
//!     
//!     ArrayContains = False
//! End Function
//! ```
//!
//! ### Pattern 6: Find Element Index
//!
//! ```vb6
//! Function FindInArray(arr As Variant, value As Variant) As Long
//!     Dim i As Long
//!     
//!     FindInArray = -1  ' Not found
//!     If Not IsArray(arr) Then Exit Function
//!     
//!     For i = LBound(arr) To UBound(arr)
//!         If arr(i) = value Then
//!             FindInArray = i
//!             Exit Function
//!         End If
//!     Next i
//! End Function
//! ```
//!
//! ### Pattern 7: Reverse Array In Place
//!
//! ```vb6
//! Sub ReverseArray(arr As Variant)
//!     Dim i As Long
//!     Dim j As Long
//!     Dim temp As Variant
//!     
//!     If Not IsArray(arr) Then Exit Sub
//!     
//!     i = LBound(arr)
//!     j = UBound(arr)
//!     
//!     Do While i < j
//!         temp = arr(i)
//!         arr(i) = arr(j)
//!         arr(j) = temp
//!         i = i + 1
//!         j = j - 1
//!     Loop
//! End Sub
//! ```
//!
//! ### Pattern 8: Slice Array
//!
//! ```vb6
//! Function SliceArray(arr As Variant, startIndex As Long, endIndex As Long) As Variant
//!     Dim result() As Variant
//!     Dim i As Long
//!     Dim j As Long
//!     
//!     If Not IsArray(arr) Then Exit Function
//!     If startIndex < LBound(arr) Or endIndex > UBound(arr) Then Exit Function
//!     
//!     ReDim result(0 To endIndex - startIndex)
//!     j = 0
//!     For i = startIndex To endIndex
//!         result(j) = arr(i)
//!         j = j + 1
//!     Next i
//!     
//!     SliceArray = result
//! End Function
//! ```
//!
//! ### Pattern 9: Fill Array With Value
//!
//! ```vb6
//! Sub FillArray(arr As Variant, value As Variant)
//!     Dim i As Long
//!     
//!     If Not IsArray(arr) Then Exit Sub
//!     
//!     For i = LBound(arr) To UBound(arr)
//!         If IsObject(value) Then
//!             Set arr(i) = value
//!         Else
//!             arr(i) = value
//!         End If
//!     Next i
//! End Sub
//! ```
//!
//! ### Pattern 10: Multi-Dimensional Array Iteration
//!
//! ```vb6
//! Sub ProcessGrid(grid As Variant)
//!     Dim i As Long, j As Long
//!     
//!     If Not IsArray(grid) Then Exit Sub
//!     
//!     For i = LBound(grid, 1) To UBound(grid, 1)
//!         For j = LBound(grid, 2) To UBound(grid, 2)
//!             Debug.Print grid(i, j)
//!         Next j
//!     Next i
//! End Sub
//! ```
//!
//! ## Advanced Usage Examples
//!
//! ### Example 1: Generic Array Utilities Class
//!
//! ```vb6
//! Public Class ArrayUtils
//!     Public Function GetSize(arr As Variant, Optional dimension As Long = 1) As Long
//!         On Error GoTo ErrorHandler
//!         
//!         If Not IsArray(arr) Then
//!             GetSize = 0
//!         Else
//!             GetSize = UBound(arr, dimension) - LBound(arr, dimension) + 1
//!         End If
//!         Exit Function
//!         
//!     ErrorHandler:
//!         GetSize = 0
//!     End Function
//!     
//!     Public Function Clone(source As Variant) As Variant
//!         Dim dest As Variant
//!         Dim i As Long
//!         
//!         If Not IsArray(source) Then Exit Function
//!         
//!         ReDim dest(LBound(source) To UBound(source))
//!         For i = LBound(source) To UBound(source)
//!             If IsObject(source(i)) Then
//!                 Set dest(i) = source(i)
//!             Else
//!                 dest(i) = source(i)
//!             End If
//!         Next i
//!         
//!         Clone = dest
//!     End Function
//!     
//!     Public Function IndexOf(arr As Variant, value As Variant) As Long
//!         Dim i As Long
//!         
//!         IndexOf = -1
//!         If Not IsArray(arr) Then Exit Function
//!         
//!         For i = LBound(arr) To UBound(arr)
//!             If arr(i) = value Then
//!                 IndexOf = i
//!                 Exit Function
//!             End If
//!         Next i
//!     End Function
//!     
//!     Public Function Reverse(arr As Variant) As Variant
//!         Dim result As Variant
//!         Dim i As Long
//!         Dim j As Long
//!         
//!         If Not IsArray(arr) Then Exit Function
//!         
//!         ReDim result(LBound(arr) To UBound(arr))
//!         j = UBound(arr)
//!         For i = LBound(arr) To UBound(arr)
//!             result(j) = arr(i)
//!             j = j - 1
//!         Next i
//!         
//!         Reverse = result
//!     End Function
//! End Class
//! ```
//!
//! ### Example 2: Safe array accessor with bounds checking
//!
//! ```vb6
//! Public Class SafeArray
//!     Private m_data As Variant
//!     
//!     Public Sub Initialize(size As Long, Optional lowerBound As Long = 0)
//!         ReDim m_data(lowerBound To lowerBound + size - 1)
//!     End Sub
//!     
//!     Public Property Get Item(index As Long) As Variant
//!         If index < LBound(m_data) Or index > UBound(m_data) Then
//!             Err.Raise 9, "SafeArray", "Index out of bounds: " & index & _
//!                       " (valid range: " & LBound(m_data) & " to " & UBound(m_data) & ")"
//!         End If
//!         
//!         If IsObject(m_data(index)) Then
//!             Set Item = m_data(index)
//!         Else
//!             Item = m_data(index)
//!         End If
//!     End Property
//!     
//!     Public Property Let Item(index As Long, value As Variant)
//!         If index < LBound(m_data) Or index > UBound(m_data) Then
//!             Err.Raise 9, "SafeArray", "Index out of bounds"
//!         End If
//!         
//!         If IsObject(value) Then
//!             Set m_data(index) = value
//!         Else
//!             m_data(index) = value
//!         End If
//!     End Property
//!     
//!     Public Property Get LowerBound() As Long
//!         LowerBound = LBound(m_data)
//!     End Property
//!     
//!     Public Property Get UpperBound() As Long
//!         UpperBound = UBound(m_data)
//!     End Property
//!     
//!     Public Property Get Count() As Long
//!         Count = UBound(m_data) - LBound(m_data) + 1
//!     End Property
//! End Class
//! ```
//!
//! ### Example 3: Matrix Operations Helper
//!
//! ```vb6
//! Public Class MatrixHelper
//!     Public Function GetRowCount(matrix As Variant) As Long
//!         If Not IsArray(matrix) Then
//!             GetRowCount = 0
//!         Else
//!             GetRowCount = UBound(matrix, 1) - LBound(matrix, 1) + 1
//!         End If
//!     End Function
//!     
//!     Public Function GetColumnCount(matrix As Variant) As Long
//!         If Not IsArray(matrix) Then
//!             GetColumnCount = 0
//!         Else
//!             GetColumnCount = UBound(matrix, 2) - LBound(matrix, 2) + 1
//!         End If
//!     End Function
//!     
//!     Public Function GetRow(matrix As Variant, rowIndex As Long) As Variant
//!         Dim result() As Variant
//!         Dim j As Long
//!         Dim k As Long
//!         
//!         If Not IsArray(matrix) Then Exit Function
//!         
//!         ReDim result(LBound(matrix, 2) To UBound(matrix, 2))
//!         For j = LBound(matrix, 2) To UBound(matrix, 2)
//!             result(j) = matrix(rowIndex, j)
//!         Next j
//!         
//!         GetRow = result
//!     End Function
//!     
//!     Public Function GetColumn(matrix As Variant, colIndex As Long) As Variant
//!         Dim result() As Variant
//!         Dim i As Long
//!         
//!         If Not IsArray(matrix) Then Exit Function
//!         
//!         ReDim result(LBound(matrix, 1) To UBound(matrix, 1))
//!         For i = LBound(matrix, 1) To UBound(matrix, 1)
//!             result(i) = matrix(i, colIndex)
//!         Next i
//!         
//!         GetColumn = result
//!     End Function
//!     
//!     Public Sub Fill(matrix As Variant, value As Variant)
//!         Dim i As Long, j As Long
//!         
//!         If Not IsArray(matrix) Then Exit Sub
//!         
//!         For i = LBound(matrix, 1) To UBound(matrix, 1)
//!             For j = LBound(matrix, 2) To UBound(matrix, 2)
//!                 matrix(i, j) = value
//!             Next j
//!         Next i
//!     End Sub
//! End Class
//! ```
//!
//! ### Example 4: Dynamic Array Manager
//!
//! ```vb6
//! Public Class DynamicArray
//!     Private m_data() As Variant
//!     Private m_count As Long
//!     Private m_lowerBound As Long
//!     
//!     Private Sub Class_Initialize()
//!         m_count = 0
//!         m_lowerBound = 0
//!         ReDim m_data(m_lowerBound To m_lowerBound + 9)  ' Initial capacity of 10
//!     End Sub
//!     
//!     Public Sub Add(value As Variant)
//!         If m_count > UBound(m_data) - LBound(m_data) Then
//!             ' Resize array (double capacity)
//!             ReDim Preserve m_data(LBound(m_data) To UBound(m_data) * 2 + 1)
//!         End If
//!         
//!         If IsObject(value) Then
//!             Set m_data(LBound(m_data) + m_count) = value
//!         Else
//!             m_data(LBound(m_data) + m_count) = value
//!         End If
//!         
//!         m_count = m_count + 1
//!     End Sub
//!     
//!     Public Function ToArray() As Variant
//!         Dim result() As Variant
//!         Dim i As Long
//!         
//!         If m_count = 0 Then
//!             ToArray = Array()
//!             Exit Function
//!         End If
//!         
//!         ReDim result(m_lowerBound To m_lowerBound + m_count - 1)
//!         For i = 0 To m_count - 1
//!             If IsObject(m_data(LBound(m_data) + i)) Then
//!                 Set result(m_lowerBound + i) = m_data(LBound(m_data) + i)
//!             Else
//!                 result(m_lowerBound + i) = m_data(LBound(m_data) + i)
//!             End If
//!         Next i
//!         
//!         ToArray = result
//!     End Function
//!     
//!     Public Property Get Count() As Long
//!         Count = m_count
//!     End Property
//!     
//!     Public Property Get Capacity() As Long
//!         Capacity = UBound(m_data) - LBound(m_data) + 1
//!     End Property
//! End Class
//! ```
//!
//! ## Error Handling
//!
//! `LBound` can raise errors in specific cases:
//!
//! ```vb6
//! ' Error 9: Subscript out of range - dimension exceeds array dimensions
//! Dim arr(5, 10) As Integer
//! ' Debug.Print LBound(arr, 3)  ' Error 9 - only 2 dimensions
//! ```
//!
//! ```vb6
//! ' Error 9: Array not dimensioned (dynamic arrays)
//! Dim dynArr() As String
//! ' Debug.Print LBound(dynArr)  ' Error 9 - not dimensioned yet
//! ```
//!
//! ```vb6
//! ' Safe pattern with error handling
//! Function GetLowerBound(arr As Variant, Optional dimension As Long = 1) As Long
//!     On Error Resume Next
//!     GetLowerBound = LBound(arr, dimension)
//!     If Err.Number <> 0 Then
//!         GetLowerBound = -1  ' Indicate error
//!     End If
//!     On Error GoTo 0
//! End Function
//! ```

use crate::error::{VBError, VBResult};
use crate::value::{VBLong, VBVariant};

/// Implementation of the `LBound` function.
///
/// Returns the lower bound (smallest available subscript) for the specified
/// dimension of an array.
///
/// VB6 behavior:
/// - Default dimension is 1 (first dimension) when omitted
/// - Dimension parameter is 1-based (1 = first dimension, 2 = second, ...)
/// - Raises error 9 (Subscript out of range) if the array is not initialized
///   or if `dimension` exceeds the number of array dimensions
/// - Raises error 9 if `dimension` is less than 1
pub fn lbound(array: &VBVariant, dimension: Option<&VBLong>) -> VBResult<VBVariant> {
    // The dimension parameter is 1-based in VB6; convert to 0-based index
    let dim_index = match dimension {
        None => 0,
        Some(d) if d.as_i32() >= 1 => (d.as_i32() - 1) as usize,
        Some(_) => return Err(VBError::subscript_out_of_range()),
    };

    // Validate that the argument is an array
    let VBVariant::Array(arr) = array else {
        return Err(VBError::type_mismatch());
    };

    let lower = arr.lower_bound(dim_index)?;
    Ok(VBVariant::Long(lower))
}

#[cfg(test)]
mod tests {
    use super::lbound;
    use crate::array::{ArrayDimension, ArrayValue};
    use crate::error::err_number;
    use crate::types::VBType;
    use crate::value::{VBLong, VBVariant};

    fn make_array(bounds: (i32, i32)) -> VBVariant {
        let (lower, upper) = bounds;
        let dim_len = (upper - lower + 1) as usize;
        let data: Vec<VBVariant> = (0..dim_len).map(|_| VBVariant::Empty).collect();
        VBVariant::Array(ArrayValue::from_vec_with_bounds(VBType::Long, data, lower))
    }

    #[test]
    fn default_dimension_returns_first_lower_bound() {
        let arr = make_array((0, 5));
        let result = lbound(&arr, None).unwrap();
        assert_eq!(result, VBVariant::Long(0));
    }

    #[test]
    fn explicit_first_dimension() {
        let arr = make_array((0, 5));
        let result = lbound(&arr, Some(&VBLong::from(1))).unwrap();
        assert_eq!(result, VBVariant::Long(0));
    }

    #[test]
    fn non_zero_lower_bound() {
        let arr = make_array((5, 10));
        let result = lbound(&arr, None).unwrap();
        assert_eq!(result, VBVariant::Long(5));
    }

    #[test]
    fn negative_lower_bound() {
        let arr = make_array((-2, 3));
        let result = lbound(&arr, None).unwrap();
        assert_eq!(result, VBVariant::Long(-2));
    }

    #[test]
    fn multi_dimensional_first_dim() {
        let dims = [ArrayDimension::new(1, 5), ArrayDimension::new(0, 3)];
        let arr = VBVariant::Array(ArrayValue::new_fixed(VBType::Long, &dims).unwrap());
        let result = lbound(&arr, None).unwrap();
        assert_eq!(result, VBVariant::Long(1));
    }

    #[test]
    fn multi_dimensional_second_dim() {
        let dims = [ArrayDimension::new(1, 5), ArrayDimension::new(0, 3)];
        let arr = VBVariant::Array(ArrayValue::new_fixed(VBType::Long, &dims).unwrap());
        let result = lbound(&arr, Some(&VBLong::from(2))).unwrap();
        assert_eq!(result, VBVariant::Long(0));
    }

    #[test]
    fn dimension_out_of_range() {
        let dims = [ArrayDimension::new(1, 5), ArrayDimension::new(0, 3)];
        let arr = VBVariant::Array(ArrayValue::new_fixed(VBType::Long, &dims).unwrap());
        let err = lbound(&arr, Some(&VBLong::from(3))).unwrap_err();
        assert_eq!(err.number, err_number::SUBSCRIPT_OUT_OF_RANGE);
    }

    #[test]
    fn zero_dimension_is_error() {
        let arr = make_array((0, 5));
        let err = lbound(&arr, Some(&VBLong::from(0))).unwrap_err();
        assert_eq!(err.number, err_number::SUBSCRIPT_OUT_OF_RANGE);
    }

    #[test]
    fn negative_dimension_is_error() {
        let arr = make_array((0, 5));
        let err = lbound(&arr, Some(&VBLong::from(-1))).unwrap_err();
        assert_eq!(err.number, err_number::SUBSCRIPT_OUT_OF_RANGE);
    }

    #[test]
    fn uninitialized_dynamic_array_errors() {
        let arr = VBVariant::Array(ArrayValue::new_dynamic(VBType::Long));
        let err = lbound(&arr, None).unwrap_err();
        assert_eq!(err.number, err_number::SUBSCRIPT_OUT_OF_RANGE);
    }

    #[test]
    fn non_array_is_type_mismatch() {
        let err = lbound(&VBVariant::from_string("not an array"), None).unwrap_err();
        assert_eq!(err.number, err_number::TYPE_MISMATCH);
    }

    #[test]
    fn paramarray_has_zero_lower_bound() {
        // ParamArray always has LBound = 0
        let data: Vec<VBVariant> = vec![VBVariant::from_string("a"); 3];
        let arr = VBVariant::Array(ArrayValue::from_vec_with_bounds(VBType::Variant, data, 0));
        let result = lbound(&arr, None).unwrap();
        assert_eq!(result, VBVariant::Long(0));
    }
}