Link to home
Start Free TrialLog in
Avatar of soorraj
soorrajFlag for United States of America

asked on

Sorting a Collection<t>

I am trying to do a sort for Collection<T>. I have the VB code that works fine. But i want to do it in C#. I used the developer fusion to convert the code form VB.NET to C#. When i build the C# code it has many errors.

 The CarModel collection calls the SortableBindingList
public class CarModelCollection :  SortableBindingList<CarModal>

 I have attached the VB version of Sort , C# ver of code and my code.

The errors are in the SortableBindingList.cs

below error is on this line
public class SortableBindingList<T>: BindingList<T>,ISortableBindingList

    SortableBindingList<T>' does not implement interface member 'ISortableBindingList.ApplySortCore(System.ComponentModel.PropertyDescriptor, System.ComponentModel.ListSortDirection)'. 'SortableBindingList<T>.ApplySortCore(System.ComponentModel.PropertyDescriptor, System.ComponentModel.ListSortDirection)' cannot implement an interface member because it is not public.


    'SortableBindingList<T>' does not implement interface member 'ISortableBindingList.SupportsSortingCore'.
    'System.ComponentModel.BindingList<T>.SupportsSortingCore' cannot implement an interface member because it is not public.


List<T> items = this.Items; gives this error
    Cannot implicitly convert type 'System.Collections.Generic.IList<T>' to 'System.Collections.Generic.List<T>'. An explicit conversion exists
    (are you missing a cast?)
CarModal.txt
CarModelCollection.txt
ISortableBindingList.txt
PropertyComparer.txt
SortableBindingList.txt
ISortableBindingListVB.txt
PropertyComparerVB.txt
SortableBindingListVB.txt
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

It seems to work. What I did was:

- on the "ISortableBindingList" in SortableBindingList.cs you can hover and then choose from the submenu: Implement interface. Then you can see that the method signatures were wrong, globally speaking they all had to be "public new ..." instead of "protected override"

- use a cast for the "cannot implicitly convert" error on Items.

I have now:
//SortableBindingList.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.ComponentModel;

public class SortableBindingList<T>: BindingList<T>,ISortableBindingList
{

    private bool _sorted = false;
    private PropertyDescriptor _sortProperty = null;

    private ListSortDirection _sortDirection = ListSortDirection.Ascending;
    public SortableBindingList()
        : base()
    {

    }

    public SortableBindingList(IList<T> list)
        : base(list)
    {
    }

    public new bool SupportsSortingCore
    {
        get { return true; }
    }

    public new bool IsSortedCore
    {
        get { return _sorted; }
    }

    public new ListSortDirection SortDirectionCore
    {
        get { return _sortDirection; }
    }

    public new PropertyDescriptor SortPropertyCore
    {
        get { return _sortProperty; }
    }

    public new void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
    {
        List<T> items = (List<T>)this.Items;

        if ((items != null))
        {
            PropertyComparer<T> pc = new PropertyComparer<T>(prop, direction);
            items.Sort(pc);
            _sorted = true;

            _sortProperty = prop;
            _sortDirection = direction;
        }
        else
        {
            _sorted = false;
        }
    }

    public new void RemoveSortCore()
    {
        throw new NotImplementedException();
    }
}

Open in new window

Now at least it compiles. I'm in the process of testing...
Yep, it works.
User generated imageThis is probably not the way you will be using it... (referring to the code I used to sort)
Avatar of soorraj

ASKER

I need to make it work the way i am doing.  BY changing to  public new it complies but it doesn't work.
Ah, that's probably due to the fact that you use it differently, sort from the grid? Can you share some more code?
Avatar of soorraj

ASKER

This is what i have. It is in Winforms. But that should not matter i guess for applying the sort.
There are 4 projects.
Business Layer - CarModal.cs, CarModalManager.cs, CarModalCollection.cs

DataAccess Layer - CarModalDB.cs

MyUtilities- Have the three sort programs that i uploaded before

Presentation - Form.cs
Which has the code
private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = CarModalManager.GetList();
        }

CarModalManager.txt
CarModalDB.txt
ASKER CERTIFIED SOLUTION
Avatar of Robert Schutt
Robert Schutt
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of soorraj

ASKER

Thanks for your help. It works your way too. But i am just curious why i am not able to use the Interface. Probably it is different in c#.