Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

My code should explain whats going on so I will be breif in my explanation

 

I have an ArrayList I have added to the arrayList from an another array

 

I remove the list, it removes the name as I can see its results but the arrList count remains the same as though it hasnt been removed

I assume that what it has done is simply remove the data from a an element and the element has a value now of zero but still will be counted

So I used trim to Size of which I understood would then trim the ArrList down to elements only containing data Right?

 

Anyway.... I am trying to add and remove and the counts reflect the change as well

 

Help

 

vbMarKO

Visual Basic 2008 Express Edition!
Posted
How about a code snippet of what you are doing? My guess is that you are using the AddRange method to add the Array to the ArrayList, but my question is how are you removing the Array from the ArrayList?
"Never ascribe to malice that which can adequately be explained by incompetence." -- Napolean Bonaparte
Posted

LOL

 

 

I forgot to add the code

 

Ok here its

 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim myArr() As String
       Dim strText As String
       strText = "Bob" & vbCrLf & "Tom" & vbCrLf & "Sally" & vbCrLf & "Sue"
       myArr = Split(strText, vbCrLf)

       Dim arrList As New ArrayList(myArr)
       Dim name As String
       For Each name In arrList
           TextBox1.Text = TextBox1.Text & name & vbCrLf
       Next

       Label1.Text = arrList.Count
   End Sub

   Private Sub removeBTN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles removeBTN.Click
       Dim myArr() As String
       Dim strText As String
       strText = TextBox1.Text
       myArr = Split(strText, vbCrLf)
       Dim arrList As New ArrayList(myArr)
       If removeText.Text <> "" Then
           arrList.Remove(removeText.Text)
           arrList.TrimToSize()
           Label1.Text = arrList.Count ' I used the TrimToSize hoping that would fix it
           ' Also the arrList.Count should be one less than it was but it doesnt change I mean the count doesnt
           ' but it does remove the Name writen in the removeText TEXTBOX so an Empty Element I think still exists
       Else
           Return
       End If

       TextBox1.Text = ""
       Dim name As String
       For Each name In arrList
           TextBox1.Text = TextBox1.Text & name & vbCrLf
       Next

   End Sub

   Private Sub addBTN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addBTN.Click
       Dim myArr() As String
       Dim strText As String
       strText = TextBox1.Text  ' Collects Data from TextBox assigns it to String Variable
       myArr = Split(strText, vbCrLf)  ' I then assing contents of String Variable to an Array

       Dim arrList As New ArrayList(myArr)  ' I then add the Array to an ArrayList
       If addText.Text <> "" Then
           arrList.Add(addText.Text)  ' Here I add a name writen by user in addText TextBox to the ArrayList
           ' I am assuming at this point it should just add it to the end of the list
       Else
           Return
       End If
       TextBox1.Clear()  ' Clear TextBox not really needed I guess but its a habit I have so its here
       Dim name As String
       Dim strNewText As String
       For Each name In arrList
           strNewText = strNewText & name & vbCrLf ' Assigning Elements including new elementof the ArrayList to String Variable
       Next
       TextBox1.Text = strNewText ' Displaying it in the TextBox
       Label1.Text = arrList.Count  ' Doing account to see if the amount of Elements match that which is in the 
       ' TextBox   and each TIme it is NOT its 2 more elements than it should be
       ' so if you had 4 Names in the TextBox it should add 1 more element to the list the count should be 5 instead
       ' its 1 more unless I have used the remove first then added then its 2 more   
       ' I am assuming empty elements but how do I rid mysolf of this problem

   End Sub

 

vbMarkO

:) there it is :)

Visual Basic 2008 Express Edition!
Posted

I can't reproduce your problem. I added the string "ABC" three times to an Array, then added the Array to an ArrayList using the AddRange method. I then used the Remove method to remove the first occurrance of "ABC" from the ArrayList. The Count property of the ArrayList is decremented appropriately.

 

My first guess is that your TextBox.Text values do not match anything in the ArrayList and therefore nothing is being removed from the ArrayList.

 

Here is a code snippet I ran in a Console Application:

 

Private Sub TestArrayList()
  Dim arrList As New ArrayList
  Dim arry As Array

  arry = Array.CreateInstance(GetType(System.String), 3)
  For i As Integer = 0 To arry.GetUpperBound(0)
     arry.SetValue("ABC", i)
  Next

  arrList.AddRange(arry)
  Console.WriteLine(arrList.Count.ToString)

  arrList.Remove("ABC")
  Console.WriteLine(arrList.Count.ToString)
End Sub

 

And it produces the following output:

 

C:\dev\tmp\Test\bin>test
3
2

"Never ascribe to malice that which can adequately be explained by incompetence." -- Napolean Bonaparte
Posted
The loop you are using to build the textbox contents in all of your button click handlers is adding an extra vbCrLF' date=' this is causing the extra array element when you are doing the split.[/quote']

 

 

Bingo That is it, I found it when I was debugging it, took me a while to figure out how to correct it but I did.

 

I did it using this

 

       Dim strText As String
       strText = TextBox1.Text  ' Collects Data from TextBox assigns it to String Variable
       myArr = Split(strText, vbCrLf) ' I then assign contents of String Variable to an Array
       Dim arrList As New ArrayList 'Dim arrList As New ArrayList(myArr)  
       For i As Integer = 0 To UBound(myArr) - 1
           arrList.Add(myArr(i)) ' I then add the Array to an ArrayList
       Next

That fixed it! It no longer adds the vbCrlF at the end :)

 

vbMarkO

Visual Basic 2008 Express Edition!

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...