Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 6th, 2006, 02:25 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default IComparer implementation

Hi again folks,

I'm trying to convert an IComparer implementation for a SortedList from VB.NET to C#:

The VB.NET works fine, the C# throws an InvalidCastException where noted in the code. Any idea why?

VB.NET code

Code:
Private m_XPanderComparer As XPanderComparer
Private m_ControlList As New SortedList(m_XPanderComparer)

…

‘ Add control to control list
e.Control.Tag = m_NextControlKey
m_NextControlKey += 1
m_ControlList.Add(e.Control.Tag, e.Control)

‘ VB.NET IComparer implmementation
Public Class XPanderComparer
    Implements IComparer

    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
        Dim xp1 As XPander = CType(x, XPander)
        Dim xp2 As XPander = CType(y, XPander)
        Dim result As Integer = 0

        If xp1.Top < xp2.Top Then result = -1
        If xp1.Top > xp2.Top Then result = 1

        Return result
    End Function
End Class
C# Code

Code:
private XpanderComparer _xpanderComparer = new XpanderComparer();
private SortedList _controlList;

…

// Add control to control list
e.Control.Tag = _nextControlKey;
_nextControlKey += 1;
_controlList.Add( e.Control.Tag, e.Control );

// C# IComparer implementation
public class XpanderComparer : IComparer
{

    #region IComparer Members

    public int Compare(object x, object y) 
    {
        try 
    {
        XpanderControl xpander1 = (XpanderControl)x; <=== InvalidCastException
    XpanderControl xpander2 = (XpanderControl)y;
    int result = 0;

    if( xpander1.Top < xpander2.Top )
        result = -1;
    if( xpander1.Top > xpander2.Top )
        result = 1;

    return result;

    } 
    catch (Exception e) 
    {
        MessageBox.Show(e.ToString());
        return 0;
    }
    }

    #endregion

    }
Thanks for any thoughts

Bob

 
Old February 6th, 2006, 02:28 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Forgot the C# SortedList constructor:

public XpanderListControl () {
    InitializeComponent();

    _controlList = new SortedList( _xpanderComparer );


}

Bob






Similar Threads
Thread Thread Starter Forum Replies Last Post
IComparer Chapter 5 HELP PLEASE psychonet BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 1 May 18th, 2007 09:17 AM
pagination implementation vivekkumar_23 Struts 1 July 12th, 2006 05:52 AM
SSL implementation piyush_vish ASP.NET 1.0 and 1.1 Professional 0 January 6th, 2006 07:30 AM
COM Implementation using VB6 haianoop Pro VB 6 0 December 28th, 2005 04:01 PM
c# implementation help cshanmuganathan ASP.NET 1.0 and 1.1 Basics 1 May 28th, 2005 12:08 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.