Results 1 to 4 of 4

Thread: Array and ListBox

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    2

    Array and ListBox

    Hello,

    I am working on an assignment. It involves entering postal codes (like 1111 AA), sorting them, and showing them. We have to use arrays and listboxes.

    So far, I have managed to put the postal codes in one listbox. I put the data in an array by using this:

    boxList.Items.CopyTo(Array, 0)

    Then I sort them based on the numbers using this:

    Dim Low As Integer
    Dim High As Integer
    Dim Temp As Integer

    For Low = LBound(Lijst) To Array.Length - 2
    For High = Low + 1 To Array.Length - 1
    If Low > High Then
    Temp = High
    High = Low
    Low = Temp
    End If
    Next High
    Next Low

    Then, I want to put the sorted array into a second listbox:

    For p = 0 To List.Length
    boxSorted.Items.Add(List(p))
    Next p

    I get the following error, and the italic part is marked yellow:
    Value can't be null. Parameter name: item

    I don't know what it means and how I can solve it. Could anyone help me?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Array and ListBox

    The immediate cause of that exception is that 'List(p)' is Nothing. Perhaps you haven't put any values into List or maybe you just messed up one or two but at least one element is Nothing. You need to go back to where you created List and then step through the code and make sure you do assign a value to each element and that none of them are ever set back to Nothing.

    You have another issue too though. Let's say that List has 10 elements. What will List.Length be? If your loop goes from zero to List.Length, how many iterations is that?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    2

    Re: Array and ListBox

    Hello,

    Thank you for your reply. You actually pointed at the same mistake twice in your reply: I did not think of the fact that arrays start at 0.
    I made this mistake while filling the array, which caused the last place to be empty.

    After putting ' - 1' behind List.Length and behind Items.Count, everything seems to work. Thanks again

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Array and ListBox

    Hmmm... that seems strange. If the issue was the invalid index then I would have expected an IndexOutOfRangeException, while the error message you quoted seems to suggest an InvalidOperationException. Anyway, all's well that ends well.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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