|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
I can't find the error here....___________________________________________________________________________________ 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 Hello, Alex,
I suspect the problem might be with: > Dim other As Square = CType(obj, Square) Perhaps obj is not the Type that you think. For example, I see in your output line: > Console.WriteLine(CType(a(i).Value, SquareDictionary.Square).Side) that you are casting a(i).Value to Square. Maybe you also need to cast obj.Value to Square in the CompareTo function. You should be able to test this by setting a breakpoint on the line doing the casting in CompareTo and test the type that is being cast. Cheers, Randy Radu wrote: Show quoteHide quote > 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 > > Hello Radu,
Change your line to: Array.Sort(a, New SquareDictionaryComparer) And add the following class: Class SquareDictionaryComparer Implements IComparer Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare Dim one As SquareDictionary.Square = CType(CType(x, DictionaryEntry).Value, SquareDictionary.Square) Dim other As SquareDictionary.Square = CType(CType(y, DictionaryEntry).Value, SquareDictionary.Square) If one Is Nothing Then If other Is Nothing Then Return 0 Else Return -1 End If Else Return one.CompareTo(other) End If End Function End Class Regards. Show quoteHide quote "Radu" <REMOVETHISPARTcuca_macaii2***@yahoo.com> escribió en el mensaje news:LMudnUkjvMLbkoTZnZ2dnUVZ_sCdnZ2d@b2b2c.ca... | 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 | |
exe/dll as scheduled task
Access, OLE & VB.NET How To Cancel Edits on a control? Serial Date Setting Foreground Color Property at Row Level in Datagrid Stored procedure handling 1 parent record and multiple child recor vb.net to c# conversion help please How Many SQL Connections Should I Use? Name 'ADODB' is not declared Error Converting Base 2 Numbers to Base 10 |
|||||||||||||||||||||||