What i am trying to achieve here is to sort the contents of any array
by making use of a collection.
The form makes use of an array called 'IntelliArray(X,Y)' which has two elements
1. Inventory Code
2. Inventory Description
The reasoning behind this is that the user will type part of the inventory code or description
into a text box, as each letter is typed into the text box a listview below will list all inventory codes and
descriptions that conform to the text that has been typed, as the length of text increases the number of items in the list
will decrease
Very similar to autocomplete on a text box.
I am struggling with the sorting of the array
Here is my code:
Public Class FrmIntelliText2
Dim SortedList As New List(Of InventoryCollection)
____________________________________________________________________________________________________ _________________
Public Structure InventoryCollection
Implements IComparable
Dim Field1 As String
Dim Field2 As String
'Use Field1 to sort this structure
Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
Return Me.Field1.CompareTo(CType(obj, InventoryCollection).Field1)
End Function
End Structure
____________________________________________________________________________________________________ _________________
Private Sub FrmIntelliText2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call CreateTheList()
End Sub
____________________________________________________________________________________________________ _________________
Private Sub CreateTheList()
Dim NewRow As InventoryCollection
For i As Integer = 0 To IntelliArray.GetUpperBound(0) - 1
NewRow.Field1 = IntelliArray(i, 1)
NewRow.Field2 = IntelliArray(i, 2)
SortedList.Add(NewRow) 'Add the row to the list
Next
'Sort the list
SortedList.Sort()
End Sub
____________________________________________________________________________________________________ __________________
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For i As Integer = 0 To SortedList.Count - 1
MsgBox(SortedList.Item(i).Field1)
MsgBox(SortedList.Item(i).Field2)
Next
End Sub
End Class
____________________________________________________________________________________________________ __________________
I am getting the error 'Object Reference not set to instance of an object' on this line >
'Return Me.Field1.CompareTo(CType(obj, InventoryCollection).Field1)'
Not sure what i am doing wrong here, i need to use the contents of sortedlist in other subs on the form
since the user will have the option, using radio buttons, to choose whether to sort by Inventory Code
or by inventory description.
Any help would be greatly appreciated or advice on a better way to do this if anyone has any ideas.
it would be great if it was possible to sort a multi dimensional array.
by making use of a collection.
The form makes use of an array called 'IntelliArray(X,Y)' which has two elements
1. Inventory Code
2. Inventory Description
The reasoning behind this is that the user will type part of the inventory code or description
into a text box, as each letter is typed into the text box a listview below will list all inventory codes and
descriptions that conform to the text that has been typed, as the length of text increases the number of items in the list
will decrease
Very similar to autocomplete on a text box.
I am struggling with the sorting of the array
Here is my code:
Public Class FrmIntelliText2
Dim SortedList As New List(Of InventoryCollection)
____________________________________________________________________________________________________ _________________
Public Structure InventoryCollection
Implements IComparable
Dim Field1 As String
Dim Field2 As String
'Use Field1 to sort this structure
Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
Return Me.Field1.CompareTo(CType(obj, InventoryCollection).Field1)
End Function
End Structure
____________________________________________________________________________________________________ _________________
Private Sub FrmIntelliText2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call CreateTheList()
End Sub
____________________________________________________________________________________________________ _________________
Private Sub CreateTheList()
Dim NewRow As InventoryCollection
For i As Integer = 0 To IntelliArray.GetUpperBound(0) - 1
NewRow.Field1 = IntelliArray(i, 1)
NewRow.Field2 = IntelliArray(i, 2)
SortedList.Add(NewRow) 'Add the row to the list
Next
'Sort the list
SortedList.Sort()
End Sub
____________________________________________________________________________________________________ __________________
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For i As Integer = 0 To SortedList.Count - 1
MsgBox(SortedList.Item(i).Field1)
MsgBox(SortedList.Item(i).Field2)
Next
End Sub
End Class
____________________________________________________________________________________________________ __________________
I am getting the error 'Object Reference not set to instance of an object' on this line >
'Return Me.Field1.CompareTo(CType(obj, InventoryCollection).Field1)'
Not sure what i am doing wrong here, i need to use the contents of sortedlist in other subs on the form
since the user will have the option, using radio buttons, to choose whether to sort by Inventory Code
or by inventory description.
Any help would be greatly appreciated or advice on a better way to do this if anyone has any ideas.
it would be great if it was possible to sort a multi dimensional array.