Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Recursive Function Calls 1

Status
Not open for further replies.

NeilFrank

Programmer
Mar 12, 2000
167
CA
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


 
In the function it looks like "UniqueKey" is undefined and was intended to be the function name itself, i.e., "UniqueNum".
 

My mistake - a typo (I abridged the code for this post)!
Here is everything again, I hope correctly:

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 UniqueNum.
'Function exited with UniqueNum = 0
lNum = UniqueNum

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
 
In the function replace

lNum = UniqueNum

with the correct calling convention:

lNum = UniqueNum()
 
That did the trick, thanks!

I guess this proves the old adage 's/he with the complete compendium of correct calling conventions can collect copious congratulations; s/he without can contemplate conundrums, catastrophes and code clashes'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top