Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# : Implementation of two abstract lists within a non-generic class?

Sorry about the stupid title, had no idea how to word this

I'm creating a class that has two lists of the same type. It's used to copy a reference to a object in the first list to the second list.

While both lists will be of the same type (hold the same type of object) it can be different each time this class is initialized.

So I'm guessing I should make the List types as some kind of abstract list. I would like to ensure they will be strongly typed when instanced (but not neccesary if problematic). The problem is inside the method that moves selected items from list1 to list2 abstract list types normally don't have methods for that.

I guess the normal solution would be to make the class generic ( className<T> thingy ) but I'm not sure I can do that ( at least I don't know how ) because this class inherits a WPF UserControl.

Here is the Code:

public partial class SubsetSelectionLists : UserControl
{
    public static DependencyProperty SetCollectionProperty = DependencyProperty.Register("SetCollection", typeof("Need a abstract list type here"), typeof(SubsetSelectionLists));
    public static DependencyProperty SubsetCollectionProperty = DependencyProperty.Register("SubsetCollection", typeof("Need a abstract list type here"), typeof(SubsetSelectionLists));

    public "Need a abstract list type here" SetCollection
    {
        get
        {
            return ("Need a abstract list type here") GetValue(SetCollectionProperty);
        }
        set
        {
            SetValue(SetCollectionProperty, value);
        }
    }

    public "Need a abstract list type here" SubsetCollection
    {
        get
        {
            return ("Need a abstract list type here")GetValue(SubsetCollectionProperty);
        }
        set
        {
            SetValue(SubsetCollectionProperty, value);
        }
    }

    public SubsetSelectionLists()
    {
        InitializeComponent();
        SubsetSelectiongrid.DataContext = this;
    }

    private void selectionBtnClick(object sender, RoutedEventArgs e)
    {
        SubsetCollection.AddTheseItems(SET.SelecctedItems)
    }

    private void removeBtnClick(object sender, RoutedEventArgs e)
    {
        SUBSET.RemoveTheseItems(SUBSET.SelectedItem);
    }
}

EDIT: Solution

Some answers pointed me towards the optimal solution of creating a Generic Class. However this can be problematic in WPF. You would have to skip the XAML on the top level generic class.

This would mean that you could do the XAML in the type specific child classes which isn't something you would like to do (unless only the code is something you would re-use but the look is variable). You could also have designed the control using code I guess but I'm not sure how effective that would be.

I was pointed towards the IList object which is a abstract list that many others inherit from and I'm gonna use. It's a bit of a hack but as this will not be used in a open library I'm ok with it. Otherwise I would use the Generic Route.

like image 269
Ingó Vals Avatar asked Feb 02 '11 12:02

Ingó Vals


2 Answers

I don't know how well the designers play with user controls that are generic classes. If that is a problem (which I would guess), one way to get away from that is to expose the collections IList but still use List<T> that are constructed in runtime to have type safety in the storage (code sample reduced to include only list object creation and properties for exposing them; add code for DependencyProperty and so on as needed):

public class YourControl : UserControl
{

    // this method will set up the internal lists for accepting
    // objects of the specified type only
    public void SetListType(Type containedType)
    {
        var listType = typeof(List<>).MakeGenericType(new[] { containedType });
        SetCollection = (IList)Activator.CreateInstance(listType);
        SubsetCollection = (IList)Activator.CreateInstance(listType);
    }
    public IList SetCollection { get; private set; }
    public IList SubsetCollection { get; private set; }
}

// usage example:
theControl.SetListType(typeof(string));
theControl.SetCollection.Add("some string"); // works ok
theControl.SetCollection.Add(42); // fails, 42 is not a string

The obvious downside is that SetCollection and SubsetCollection expose "untyped" object lists.

like image 124
Fredrik Mörk Avatar answered Oct 31 '22 19:10

Fredrik Mörk


Something like this or am i shooting wide of the mark?

public partial class SubsetSelectionLists<T> : UserControl
{
    public List<T> SetCollection { get; set; }

    public List<T> SubsetCollection { get; set; }

    public SubsetSelectionLists()
    {
        SubsetSelectiongrid.DataContext = this;
    }

    private void selectionBtnClick(object sender, RoutedEventArgs e)
    {
        SubsetCollection.AddRange(SetCollection);
    }

    private void removeBtnClick(object sender, RoutedEventArgs e)
    {
        SubsetCollection.RemoveAll(x => SetCollection.Contains(x));
    }
}
like image 37
Hawxby Avatar answered Oct 31 '22 17:10

Hawxby