Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

I keep getting this weird error every so often

 

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

 

Additional information: Index was out of range. Must be non-negative and less than the size of the collection.

 

What I am doing is loading some objects into an a ListBox and then I click on a item and it fills two textboxes with data and adds the selected object to an ArrayList. When I delete data from one of the textboxes I want to remove it from the ArrayList. If someone can just look at the attach file and see if you can make heads or tails of it.

 

Thanks,

Dave Pifer

 

[edit] Don't post binaries [/edit]

testform.zip

Edited by divil
  • Administrators
Posted

The problem is you are using absoulte numbers for the textboxes but index offsets for the remove at.

 

i.e

After adding 3 items the array contains 3 items (0,1,2)

If you remove the item from ICD9Code1 then the ArrayList contains 2 items (0,1).

If you try to delete ICD9Code2 the aryDiagnosis.RemoveAt(i) is trying to remove item 2 but only 0 & 1 are present - hence the error.

 

You may be better trying to remove them by name rather than the number i.

 

If I get chance I'll have a look at the code later today and see what I can do...

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

  • Administrators
Posted (edited)
Try this, my code is a bit scrappy admitedly but in a very brief test it seemed to work. I got round the problem by rather than removing from the array I just set the relevant entry to Nothing. Not the best solution but it works for now

testform.zip

Edited by PlausiblyDamp

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted (edited)

Thanks...I have not had time to get back with you. I think I figured it out so far no errors at all. Thanks for looking into it.

 

Here is the solution I came up with.

 

Thanks again.

Dave

testform.zip

Edited by PlausiblyDamp
Posted

       Dim i As Integer
       For i = 2 To 10
           Create_PtDiagnosis_TxtControls(i)
           Create_ICD9Code_TxtControls(i)
       Next

 

You can change that to

For i = 0 to 8

 

Then you won't have to do

 

       tb.Name = "ptDiag" & CStr(txtNum - 2)
       tb.Name = "ICD9Code" & CStr(txtNum - 2)

 

Although I'm curious, you have 10 tests listed in the ListBox but only 9 rows of TextBoxes? Is that what you wanted? You could of done;

 

For i = 0 to ListBox1.Items.Count - 1

 

Hard coding numbers isn't good. At the very least use constants at the top of your page for such.

 

As for your ArrayList problem, why not use the Tag to store the Diagnosis object? That way you can just ArrayList.Remove(object) rather then having to search for the exact index. It'll also disolve all those IndexOutOfBounds errors.

 

So in your PopulatePtDiag() method, maybe something like this;

 

       For Each ctrl In Me.Controls
           If TypeOf ctrl Is TextBox Then
               ' Why the two loops? You can contain it in one.
               For i = 0 To aryDiagnosis.Count - 1
                   ' All controls have a Name property if I'm 
                   'not mistaken. No need to cast.
                   If ctrl.Name = "ptDiag" & i Then
                       ' BTW, Control has a Text property, so 
                       'you shouldn't need to cast it to a TextBox.
                       ctl.Text = aryDiagnosis.Item(i).DiagDesc.ToString
                       ' Added tag code here
                       ctl.Tag = aryDiagnosis.Item(i)

                   ' Again, shouldn't be a need to cast.
                   Else If ctl.Name = "ICD9Code" & i Then
                       ctrl.Text = aryDiagnosis.Item(i).ICD9Code.ToString
                       ' Added tag code here
                       ctl.Tag = aryDiagnosis.Item(i)
                   End If
               Next
           End If
       Next

 

Then removing it is as easy as removing the object in the tag;

 

   Private Sub ptDiag_KeyUp(ByVal sender As System.Object, ByVal e As KeyEventArgs)
       'Not quite sure if this works in VB.NET, but it does in C#. Basically it 
       'does both casting and checking at the same time.
       Dim txtbox As TextBox = sender As TextBox
       If (txtbox = Nothing) Then
           Return
       End If

       If (txtbox.Name = "ptDiag" & i AND txtBox.TextLength = 0) Then
                   ' This loop shouldn't be needed, there was another post on how 
                   'to get a control based off a string name without looping.
                   For Each ctrl In Me.Controls
                       If TypeOf ctrl Is TextBox Then
                           ctl.Name = "ICD9Code" & i Then
                               'Here's where we remove the Diagnosis object.
                               aryDiagnosis.Remove(ctl.Tag)

                               ' What is this for exactly? You can just do 
                               'aryDiagnosis.Count
                               idiagCount -= 1
                               Clear_TxtControls()
                               PopulatePtDiag()
                               Exit Sub
                           End If
                       End If
                   Next           
       End If
   End Sub

 

I don't have Visual Studio installed on this machine so I'm just looking at your code.. I apologize if this doesn't work exactly how you want, but the philosophy is the same.

 

I also added some comments here and there.

Gamer extraordinaire. Programmer wannabe.
Posted

Wyrd,

 

Thanks for your help...adding the object to the tag property was a great idea and so was the casting solution. The reason I used the 2 to 10 in the for loop is that I wanted the first textbox control y axis to start at 48 and have them 24 pixels apart since they are only going to be nine instances that seem to be the best fix. The reason I used in my test form 10 items in the listbox and nine textboxes because in the listbox there are going to be an unknown number of list items and always nine instances of the textbox I just added ten items just for test purposes only. But again...thanks alot for even looking at this as closely as you did. I've been only involved with VB.NET for about two months or so....and still learning all that I can.

 

Thanks,

Dave

Posted
Sure np. I hope you were able to understand the code. ;) Taking a quick glance at it I noticed a few errors that I made while altering your code (forgetting an If and using ctrl and ctl interchangably, and probably numerous other typos)
Gamer extraordinaire. Programmer wannabe.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...