StackOverFlowException was Unhandled on For Loop

I just made my Card Selection Dynamic, instead of a sequence of 5 if/elseif statements.

Private Sub PlayElse()
    Dim StartHeads As Integer
    Dim CardCheckBoxArray() As CheckBox = {CardCheckBox1, CardCheckBox2, CardCheckBox3, CardCheckBox4, CardCheckBox5, CardCheckBox6, CardCheckBox7, CardCheckBox8, CardCheckBox9, _
                                          CardCheckBox10, CardCheckBox11, CardCheckBox12, CardCheckBox13, CardCheckBox14, CardCheckBox15, CardCheckBox16, CardCheckBox17, _
                                          CardCheckBox18, CardCheckBox19, CardCheckBox20, CardCheckBox21, CardCheckBox22, CardCheckBox23, CardCheckBox24, CardCheckBox25}
    'Reset Number Generator
    Number = (DeckGroup(Rnd.Next(0, DeckGroup.Count)).ID)

    'card 1-5
    For StartHeads = 0 To 4
        If CardCheckBoxArray(StartHeads).Checked = True And DeckGroup(Number).QuantityInteger > 0 Then
            'Grab New Card From Deck
            DeckGroup(Number).QuantityInteger -= 1
            Player1HandGroup(Number).QuantityInteger += 1
            CardTypeArray(StartHeads) = Player1HandGroup(Number).CardType
            CardCheckBoxArray(StartHeads).Text = Player1HandGroup(Number).CardNameString
            NumberArray(StartHeads) = Number
        Else
            Call PlayElse()
        End If

This was my original code (note there is an End If. I just didn't want to repeat the code through cards 2-5)

If CardCheckBox1.Checked = True And DeckGroup(Number).QuantityInteger > 0 Then
    'Grab New Card From Deck
    DeckGroup(Number).QuantityInteger -= 1
    Player1HandGroup(Number).QuantityInteger += 1
    CardTypeArray(0) = Player1HandGroup(Number).CardType
    CardCheckBox1.Text = Player1HandGroup(Number).CardNameString
    NumberArray(0) = Number

Nothing has really changed except, any spot with a 0 is no represented by the corresponding number in the loop. I have several loops similar to this one in my project, yet somehow this is the one that gets a stackoverflow exception. The CardCheckBoxes in the CardCheckBoxArray goes up to 25 because there are 5 players in the game. Any CardCheckBox past 6 in this particular statement simply isn't accessed, since this logic only represents player 1's cards. (Forgot to mention the continue buttons leads to the top of the sub-procedure when the error occurs, highlighting Private Sub Playelse().)

Upvotes: 0

Views: 1051

Answers (2)

Steven Doggart
Steven Doggart

Reputation: 43743

The most common cause for stack overflow exceptions, as is the case here, is when you have a recursive method which calls itself too many times. Each time a method calls itself, it adds more data to the stack. The data isn't removed from the stack until the method exits. For instance, the following method is guaranteed to throw a StackOverFlowException:

Public Sub Fail(count As Integer)
    Fail(count + 1)
End Sub

As you can see, when the Fail method, in my example is called, it infinitely calls itself over and over again, never exiting. Therefore, the data on the stack keeps growing until eventually it runs out of space and throws the exception.

Upvotes: 1

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112782

Sub PlayElse calls itself recursively in the Else case. You always must insure that recursions terminate at some point, otherwise you end up in an infinite recursion that throws a stack overflow exception.

The If condition says ... And DeckGroup(Number).QuantityInteger > 0. If this is the case DeckGroup(Number).QuantityInteger is decremented. At some point it will be zero and the Else case will be executed, calling PlayElse which will again execute the Else case because QuantityInteger is zero now, and so on.

Should Call PlayElse() be at the end of the Then case instead?

Upvotes: 0

Related Questions