ArrayList Sorting in VB.NET - How to use ICompare

R

Rob G

Hello,

I have a basic understanding of Objects, inheritance, polymorhpism, etc. I
am new to VB.NET and I am having a problem figuring this out.

I want to sort a CheckBoxList. Using the ArrayList seemed to make the most
sense. When I use it I get the error: At least one object must implement
IComparable. Here's the code that produces that error:

Public Class Sorting
Implements IComparable, IComparer

Private Shared aryOrigData As New ArrayList
Private Shared aryChangedData As New ArrayList
Public Shared Function GatherChangedCheckBoxData(ByVal chkboxlist As
CheckBoxList)
Dim li As ListItem

For Each li In chkboxlist.Items
aryChangedData.Add(li)
Next


aryChangedData.Sort()

End Function


The above code is for a web form. As I am learning to do this, I made
borrowed the code from the Help files and placed it into a new project, I
didn't get any error. Here's that code:

Imports System
Imports System.Collections
Imports Microsoft.VisualBasic


Public Class SamplesArrayList

Public Shared Sub Main()


' Creates and initializes a new ArrayList.
Dim myAL As New ArrayList
myAL.Add("The")
myAL.Add("quick")
myAL.Add("brown")
myAL.Add("fox")
myAL.Add("jumps")
myAL.Add("over")
myAL.Add("the")
myAL.Add("lazy")
myAL.Add("dog")

' Displays the values of the ArrayList.
Console.WriteLine("The ArrayList initially contains the " _
+ "following values:")
PrintIndexAndValues(myAL)

' Sorts the values of the ArrayList.
myAL.Sort()

' Displays the values of the ArrayList.
Console.WriteLine("After sorting:")
PrintIndexAndValues(myAL)
End Sub 'Main


Public Shared Sub PrintIndexAndValues(ByVal myList As IEnumerable)
Dim i As Integer = 0
Dim myEnumerator As System.Collections.IEnumerator = _
myList.GetEnumerator()
While myEnumerator.MoveNext()
Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab
_
+ "{1}", i, myEnumerator.Current)
i = i + 1
End While
Console.WriteLine()
End Sub
End Class

=============================
My question is then, what is the difference between the two? Is it becuase
one is using ListItems? If that is the case how do I solve it? If it is
implementing ICompare, could someone direct me to a good source that
explains it well.

Thanks so much for your help.
 
K

Kevin Spencer

Try using a System.Collections.SortedList instead.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
R

Rob G

Thanks for the tip. I'll look into it.

Kevin Spencer said:
Try using a System.Collections.SortedList instead.

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

Rob G said:
Hello,

I have a basic understanding of Objects, inheritance, polymorhpism, etc. I
am new to VB.NET and I am having a problem figuring this out.

I want to sort a CheckBoxList. Using the ArrayList seemed to make the most
sense. When I use it I get the error: At least one object must implement
IComparable. Here's the code that produces that error:

Public Class Sorting
Implements IComparable, IComparer

Private Shared aryOrigData As New ArrayList
Private Shared aryChangedData As New ArrayList
Public Shared Function GatherChangedCheckBoxData(ByVal chkboxlist As
CheckBoxList)
Dim li As ListItem

For Each li In chkboxlist.Items
aryChangedData.Add(li)
Next


aryChangedData.Sort()

End Function


The above code is for a web form. As I am learning to do this, I made
borrowed the code from the Help files and placed it into a new project, I
didn't get any error. Here's that code:

Imports System
Imports System.Collections
Imports Microsoft.VisualBasic


Public Class SamplesArrayList

Public Shared Sub Main()


' Creates and initializes a new ArrayList.
Dim myAL As New ArrayList
myAL.Add("The")
myAL.Add("quick")
myAL.Add("brown")
myAL.Add("fox")
myAL.Add("jumps")
myAL.Add("over")
myAL.Add("the")
myAL.Add("lazy")
myAL.Add("dog")

' Displays the values of the ArrayList.
Console.WriteLine("The ArrayList initially contains the " _
+ "following values:")
PrintIndexAndValues(myAL)

' Sorts the values of the ArrayList.
myAL.Sort()

' Displays the values of the ArrayList.
Console.WriteLine("After sorting:")
PrintIndexAndValues(myAL)
End Sub 'Main


Public Shared Sub PrintIndexAndValues(ByVal myList As IEnumerable)
Dim i As Integer = 0
Dim myEnumerator As System.Collections.IEnumerator = _
myList.GetEnumerator()
While myEnumerator.MoveNext()
Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab
_
+ "{1}", i, myEnumerator.Current)
i = i + 1
End While
Console.WriteLine()
End Sub
End Class

=============================
My question is then, what is the difference between the two? Is it becuase
one is using ListItems? If that is the case how do I solve it? If it is
implementing ICompare, could someone direct me to a good source that
explains it well.

Thanks so much for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top