Search code examples
vb.netfor-loopexecution

Code following a For loop won't execute - VB.NET


I'm having a strange issue here. I have a For loop inside a sub, and right below it I have a MessageBox function. Everything compiles correctly, however for some reason, if the loop executes and exits successfully, anything outside and below the For doesn't get executed.

Public Sub Example()
    For i = 0 To 9
        ListBox.Items.Add(i.ToString)
    Next

    MessageBox.Show("Done")    'This doesn't execute
    Beep()                     'Doesn't either
    Label.Text = "Done"        'etc.
End Sub

Yes, it displays only 10 items in the ListBox afterwards.

I've searched everywhere, but I did not find anything related to such an issue. I'm kinda puzzled, anyone got a clue of what's going on?

EDIT: Forgot to specify, the sub is actually a TextBox.TextChanged event


Solution

  • It seems the ListBox.Items.Add contained a null value on the last iteration. Fixing that seemingly solved it. I'm not sure why it wouldn't stop the execution and return an error (newbie here).