abin kurian
abin kurian

Reputation: 41

How to fix error while converting list to array and then comparing the array?

I have created a 2 lists where one list is populated from the objects of the other list. As I transfer the details from the second list to an array and then analyze them I am getting an error. I can't seem to understand where the error is.

So I assume the issue lies with the array initialization and conversion of the array while comparison.

coa = ""

For i = 0 To partab.ListBox2.ListCount - 1
    If partab.ListBox2.Selected(i) Then
        If coa = "" Then
            coa = partab.ListBox2.List(i).tostring()
        Else
            coa = coa & "," & partab.ListBox2.List(i).tostring()
        End If
    End If
Next i

Dim arr() As String
Dim arrv() As Integer

arr = Split(coa, ",")

For i = 0 To partab.ListBox2.ListCount - 1
    If IsEmpty(arr) = True Then
        arrv(0) = 99
    Else
        If arr(i) = "Vehicle-Vehicle" Then
            arrv(i) = 1
        ElseIf arr(i) = "Vehicle-Pedestrian" Then
            arrv(i) = 2
        ElseIf arr(i) = "Vehicle-Bicycle/Others" Then
            arrv(i) = 3
        ElseIf arr(i) = "Vehicle-Animal" Then
            arrv(i) = 4
        Else
            arrv(i) = 99
        End If
    End If
Next i

Dim displayar As String
displayar = ""
If IsEmpty(arrv) Then
    displayar = 99
Else
    For i = 0 To partab.ListBox2.ListCount - 1
        displayar = displayar & "," & arrv(i)
    Next i
End If

So the code assumed the value of variable coa as empty and then accepts the value from the listbox2. The values are separated by a comma and stored in a string. The string is then converted to an array.

The array elements are compared to the string values as shown and another array with corresponding codes store the values. The second array is the one that is being for my analysis.

I am not sure where the code is wrong.

Upvotes: 0

Views: 42

Answers (3)

Siddharth Rout
Siddharth Rout

Reputation: 149305

I believe you are using VB.net. If yes, then is this what you are trying?

Public Class Form1
    '~~> Adding sample items for demonstration purpose
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With ListBox2.Items
            .Add ("Vehicle-Vehicle")
            .Add ("Sid")
            .Add ("Vehicle-Pedestrian")
            .Add ("Sid")
            .Add ("Vehicle-Bicycle/Others")
            .Add ("Sid")
            .Add ("Vehicle-Animal")
        End With
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim coa As String = ""
        Dim displayar As String = ""

        '~~> Loop though selected items of the listbox
        For i As Integer = 0 To ListBox2.SelectedItems.Count - 1
            coa = coa & "," & ListBox2.SelectedItems(i)
        Next i

        '~~> Check if there is something or not in coa
        If String.IsNullOrEmpty(coa) Then
            displayar = 99
        Else
            Dim arr = Split(coa, ",")
            Dim arrv() As String
            ReDim arrv(UBound(arr))
            Dim n As Integer = 0

            '~~> Loop though the array and check it's values
            For i As Integer = 0 To UBound(arr)
                Select Case arr(i)
                    Case "Vehicle-Vehicle": arrv(n) = 1
                    Case "Vehicle-Pedestrian": arrv(n) = 2
                    Case "Vehicle-Bicycle/Others": arrv(n) = 3
                    Case "Vehicle-Animal": arrv(n) = 4
                    Case Else: arrv(n) = 99
                End Select
                n += 1
            Next i

            '~~> Get the complete string without looping
            displayar = Join(arrv, ",")
        End If

        MessageBox.Show (displayar)
    End Sub
End Class

If I select all the items then the output will be 99,1,99,2,99,3,99,4 and if I do not select anything then the output will be 99

Upvotes: 0

Variatus
Variatus

Reputation: 14383

The variable coa isn't declared in your code. So, I presume that it is a variant. You assign the value "" to it at the start of your code, and that makes it a variant of string type. Next, your code checks whether it really has a null string value, which looks superfluous, since you just assigned that value to it, but if it does have that value your code then proceeds to assign an array to it. That turns it into a variant of array type. Note that it an array can't ever have the value "". I suggest you declare cao as whatever you want it to be and treat it as such.

The array assigned to cao is an array of all items selected in the ListBox. That could be none or several. There is no way of knowing by looking at the code.

However, For i = 0 To partab.ListBox2.ListCount - 1 attempts to read all the values in the ListBox from the array cao. That should be possible only if all items in the ListBox were selected because cao only contains the selected items. Therefore the line If arr(i) = "Vehicle-Vehicle" Then must throw a subscript error when i is larger than UBound(cao).

I suggest to format the loop as For i = LBound(cao) To UBound(cao). That would cure this particular error. There might be others.

Upvotes: 1

Sviat Lavrinchuk
Sviat Lavrinchuk

Reputation: 404

The "arrv" variable can't have an index as it is not an array.

Upvotes: 0

Related Questions