At least one object must implement IComparable

F

fniles

I have a collection inside a class, sometimes when I add to the collection,
I get the error "At least one object must implement IComparable".
What does the error mean ?
Thanks.

Public Class SessionClass
Private Quotes As Collection = New Collection
Sub NewQuote(ByVal Message As String)
Dim swError As StreamWriter

Try
Quotes.Add(Message) '--> error here
Catch ex As Exception
swError = New StreamWriter(Application.StartupPath &
"\AQErrorLog-" & lIndex & "-" & Date.Now.ToString("MMddyy") & ".txt" &
vbCrLf, True)
swError.Write(Now & " Session - NewQuote - error = " &
ex.Message)
swError.Close()
End Try
End Sub
 
T

tommaso.gastaldi

hi
Can you report what "Message" contains when you get the exception?

Also can you report what the 2-nd element of the collection contains
at the time of exception?

-tom

PS
I do not see why not to use a simple arraylist here. The collection
uses a keyvalue entry.

fniles ha scritto:
 
T

tommaso.gastaldi

hi
Can you report what "Message" contains when you get the exception?

Also can you report what the 2-nd element of the collection contains
at the time of exception?

-tom

PS
I do not see why not to use a simple arraylist here. The collection
uses a keyvalue entry.

fniles ha scritto:
 
F

fniles

Thank you for your reply.
Can you report what "Message" contains when you get the exception?
ex.Message = At least one object must implement IComparable
Also can you report what the 2-nd element of the collection contains
at the time of exception?
I did not catch the value of the 2nd element of the collection when the
program broke, and this error does not happen all the time.
May I know why do you need to know the value of the 2nd element of the
collection ?
I do not see why not to use a simple arraylist here. The collection
uses a keyvalue entry.
Can you please tell me why simple arraylist will be better than collection ?
What did you mean by "collection uses a keyvalue entry" ?

Thanks.
 
F

fniles

Thanks.
They "key" in the "add" method is an optional parameter. I do not use the
key when adding a member to the collection.

Can you please tell me why simple arraylist will be better than collection ?
May I know why do you need to know the value of the 2nd element of the
collection ?
 
T

tommaso.gastaldi

I am especially fond of 3 objects which I use often:

arraylist
hashtable
sortedlist

so even if technically the arraylist is a collection, conceptually,
when storing single values,
I prefer the arraylist, also because it is often the case that the
insertion order is meaningful.

In case of exception, if you do not look at what object you actually
stored, it
would be hard to spot the cause of error.
From the error message it's like you are storing objects different from
strings, which
the compiler does not know how to compare because an IComparable
implemenmtation is missings althought that would not seem possible with
the signature "NewQuote(ByVal Message As String)" but perhaps you
changed something for posting purposes...

-t

fniles ha scritto:
 
F

fniles

If I need to add and remove items from the arraylist or collection, which
one is faster: arraylist or collection ?

Thank you.
 
T

tommaso.gastaldi

Arraylists are very fast. I have never compared with collection but you
can make some experiment and measure yourself how long they take. Also
for a log, I guess you may need ordered events (Ilist interface).

If you need to make extensive removal take a look at hashtables also or
better a typed collection Dictionary(Of T1, T2)
[System.Collections.Generic.Dictionary]
because removal (not removeat()) isn't O(1) in arraylist.

If you do some performance experiment please let us know the results.

-t
 

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

Top