|
-
May 8th, 2013, 03:55 AM
#1
Thread Starter
New Member
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?
-
May 8th, 2013, 04:46 AM
#2
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?
-
May 8th, 2013, 04:55 AM
#3
Thread Starter
New Member
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
-
May 8th, 2013, 04:59 AM
#4
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|