Results 1 to 2 of 2

Thread: [RESOLVED] Debugging HELP

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2010
    Posts
    34

    Resolved [RESOLVED] Debugging HELP

    Hey guys, I have an application that will display the last names, first names, and the first and last name in a listbox. Can anybody debug my code? It always crashes everytime I click the buttons.

    Code:
     Private names(4) As PersonName
    
        Private Sub MainForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            ' fills array with data
    
            names(0).first = "Mary"
            names(0).last = "Jones"
            names(1).first = "Susan"
            names(1).last = "Washington"
            names(2).first = "Carol"
            names(2).last = "O'Brien"
            names(3).first = "Jacob"
            names(3).last = "Harper"
            names(4).first = "Sue"
            names(4).last = "Chen"
        End Sub
    
        Private Sub exitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles exitButton.Click
            Me.Close()
        End Sub
    
        Private Sub lastButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lastButton.Click
            ' displays the last names in a list box
    
            ' clear then list box, then display the last names
            namesListBox.Items.Clear()
            For row As Integer = 1 To names.Length
                namesListBox.Items.Add(names(row).last)
            Next row
        End Sub
    
        Private Sub firstButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles firstButton.Click
            ' displays the first names in a list box
    
            ' clear the list box, then display the first names
            namesListBox.Items.Clear()
            For row As Integer = 1 To names.Length
                namesListBox.Items.Add(names(row).first)
            Next row
        End Sub
    
        Private Sub fullButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles fullButton.Click
            ' displays the first and last names in a list box
    
            ' clear the list box, then display the first and last names
            namesListBox.Items.Clear()
            For row As Integer = 1 To names.Length
                namesListBox.Items.Add(names(row).first _
                & " " & names(row).last)
            Next row
        End Sub

  2. #2
    Hyperactive Member
    Join Date
    Dec 2000
    Location
    Sydney
    Posts
    311

    Re: Debugging HELP

    Hi

    It might help if you gave the error information, rather than saying 'it crashes'.

    However...In your loops you have:

    Code:
            For row As Integer = 1 To names.Length
                namesListBox.Items.Add(names(row).last)
            Next row

    You're starting a 1, but the array is always zero based. And then you're counting to the length of the array, which is 5. But you don't have a 5th element. So that should read:

    Code:
    For row As Integer = 0 To names.GetUpperBound(0)
            namesListBox.Items.Add(names(row).last)
    Next row
    That might help you.

    Cheers
    Why do today what you can tomorrow...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width