C #: Implementing two abstract lists inside a non-generic class?

Sorry for the stupid title, had no idea how to say it

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

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

So, I assume that I should make List types a sort of abstract list. I would like them to be strongly typed when they were created (but not necessarily if they are problematic). The problem is that the method that moves the selected items from the lists list1 to list2 usually does not have methods for this.

I suppose that the usual solution would be to make the generic class (className <T> thingy), but I'm not sure I can do it (at least I don't know how), because this class inherits from 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

In some answers, I pointed out the optimal solution for creating a Generic Class. However, this can be problematic in WPF. You will need to skip XAML in the top-level generic class.

This would mean that you can do XAML in child classes of the type you don't want to do (unless the code is something you would reuse, but the look is variable). You could also design control with code, I think, but I'm not sure how efficient it is.

I pointed to an IList object, which is an abstract list that many others inherit, and I will use. It's a bit of a hack, but since it won't be used in an open library, I'm fine. Otherwise, I would use a common route.

+6
collections generics c # abstract-class user-controls
source share
3 answers

I don’t know how well designers play with user controls that are generic classes. If this is a problem (which I would have guessed), one way to avoid this is to expose the IList collections, but still use the List<T> , which are built at runtime, to provide type safety in the repository (sample code has been summarized to include only the creation of list objects and properties for their placement, add code for DependencyProperty , etc. as necessary):

 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 drawback is that SetCollection and SubsetCollection display "untyped" object lists.

+3
source share

Something like this or am I shooting at a wide 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)); } } 
+3
source share

As you say, a common class is a way to achieve this, and you can do it when you inherit from a parent class that is not shared, like

  partial class GenericClass1<T> : UserControl { private List<T> _list; } 

EDIT based on comments below

Define typed classes that fix the common type in the class above (and can be used by the designer), i.e.

 partial class IntClass1 : GenericClass1<int> {} partial class StringClass1 : GenericClass1<string> {} .... 

Of course, this really really works if you have a limited / static set of types that you want to keep in your lists

+2
source share

All Articles