473,320 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

For Loop giving from Index Number

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
5 1371
Rabbit
12,516 Expert Mod 8TB
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
Killer42
8,435 Expert 8TB
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
297 100+
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
Killer42
8,435 Expert 8TB
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
297 100+
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

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

Similar topics

4
by: PhilC | last post by:
Hi Folks, If I have an array holding a pair of numbers, and that pairing is unique, is there a way that I can find the array index number for that pair? Thanks, PhilC
2
by: deko | last post by:
Can I return the index number of an array if all I have is the element? For example, if I want to index the alphabet, I can put the letters in the array: Dim varLtr As Variant varLtr =...
2
by: Maziar Aflatoun | last post by:
Hi, Does anyone know how I can return the index number of a ListBox? Ex. How do I use lbxMyListBox.Items.IndexOf to search for the item that has a Text="Name" Thanks Maz.
1
by: Cordine | last post by:
i have the data row object & the data table object. I want to determine what the index position of this datarow object is in the containing table. How can i determine this??? i.e, using the...
3
by: Programatix | last post by:
Hi. I'm using a strongly typed dataset and I would like to get the index number of a column from a datarow. Is that possible? I would like to avoid using expression like this, ...
6
by: TB | last post by:
Hi All: Here´s a very simple question: I have created a datatable inside a dataset, and subsequently selected a particular row using certain criteria. How do get the index number of that...
9
by: JuHui | last post by:
>>> a='String' >>> for x in a: .... print x .... S t r i n g
3
by: Brian Piotrowski | last post by:
Hi All, I've probably done this before, but for the life of me I can't remember how I did it. I need to move values from a DB table into an array to be used for other queries. The number of...
1
by: Briansmi1116 | last post by:
I have a database, I have been working on, and I would like to be able to search for a record, by typing in the index number to display all of the information. The index number of the table, is the...
2
by: Ahmad Sheeraz Saeed | last post by:
i am new to C# 2005 i created the array of text boxes like this numerictextbox1.NumericTextBox PrReading = new numerictextbox1.NumericTextBox; what i want to know, is it possible for me to...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.