By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,219 Members | 2,140 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,219 IT Pros & Developers. It's quick & easy.

For Loop giving from Index Number

P: 1
I have a for loop that changes from 0 to 4, 5 to 9, 10 to 14, 15 to 19, then 20 to 24 each time i hit the play button. (both numbers in the range determined by a variable called CheckDynamicA and CheckDynamicB.

Each set of 5 numbers is 5 check boxes representing a player in my game im designing.
It works except for the last time I hit the "play button"

Each time I hit the play button a variable called T is given T = T + 1.
One the last click of the play button I have decision logic that turns T back to 0, and should bring the 20 to 24 back to 0 to 4.

My problem is on the last click of the button though, as the debugger says that the value of the variable = 25, and not 24, which is out of range. No where in my code (ive checked dozens of times for errors) do I have it set to 25 in the "19 to 24", and no where does it add +1 to the max range.


Expand|Select|Wrap|Line Numbers
  1. Private Sub PlayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PlayButton.Click
  2.         Dim CardCheckBoxArray() As CheckBox = {CardCheckBox1, CardCheckBox2, CardCheckBox3, CardCheckBox4, CardCheckBox5, CardCheckBox6, CardCheckBox7, CardCheckBox8, CardCheckBox9, _
  3.                                                        CardCheckBox10, CardCheckBox11, CardCheckBox12, CardCheckBox13, CardCheckBox14, CardCheckBox15, CardCheckBox16, CardCheckBox17, _
  4.                                                        CardCheckBox18, CardCheckBox19, CardCheckBox20, CardCheckBox21, CardCheckBox22, CardCheckBox23, CardCheckBox24, CardCheckBox25}
  5.         Dim HitPoints() As Label = {Nothing, HitPoints1, HitPoints2, HitPoints3, HitPoints4, HitPoints5}
  6.         Dim n As Integer
  7.         Dim DisableCheckBoxInteger As Integer
  8.  
  9.  
  10.         'fail safe to make sure checkboxes are checked
  11.         If CardCheckBox1.Checked = False And CardCheckBox2.Checked = False And CardCheckBox3.Checked = False And CardCheckBox4.Checked = False And CardCheckBox5.Checked = False And CardCheckBox6.Checked = False And _
  12.             CardCheckBox7.Checked = False And CardCheckBox8.Checked = False And CardCheckBox9.Checked = False And CardCheckBox10.Checked = False And CardCheckBox11.Checked = False And CardCheckBox12.Checked = False And _
  13.             CardCheckBox13.Checked = False And CardCheckBox14.Checked = False And CardCheckBox15.Checked = False And CardCheckBox16.Checked = False And CardCheckBox17.Checked = False And CardCheckBox18.Checked = False And _
  14.             CardCheckBox19.Checked = False And CardCheckBox20.Checked = False And CardCheckBox21.Checked = False And CardCheckBox22.Checked = False And CardCheckBox23.Checked = False And CardCheckBox24.Checked = False And _
  15.             CardCheckBox25.Checked = False Then
  16.  
  17.             WarningLabel.Text = "Please select your card(s)"
  18.         Else
  19.             'Player 1
  20.             For Me.Checks = ChecksDynamicA To ChecksDynamicB
  21.                 If T = 0 And CardCheckBoxArray(Checks).Checked = True Then
  22.                     For DisableCheckBoxInteger = 0 To 24
  23.                         Select Case DisableCheckBoxInteger
  24.                             Case 0 To 4
  25.                                 CardCheckBoxArray(DisableCheckBoxInteger).Visible = False
  26.                             Case 5 To 9
  27.                                 CardCheckBoxArray(DisableCheckBoxInteger).Visible = True
  28.                             Case 10 To 24
  29.                                 CardCheckBoxArray(DisableCheckBoxInteger).Visible = False
  30.                         End Select
  31.                     Next DisableCheckBoxInteger
  32.                     AtkPlayerDialog.Player1.Enabled = False
  33.                     AtkPlayerDialog.Player2.Enabled = True
  34.                     AtkPlayerDialog.Player3.Enabled = True
  35.                     AtkPlayerDialog.Player4.Enabled = True
  36.                     AtkPlayerDialog.Player5.Enabled = True
  37.                     ChecksDynamicA = 0
  38.                     ChecksDynamicB = 4
  39.                     'Player 2
  40.                 ElseIf T = 1 And CardCheckBoxArray(Checks).Checked = True Then
  41.                     For DisableCheckBoxInteger = 0 To 24
  42.                         Select Case DisableCheckBoxInteger
  43.                             Case 0 To 9
  44.                                 CardCheckBoxArray(DisableCheckBoxInteger).Visible = False
  45.                             Case 10 To 14
  46.                                 CardCheckBoxArray(DisableCheckBoxInteger).Visible = True
  47.                             Case 15 To 24
  48.                                 CardCheckBoxArray(DisableCheckBoxInteger).Visible = False
  49.                         End Select
  50.                     Next DisableCheckBoxInteger
  51.                     AtkPlayerDialog.Player1.Enabled = True
  52.                     AtkPlayerDialog.Player2.Enabled = False
  53.                     AtkPlayerDialog.Player3.Enabled = True
  54.                     AtkPlayerDialog.Player4.Enabled = True
  55.                     AtkPlayerDialog.Player5.Enabled = True
  56.                     ChecksDynamicA = 5
  57.                     ChecksDynamicB = 9
  58.                     'Player 3
  59.                 ElseIf T = 2 And CardCheckBoxArray(Checks).Checked = True Then
  60.                     For DisableCheckBoxInteger = 0 To 24
  61.                         Select Case DisableCheckBoxInteger
  62.                             Case 0 To 14
  63.                                 CardCheckBoxArray(DisableCheckBoxInteger).Visible = False
  64.                             Case 15 To 19
  65.                                 CardCheckBoxArray(DisableCheckBoxInteger).Visible = True
  66.                             Case 20 To 24
  67.                                 CardCheckBoxArray(DisableCheckBoxInteger).Visible = False
  68.                         End Select
  69.                     Next DisableCheckBoxInteger
  70.                     AtkPlayerDialog.Player1.Enabled = True
  71.                     AtkPlayerDialog.Player2.Enabled = True
  72.                     AtkPlayerDialog.Player3.Enabled = False
  73.                     AtkPlayerDialog.Player4.Enabled = True
  74.                     AtkPlayerDialog.Player5.Enabled = True
  75.                     ChecksDynamicA = 10
  76.                     ChecksDynamicB = 14
  77.                     'Player 4
  78.                 ElseIf T = 3 And CardCheckBoxArray(Checks).Checked = True Then
  79.                     For DisableCheckBoxInteger = 0 To 24
  80.                         Select Case DisableCheckBoxInteger
  81.                             Case 0 To 19
  82.                                 CardCheckBoxArray(DisableCheckBoxInteger).Visible = False
  83.                             Case 20 To 24
  84.                                 CardCheckBoxArray(DisableCheckBoxInteger).Visible = True
  85.                         End Select
  86.                     Next DisableCheckBoxInteger
  87.                     AtkPlayerDialog.Player1.Enabled = True
  88.                     AtkPlayerDialog.Player2.Enabled = True
  89.                     AtkPlayerDialog.Player3.Enabled = True
  90.                     AtkPlayerDialog.Player4.Enabled = False
  91.                     AtkPlayerDialog.Player5.Enabled = True
  92.                     ChecksDynamicA = 15
  93.                     ChecksDynamicB = 19
  94.                     'Player 5
  95.                 ElseIf T = 4 And CardCheckBoxArray(Checks).Checked = True Then
  96.                     For DisableCheckBoxInteger = 0 To 24
  97.                         Select Case DisableCheckBoxInteger
  98.                             Case 0 To 4
  99.                                 CardCheckBoxArray(DisableCheckBoxInteger).Visible = True
  100.                             Case 5 To 24
  101.                                 CardCheckBoxArray(DisableCheckBoxInteger).Visible = False
  102.                         End Select
  103.                     Next DisableCheckBoxInteger
  104.                     AtkPlayerDialog.Player1.Enabled = True
  105.                     AtkPlayerDialog.Player2.Enabled = True
  106.                     AtkPlayerDialog.Player3.Enabled = True
  107.                     AtkPlayerDialog.Player4.Enabled = True
  108.                     AtkPlayerDialog.Player5.Enabled = False
  109.                     ChecksDynamicA = 20
  110.                     ChecksDynamicB = 24
  111.                 End If
  112.             Next Checks
  113.             'Code for choosing which player to attack
  114.             AtkPlayerDialog.ShowDialog()
  115.             If AtkPlayerDialog.DialogResult = 1 Then
  116.                 n = 1
  117.             ElseIf AtkPlayerDialog.DialogResult = 2 Then
  118.                 n = 2
  119.             ElseIf AtkPlayerDialog.DialogResult = 3 Then
  120.                 n = 3
  121.             ElseIf AtkPlayerDialog.DialogResult = 4 Then
  122.                 n = 4
  123.             ElseIf AtkPlayerDialog.DialogResult = 5 Then
  124.                 n = 5
  125.             End If
  126.             'CheckedLoop
  127.             For Me.Checks = ChecksDynamicA To ChecksDynamicB
  128.                 'Supplement Numbers(1-5) variable in loop
  129.                 If Checks = Checks Then
  130.                     NumberChecks = NumberArray(Checks)
  131.                 End If
  132.                 'Play Card(s)
  133.                 If T = 0 Then
  134.                     If CardCheckBoxArray(Checks).Checked = True AndAlso Player1HandGroup(NumberChecks).QuantityInteger > 0 Then
  135.                         'Subtract Hitpoints when damage is delt
  136.                         Player1HandGroup(n).HitPoints -= Player1HandGroup(NumberChecks).DamageInteger
  137.                         HitPoints(n).Text = Player1HandGroup(n).HitPoints.ToString
  138.                         'When player plays hand, card quantity is removed from hand to the discard pile.
  139.                         If Player1HandGroup(NumberChecks).CardType = "Weapon" Then
  140.                             DiscardDialog.ShowDialog()
  141.                             'Choose if to Discard Weapon after usage
  142.                             If DiscardDialog.DialogResult = Windows.Forms.DialogResult.OK Then
  143.                                 Call GrabFromDeckAndDiscard()
  144.                             End If
  145.                         Else
  146.                             Call GrabFromDeckAndDiscard()
  147.                         End If
  148.                     End If
  149.                 ElseIf T = 1 Then
  150.                     If CardCheckBoxArray(Checks).Checked = True AndAlso Player1HandGroup(NumberChecks).QuantityInteger2 > 0 Then
  151.                         'Subtract Hitpoints when damage is delt
  152.                         Player1HandGroup(n).HitPoints -= Player1HandGroup(NumberChecks).DamageInteger
  153.                         HitPoints(n).Text = Player1HandGroup(n).HitPoints.ToString
  154.                         'When player plays hand, card quantity is removed from hand to the discard pile.
  155.                         If Player1HandGroup(NumberChecks).CardType = "Weapon" Then
  156.                             DiscardDialog.ShowDialog()
  157.                             'Choose if to Discard Weapon after usage
  158.                             If DiscardDialog.DialogResult = Windows.Forms.DialogResult.OK Then
  159.                                 Call GrabFromDeckAndDiscard()
  160.                             End If
  161.                         Else
  162.                             Call GrabFromDeckAndDiscard()
  163.                         End If
  164.                     End If
  165.                 ElseIf T = 2 Then
  166.                     If CardCheckBoxArray(Checks).Checked = True AndAlso Player1HandGroup(NumberChecks).QuantityInteger3 > 0 Then
  167.                         'Subtract Hitpoints when damage is delt
  168.                         Player1HandGroup(n).HitPoints -= Player1HandGroup(NumberChecks).DamageInteger
  169.                         HitPoints(n).Text = Player1HandGroup(n).HitPoints.ToString
  170.                         'When player plays hand, card quantity is removed from hand to the discard pile.
  171.                         If Player1HandGroup(NumberChecks).CardType = "Weapon" Then
  172.                             DiscardDialog.ShowDialog()
  173.                             'Choose if to Discard Weapon after usage
  174.                             If DiscardDialog.DialogResult = Windows.Forms.DialogResult.OK Then
  175.                                 Call GrabFromDeckAndDiscard()
  176.                             End If
  177.                         Else
  178.                             Call GrabFromDeckAndDiscard()
  179.                         End If
  180.                     End If
  181.                 ElseIf T = 3 Then
  182.                     If CardCheckBoxArray(Checks).Checked = True AndAlso Player1HandGroup(NumberChecks).QuantityInteger4 > 0 Then
  183.                         'Subtract Hitpoints when damage is delt
  184.                         Player1HandGroup(n).HitPoints -= Player1HandGroup(NumberChecks).DamageInteger
  185.                         HitPoints(n).Text = Player1HandGroup(n).HitPoints.ToString
  186.                         'When player plays hand, card quantity is removed from hand to the discard pile.
  187.                         If Player1HandGroup(NumberChecks).CardType = "Weapon" Then
  188.                             DiscardDialog.ShowDialog()
  189.                             'Choose if to Discard Weapon after usage
  190.                             If DiscardDialog.DialogResult = Windows.Forms.DialogResult.OK Then
  191.                                 Call GrabFromDeckAndDiscard()
  192.                             End If
  193.                         Else
  194.                             Call GrabFromDeckAndDiscard()
  195.                         End If
  196.                     End If
  197.                 ElseIf T = 4 Then
  198.                     If CardCheckBoxArray(Checks).Checked = True AndAlso Player1HandGroup(NumberChecks).QuantityInteger5 > 0 Then
  199.                         'Subtract Hitpoints when damage is delt
  200.                         Player1HandGroup(n).HitPoints -= Player1HandGroup(NumberChecks).DamageInteger
  201.                         HitPoints(n).Text = Player1HandGroup(n).HitPoints.ToString
  202.                         'When player plays hand, card quantity is removed from hand to the discard pile.
  203.                         If Player1HandGroup(NumberChecks).CardType = "Weapon" Then
  204.                             DiscardDialog.ShowDialog()
  205.                             'Choose if to Discard Weapon after usage
  206.                             If DiscardDialog.DialogResult = Windows.Forms.DialogResult.OK Then
  207.                                 Call GrabFromDeckAndDiscard()
  208.                             End If
  209.                         Else
  210.                             Call GrabFromDeckAndDiscard()
  211.                         End If
  212.                     End If
  213.                 End If
  214.                 ChecksLabel.Text = Checks.ToString
  215.             Next
  216.             T += 1
  217.             CardCheckBoxArray(Checks).Checked = False
  218.             If T = 5 Then
  219.                 T = 0
  220.             End If
  221.         End If
  222.     End Sub
Mar 25 '13 #1
Share this Question
Share on Google+
5 Replies


Rabbit
Expert Mod 10K+
P: 12,383
Please use code tags when posting code.

You have a lot of loops, it would help if you told us which loop you're talking about.
Mar 25 '13 #2

Expert 5K+
P: 8,434
I realise it's been a few months, and you've probably found an answer by now. However, for anyone who finds this in a search in future I wanted to add a comment.

I haven't read the code in detail, but based on the question this sounds like a classic problem experienced in many programming languages, caused by the fact that so few people understand how a For loop actually works. Take the following example:
Expand|Select|Wrap|Line Numbers
  1. For I = 1 To 99
  2.   ' Do something
  3. Next
  4.  
After executing this, you might reasonably expect the value of I to be 99. However, you'd be wrong.

The value of I is initially set to 1. Each time execution reaches the Next statement, VB first increments I and then tests whether it has passed the limit (99). If so, it drops through to the code after the Next statement. Otherwise, it goes bacak and "does something" again.

So in this example the value of I is incremented to 100, then VB spots that it's finished and stops looping. Hence, you come out with your variable holding a value higher than you might expect.
Jul 20 '13 #3

!NoItAll
100+
P: 296
You are changing the values of the upper and lower bounds of the loop inside the loop. That is really really bad design and you will likely never get what you expect.

Des
Jul 31 '13 #4

Expert 5K+
P: 8,434
So he is.

I don't agree that it's "really really bad design" but you certainly do need to know what you're doing, and what the effects will be. Given the reported problem, that may not be the case here.
Aug 1 '13 #5

!NoItAll
100+
P: 296
I would argue that changing the value of either bound in a for loop creates difficult to support code - it's hard to follow the logic, especially in this example of nested for loops with changing boundaries. Especially since the internal code changes both boundaries. I gotta say, "ewww." I suppose there are valid reasons for doing something like this - but I would challenge anyone on my team to try a different design if this code came under review. Code that is hard to read and follow makes it difficult to find issues - even if not related to the design...
Aug 1 '13 #6

Post your reply

Sign in to post your reply or Sign up for a free account.