Problem Calling Function A In Funtion A

Last post 07-05-2009 8:28 PM by mmmisa83. 4 replies.

Sort Posts:

  • Problem Calling Function A In Funtion A

    07-05-2009, 4:13 AM
    • Member
      9 point Member
    • mmmisa83
    • Member since 04-03-2007, 8:33 AM
    • Posts 12

    Hi,

    I have this simple function (below) that randomizes number between 1 to 10.

    This function works well, except when Random() returns 5, which means intLot=5, randomizeAgain = true, and then the same function RandomizeNo will be called to randomize again. (refer the code below)

    Even the new value of intLot equals other than 5 next time, say 4, the funtion returns 0.

    I have no idea why.

    Function RandomizeNo(ByVal totalLot As Integer) As Integer
            Dim intLot As Integer
    
            randomizeAgain = False
            
    	intLot = New Random().Next(1, 10)
    
    	For k = 1 To totalLot - 1
    	  If intLot = 5 Then
    	    randomizeAgain = True
    	    Exit For
    	  End If
    	Next
    
            If randomizeAgain = True Then
                RandomizeNo(totalLot)
            End If
    
            Return intLot
    End Function


    Please help.

    Thanks in advance.

  • Re: Problem Calling Function A In Funtion A

    07-05-2009, 7:35 AM
    Answer
    • Member
      195 point Member
    • anders__f
    • Member since 03-30-2009, 10:44 AM
    • Sweden
    • Posts 34

    Look at the exit point for this function, the return statement at line 19. It will always return intLot, no matter if line 16 was executed or not.

    The thing is that when you call the function again in line 16, you will get a new variable scope, and the function will not use the same variable/values as in the calling scope.

    I have not tried your code, but probably it will be enough to change line 16 to

    Return RandomizeNo(totalLot)


  • Re: Problem Calling Function A In Funtion A

    07-05-2009, 8:33 AM
    • Member
      9 point Member
    • mmmisa83
    • Member since 04-03-2007, 8:33 AM
    • Posts 12

    Thanks anders__f for replying. But I dont think that would solve the problem, it instead will cause infinite looping because "Return RandomizeNo(totalLot)" keeps calling the function.

    This function returns intLot, if the value of intLot is not 5. Otherwise the function will be called.

    Any other suggestion? Thanks again.

  • Re: Problem Calling Function A In Funtion A

    07-05-2009, 8:44 AM
    Answer
    • Member
      195 point Member
    • anders__f
    • Member since 03-30-2009, 10:44 AM
    • Sweden
    • Posts 34

    Well, it would only call the function again if randomizeAgain = True, and randomizeAgain is only set to true if intLot = 5. Isn't that what you want?

    These code snippets are equivalent:


    If randomizeAgain = True Then
        Return RandomizeNo(totalLot)
    End If
    
    Return intLot


    If randomizeAgain = True Then
        Return RandomizeNo(totalLot)
    Else
        Return intLot
    End If


    Just another thing, why do you use a for loop? The looping variable k is never used. And this in turn means that the parameter totalLot is in fact never used. What exactly do you want this function to return for different parameter values?

  • Re: Problem Calling Function A In Funtion A

    07-05-2009, 8:28 PM
    • Member
      9 point Member
    • mmmisa83
    • Member since 04-03-2007, 8:33 AM
    • Posts 12

    Ah, I think you are right. Your first reply suggests to change line 16 to

    Return RandomizeNo(totalLot) 

    but what I did was changing line 19 with the suggested code. That's why it caused infinite looping LOL.

    Yes, both snippets below work.

    If randomizeAgain = True Then
      Return RandomizeNo(totalLot)
    End If
    
      Return intLot 


    If randomizeAgain = True Then
      Return RandomizeNo(totalLot)
    Else
      Return intLot
    End If

    Actually I modified/simplified the function a bit to make it easier for readers to see the function flow. I just wanted to mention the function calling in the function assuming readers would ignore the For Looping part.

    Well it works now! Just by adding the word 'Return' in line 16 ;)

    Thanks a lot.

Page 1 of 1 (5 items)