4 Replies - 3368 Views - Last Post: 03 June 2011 - 01:07 PM Rate Topic: -----

#1 Splotch  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 15
  • Joined: 25-May 11

Load Event Skipping Lines

Posted 02 June 2011 - 12:40 PM

Ok. Here's what its supposed to do. The load event goes through two different List(of T) in its own For Each loop. When the form is run for the first time, the first list is populated, the second one is not so the first loop runs but the second one does not. Then an integer that would normally be modified by the second loop remains at 2 and is assigned to a private variable integer, _handsRemaining.

Here is what it DOES. It runs through the first For Each loop and populates the appropriate field. It ignores the second loop because there are no weapon Class instances in that list. Then it goes wrong. It IGNORES the remaining lines (including the third MessageBox.Show) and somehow assigns a value of 0 to the private variable, _handsRemaining.

Why is this happening? What can be done to fix it?

    Private _handsRemaining As Integer

    Private Sub Inventory_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim hand As Integer = 2

        MessageBox.Show(hand & " 0")

        For Each thing As Object In _invList
            lboInventory.Items.Add(thing.name)
        Next

        MessageBox.Show(hand & " 1")

        For Each swish As Weapon In _wieldList
            hand = hand - swish.hands
            lboWield.Items.Add(swish.name)
        Next

        handsRemaining = hand

        MessageBox.Show(hand & " 2")
    End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Load Event Skipping Lines

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,054
  • Joined: 02-July 08

Re: Load Event Skipping Lines

Posted 02 June 2011 - 12:51 PM

The default value for your variable is 0 since you did not set it. Have you debugging it line by line to see what it's doing?
Was This Post Helpful? -1
  • +
  • -

#3 Splotch  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 15
  • Joined: 25-May 11

Re: Load Event Skipping Lines

Posted 02 June 2011 - 02:23 PM

Well it should default to 0 if line 19 weren't there at all. Line 4 assigns variable hand to a value of 2. If lines 14 through 17 are skipped, then line 19 should assign a value of 2 to _handsremaining.

A line-by-line debugging shows exactly what I described. It hits line 14 and skips to the end of the Load sub.
Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,054
  • Joined: 02-July 08

Re: Load Event Skipping Lines

Posted 02 June 2011 - 02:55 PM

There was no reason to down rep here. I went thru your code, read the comments in your code.
Private _handsRemaining As Integer ' = 0 

Private Sub Inventory_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim hand As Integer = 2

    MessageBox.Show(hand & " 0")

    For Each thing As Object In _invList
        lboInventory.Items.Add(thing.name)
    Next

    MessageBox.Show(hand & " 1")
'as you say it skips this loop since no weapons are loaded
    For Each swish As Weapon In _wieldList
        hand = hand - swish.hands
        lboWield.Items.Add(swish.name)
    Next
'you also said it does not get to the third messagebox
'at this point you have not assigned handsRemaining anything, right?
'so if it was skipping the third messagebox it might be skipping this too
'thats why I asked you to debug it
    handsRemaining = hand 'first place where this variable is assigned
                          'as far as I can see.
    MessageBox.Show(hand & " 2")
End Sub


Was This Post Helpful? 0
  • +
  • -

#5 Btu  Icon User is offline

  • D.I.C Regular

Reputation: 36
  • View blog
  • Posts: 250
  • Joined: 16-May 11

Re: Load Event Skipping Lines

Posted 03 June 2011 - 01:07 PM

View PostSplotch, on 02 June 2011 - 02:23 PM, said:

Well it should default to 0 if line 19 weren't there at all. Line 4 assigns variable hand to a value of 2. If lines 14 through 17 are skipped, then line 19 should assign a value of 2 to _handsremaining.

A line-by-line debugging shows exactly what I described. It hits line 14 and skips to the end of the Load sub.

I've had one scenario where VS appeared to be jumping out of code. What was happening was my class was throwing an inner exception, which wasn't blowing up for some reason.
Try putting a Try...Catch around your code and use a messagebox to show the exception message.
Try
'code
Catch Ex as Exception
messagebox.show(ex.message)
Finally
'Other code
End Try



EDIT: I just read HawkValleys post, I have to agree with him. I think I misunderstood.

This post has been edited by Btu: 03 June 2011 - 01:16 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1