I would like to know if there is a way of knowing if parameter can be iterated over using For Each
loop before using it.
Method AddRange
This method can accept various data types. Ideally, user would pass an container which contains 0 or more items.
' Class name - Employees
Private pItems As New Collection
Public Sub AddRange(ByVal Items As Variant)
' Here should be logic of detecting if Items can be iterated over.
' If Items is not a container, throw an error.
' Otherwise, proceed with adding items.
Dim Item As Variant
For Each Item In Items
pItems.Add Item
Next Item
End Sub
Client code
Public Sub Start()
Dim Emps As New Employees
Dim Department As New Collection 'of Employees
Emps.AddRange Department
Dim AnotherDepartment(0) As Variant 'of Employees
Emps.AddRange AnotherDepartment
Dim NewJoiners As New Employees
Emps.AddRange NewJoiners
' This should not work.
Dim EmployeeName As String
Emps.AddRange EmployeeName
End Sub