[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1081
  • Last Modified:

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
0
soorraj
Asked:
soorraj
  • 4
  • 3
1 Solution
 
Robert SchuttSoftware EngineerCommented:
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...
0
 
Robert SchuttSoftware EngineerCommented:
Yep, it works.
captureThis is probably not the way you will be using it... (referring to the code I used to sort)
0
 
soorrajAuthor Commented:
I need to make it work the way i am doing.  BY changing to  public new it complies but it doesn't work.
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
Robert SchuttSoftware EngineerCommented:
Ah, that's probably due to the fact that you use it differently, sort from the grid? Can you share some more code?
0
 
soorrajAuthor Commented:
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
0
 
Robert SchuttSoftware EngineerCommented:
Hmm, it does make a bit of a difference, but I have just moved all files to a winforms project and trying from there.

I found an example similar to your code and see there that you can just use:
public class SortableBindingList<T> : BindingList<T>

Open in new window

so without specifically inheriting ISortableBindingList as well. Then I put back the 'protected override' signatures and it works beautifully.

Code is 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>
{

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

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

    }

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

    protected override bool SupportsSortingCore
    {
        get { return true; }
    }

    protected override bool IsSortedCore
    {
        get { return _sorted; }
    }

    protected override ListSortDirection SortDirectionCore
    {
        get { return _sortDirection; }
    }

    protected override PropertyDescriptor SortPropertyCore
    {
        get { return _sortProperty; }
    }

    protected override 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;
        }
    }

    protected override void RemoveSortCore()
    {
        throw new NotImplementedException();
    }
}

Open in new window

0
 
soorrajAuthor Commented:
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#.
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now