R
Radu
Hi. I have the following:
___________________________________________________________________________________
Public Class SquareDictionary
Inherits System.Collections.DictionaryBase
Default Property Item(ByVal key As String) As Square
Get
Return CType(Dictionary.Item(key), Square)
End Get
Set(ByVal Value As Square)
Dictionary.Item(key) = Value
End Set
End Property
Sub Add(ByVal key As String, ByVal value As Square)
Dictionary.Add(key, value)
End Sub
'Sa ample "Factory function"
Function Create(ByVal key As String, ByVal side As Single) As Square
Create = New Square(side)
dictionary.Add(key, Create)
End Function
Class Square
Implements IComparable
Public Side As Integer
Sub New(ByVal side As Integer)
Me.Side = side
End Sub
Public Function CompareTo(ByVal obj As Object) As Integer Implements
System.IComparable.CompareTo
If obj Is Nothing Then Return 1
Dim other As Square = CType(obj, Square)
If Side > other.Side then return 1
If Side = other.Side then return 0
If Side < other.Side then return -1
End Function
End Class
End Class
___________________________________________________________________________________
Module Module1
Sub Main()
Dim sq As New SquareDictionary()
'Add normally
sq.Add("First", New SquareDictionary.Square(10))
sq.Add("Second", New SquareDictionary.Square(20))
sq.Add("Third", New SquareDictionary.Square(30))
'Add using the "factory function"
sq.Create("Fourth", 40)
sq.Create("Fifth", 50)
sq.Create("Sixth", 60)
Dim s As DictionaryEntry
For Each s In sq
Console.Write(s.Key + ", ")
Dim ss As SquareDictionary.Square = CType(s.Value,
SquareDictionary.Square)
Console.WriteLine(ss.Side)
Next
Dim a(sq.Count) As DictionaryEntry
sq.CopyTo(a, 1)
'Array.Sort(a)
Dim i As Integer
For i = 1 To a.GetUpperBound(0)
Console.WriteLine(CType(a(i).Value, SquareDictionary.Square).Side)
Next
Console.Read()
End Sub
End Module
___________________________________________________________________________________
The code works just fine, producing:
Sixth, 60
Fifth, 50
Third, 30
Second, 20
First, 10
Fourth, 40
60
50
30
20
10
40
But now I want to add the sort capability, so I added the Function CompareTo
in SquareDictionary.Square.
I get the error "Additional information: Specified IComparer threw an
exception."
Where is my error ? In my mind, it should work.... what am I missing here ?
Thanks a lot.
Alex
___________________________________________________________________________________
Public Class SquareDictionary
Inherits System.Collections.DictionaryBase
Default Property Item(ByVal key As String) As Square
Get
Return CType(Dictionary.Item(key), Square)
End Get
Set(ByVal Value As Square)
Dictionary.Item(key) = Value
End Set
End Property
Sub Add(ByVal key As String, ByVal value As Square)
Dictionary.Add(key, value)
End Sub
'Sa ample "Factory function"
Function Create(ByVal key As String, ByVal side As Single) As Square
Create = New Square(side)
dictionary.Add(key, Create)
End Function
Class Square
Implements IComparable
Public Side As Integer
Sub New(ByVal side As Integer)
Me.Side = side
End Sub
Public Function CompareTo(ByVal obj As Object) As Integer Implements
System.IComparable.CompareTo
If obj Is Nothing Then Return 1
Dim other As Square = CType(obj, Square)
If Side > other.Side then return 1
If Side = other.Side then return 0
If Side < other.Side then return -1
End Function
End Class
End Class
___________________________________________________________________________________
Module Module1
Sub Main()
Dim sq As New SquareDictionary()
'Add normally
sq.Add("First", New SquareDictionary.Square(10))
sq.Add("Second", New SquareDictionary.Square(20))
sq.Add("Third", New SquareDictionary.Square(30))
'Add using the "factory function"
sq.Create("Fourth", 40)
sq.Create("Fifth", 50)
sq.Create("Sixth", 60)
Dim s As DictionaryEntry
For Each s In sq
Console.Write(s.Key + ", ")
Dim ss As SquareDictionary.Square = CType(s.Value,
SquareDictionary.Square)
Console.WriteLine(ss.Side)
Next
Dim a(sq.Count) As DictionaryEntry
sq.CopyTo(a, 1)
'Array.Sort(a)
Dim i As Integer
For i = 1 To a.GetUpperBound(0)
Console.WriteLine(CType(a(i).Value, SquareDictionary.Square).Side)
Next
Console.Read()
End Sub
End Module
___________________________________________________________________________________
The code works just fine, producing:
Sixth, 60
Fifth, 50
Third, 30
Second, 20
First, 10
Fourth, 40
60
50
30
20
10
40
But now I want to add the sort capability, so I added the Function CompareTo
in SquareDictionary.Square.
I get the error "Additional information: Specified IComparer threw an
exception."
Where is my error ? In my mind, it should work.... what am I missing here ?
Thanks a lot.
Alex