|
-
Apr 18th, 2009, 07:54 PM
#1
Thread Starter
Fanatic Member
Index Outside of Array Runtime error....
I just recently started getting an index out of bounds error on one of my arrays at runtime. I was not having this issue previously. It only seems to happen when several items are added to a listbox. Here is my declaration and code. Thanks for any help resolving this issue.
Code:
Dim ClientActiveMedProbs(frmmain.lstActiveMedProbs.Items.Count) As String
'store all active medical problem information in an array & build a string
For i As Integer = 0 To frmmain.lstActiveMedProbs.Items.Count - 1
ClientActiveMedProbs(i) = frmmain.lstActiveMedProbs.Items.Item(i).ToString
Next
If frmmain.lstActiveMedProbs.Items.Count = 0 Then
ActiveMedProbsResult = ""
Else
ActiveMedProbsResult = String.Join(", ", ClientActiveMedProbs)
ActiveMedProbsResult = Left(ActiveMedProbsResult, Len(ActiveMedProbsResult) - 2)
End If
The actual error message is:
Index was outside the bounds of the array.
-
Apr 18th, 2009, 08:35 PM
#2
Re: Index Outside of Array Runtime error....
First up, this:
Code:
Dim ClientActiveMedProbs(frmmain.lstActiveMedProbs.Items.Count) As String
should be this:
Code:
Dim ClientActiveMedProbs(frmmain.lstActiveMedProbs.Items.Count - 1) As String
As for the problem, it looks as though it should be OK so, as always, the thing to do is to debug your code. Run the project in the debugger and step through that section. What is the Count of the ListBox Items? What is the Length of the array? What is 'i'? You don't solve run time errors just by looking at code. You watch it as it's running and take into account the data it's actually using at the time.
-
Apr 30th, 2009, 11:30 AM
#3
Thread Starter
Fanatic Member
Re: Index Outside of Array Runtime error....
Ok, I have continued to debug this and am still getting the error at runtime. I even tested in debug mode in VB and added 20 items to the listbox. No error in VB. Then I compiled it and it immediately errored with the same error message at runtime. I would like to emphasize that I am working with a listbox control here, not a listview.
Code:
Dim ClientActiveMedProbs(frmmain.lstActiveMedProbs.Items.Count -1) As String
Dim i as Integer
'store all active medical problem information in an array
For i = 0 To frmmain.lstActiveMedProbs.Items.Count - 1
ClientActiveMedProbs(i) = frmmain.lstActiveMedProbs.Items.Item(i).ToString
Next
The issue is definitely a mathematical problem. The code in the loop is basically saying: From the index of 0 to the total count of items in the listbox -1, correct. This means that the listbox index will start at 0 and continue to loop through until it reaches the total count of items in the listbox - 1.
I am missing something very basic here. Please help! Thanks.
-Jeremy
-
Apr 30th, 2009, 11:37 AM
#4
Re: Index Outside of Array Runtime error....
What is the actual value of i at the point at which it errors?
-
Apr 30th, 2009, 01:13 PM
#5
Re: Index Outside of Array Runtime error....
if the error is happening on this line:
ClientActiveMedProbs(i) = frmmain.lstActiveMedProbs.Items.Item(i).ToString
Then what you need to do is determine WHICH side of the = the error is on....
-tg
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
|