locked
ReDim Preserve, conversion from VB6 yo VB,NET? Count duplicate items in listbox RRS feed

  • Question

  • User-716811762 posted

    Hello guys i'm working on a webform and after doing some googling i found a post where it's explained what i was looking for --> Count duplicate items in listbox and display them on this way:

    value01, value02, value03, value02 to 

    value01 - 1 time.

    value02 - 2 times.

    value03 - 1 time.

    The issue is that the code was written for VB6 and I'm using VB.NET. I tried to manage a conversion but it's impossible for me to work the redim preserve out.

    The original VB6 code is:

    Private Sub Command1_Click()
        Dim MyArray() As String, CountOccurance As Long
        Dim i As Long, j As Long, k As Long
    
        k = 0
        Do While k < (List1.ListCount - 1)
            For i = 0 To List1.ListCount - 1
                If List1.List(k) = List1.List(i) Then _
                CountOccurance = CountOccurance + 1
            Next i
        
            ReDim Preserve MyArray(k + 1) As String
            MyArray(k) = List1.List(k) & "-" & CountOccurance
            CountOccurance = 0
            k = k + 1
        Loop
    
        For i = LBound(MyArray) To UBound(MyArray)
            List2.AddItem MyArray(i)
        Next i
    
        TotListItms = List2.ListCount - 1
        Do While TotListItms >= 0
            For i = TotListItms - 1 To 0 Step -1
                If List2.List(i) = List2.List(TotListItms) Then
                    List2.RemoveItem i
                    TotListItms = TotListItms - 1
                End If
            Next i
            TotListItms = TotListItms - 1
        Loop
    End Sub

    And the one I'm writting looks like: 

    Protected Sub Button2_Click(sender As Object, e As System.EventArgs)
            Dim MyArray() As String, CountOccurance As Long
            Dim i As Long, j As Long, k As Long
    
            k = 0
            Do While k < (ListBox3.Items.Count - 1)
                For i = 0 To ListBox3.Items.Count - 1
                    If "ListBox3.Items.Item(k)" = "ListBox3.Items.Item(i)" Then _
                    CountOccurance = CountOccurance + 1
                Next i
        
            ReDim Preserve MyArray(k + 1) As String
                MyArray(k) = "ListBox3.Items.Item(k)" & "-" & CountOccurance
                CountOccurance = 0
                k = k + 1
            Loop
    
            For i = LBound(MyArray) To UBound(MyArray)
                ListBox7.Items.Add(MyArray(i))
            Next i
    
            Dim TotListItms As String = ListBox7.Items.Count - 1
            Do While TotListItms >= 0
                For i = TotListItms - 1 To 0 Step -1
                    If "ListBox7.Items.Item(i)" = "ListBox7.Items.Item(TotListItms)" Then
                        ListBox7.Items.Remove(i)
                        TotListItms = TotListItms - 1
                    End If
                Next i
                TotListItms = TotListItms - 1
            Loop
        End Sub

    Thanks in advance

     

     

    Friday, August 5, 2011 2:41 PM

Answers

  • User896700311 posted

    Hi,

    replace:

    ReDim Preserve MyArray(k + 1) As String

    this way:

    ReDim Preserve MyArray(k + 1)

    Any doubt, post your comment.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 9, 2011 4:44 PM

All replies

  • User560403387 posted

    Rather than using a string array, use a generic list of type string: Dim MyArray  As New List(Of String). Then you can simply use the Add method to add the string to the list. These types of things have gotten a lot easier since vb6 times.

    Menno

    Friday, August 5, 2011 2:54 PM
  • User-716811762 posted

    Thanks Menno.

    It continues showing an error. When I type

    Dim MyArray  (k + 1) As String
    MyArray(k) = "ListBox3.Items.Item(k)" & "-" & CountOccurance
    CountOccurance = 0
    k = k + 1 

    It says that  MyArray hides a variable in a block inclusion. And I have no clue what's going on here! Thank you 

    Saturday, August 6, 2011 6:06 AM
  • User-716811762 posted

    I kindly appreciate an example because i feel like stuck with ths code. Thanks

    Sunday, August 7, 2011 8:05 AM
  • User896700311 posted

    Hi,

    replace:

    ReDim Preserve MyArray(k + 1) As String

    this way:

    ReDim Preserve MyArray(k + 1)

    Any doubt, post your comment.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 9, 2011 4:44 PM