In order to generate a unique number I wrote the following simple Function. It fails to do the job because there is no recursive Function call. However, using an analogous Subroutine works because the desired recursive call to the Sub _does_ happen.
Is this a VB feature, and, if so, why?
Public gcollUniqNum as New Collection
Public glUniqNum as Long
Public Function UniqueNum() As Long
Dim lNum As Long
lNum = GenRan(1)
If InColl(gcollUniqNum, CStr(lNum)) = True Then
'NO recursive call to UniqueKey.
'Function exited with UniqueKey = 0
lNum = UniqueKey
Else
gcollUniqNum.Add lNum, CStr(lNum)
UniqueNum = lNum
End If
End Function
Public Sub GenerateUniqueNumber()
Dim lNum As Long
lNum = GenRan(1)
If InColl(gcollUniqNum, CStr(lNum)) = True Then
'DOES result in recursive call
GenerateUniqueNumber
Else
gcollUniqNum.Add lNum, CStr(lNum)
glUniqNum = lNum
End If
End Sub
Public Function InCollection(collDesignated As Collection, Key As String) As Boolean
On Error GoTo NotThere
'VB generates a run-time error if Key is not in collDesignated
If collDesignated.Item(Key) <> "" Then
InCollection = True
Exit Function
End If
NotThere:
End Function
Is this a VB feature, and, if so, why?
Public gcollUniqNum as New Collection
Public glUniqNum as Long
Public Function UniqueNum() As Long
Dim lNum As Long
lNum = GenRan(1)
If InColl(gcollUniqNum, CStr(lNum)) = True Then
'NO recursive call to UniqueKey.
'Function exited with UniqueKey = 0
lNum = UniqueKey
Else
gcollUniqNum.Add lNum, CStr(lNum)
UniqueNum = lNum
End If
End Function
Public Sub GenerateUniqueNumber()
Dim lNum As Long
lNum = GenRan(1)
If InColl(gcollUniqNum, CStr(lNum)) = True Then
'DOES result in recursive call
GenerateUniqueNumber
Else
gcollUniqNum.Add lNum, CStr(lNum)
glUniqNum = lNum
End If
End Sub
Public Function InCollection(collDesignated As Collection, Key As String) As Boolean
On Error GoTo NotThere
'VB generates a run-time error if Key is not in collDesignated
If collDesignated.Item(Key) <> "" Then
InCollection = True
Exit Function
End If
NotThere:
End Function