Search code examples
c#listunique

Can I use a C# List in this case?


I have a list of similar types of actions for which I need to create different classes ActionClass1, ActionClass2. Furthermore I have different sites at which I need to perform a list of different actions. Note that the content of this list will depend on the site.

I want to develop a GUI to be able to edit the actions that are performed on a given site SiteClass using c# and WPF.

My idea is now to do the following:

  1. Subclass the different action classes ActionClass1, etc. from a common, abstract action class AbstractActionClass.
  2. Create lists ActionList of AbstractActionClass, which will contain the different action as concrete implementations (e.g. ActionClass1, etc.).
  3. Create a list SiteList of instances of SiteClass, that will contain the sites with their corresponding list of actions and additional information (such as site-location, etc.).

So far, I have a pretty good idea regarding the above. However I will need to now be able to edit the ActionList for each site and style it in the GUI so that all sites with the same ActionList will look the same (e.g. have the same color). I therefore need to be able to compare the ActionList of each instance of SiteClass in SiteList to check if they are different, find out how many different instances there are, and then style them accordingly in the GUI.

So my question is: Will I be able to use lists of type ActionList List<AbstractActionClass> in SiteClass in order to perform comparisons such as 'Unique', etc. to find out the number of unique occurrences of ActionList List<AbstractActionClass> inside SiteList List<SiteClass> and use this information for the styling? Is there a better way?


Solution

  • Short answer: Yes

    Detailed answer. You can create a List<AbstractionClass> and to handle the different rendering there are two approaches you can take:

    1. Add an abstract property to the class that tells you what the class is:

      public abstract string ClassType { get; }
      

    and implement it in each of the different action Lists as a simple

    public override string ClassType { get { return "ActionClass1";}}
    

    then in your rendering code do something along the lines of

    switch (ac.ClassType)
    { 
        case "ActionClass1":
            /// render according to class 1;
            break;
        case "ActionClass2":
            /// render according to class 2;
            break;
      }
    
    1. You could do some simple casting:

      if (ac is ActionClass1)
      {
          /// render according to class 1
      }
      else if (ac is ActionClass2)
      {
         ///  render according to class 2
      }
      
    2. You could abstract the List itself:

      public class ActionClassList : List<ActionClass>
      {
          public abstract string ClassType { get;}
      }
      

    and implement ActionClassList1 and ActionClassList2 to return different ClassTypes.

    On the receiving end you always accept an ActionClassList, but it could be either one of the derived classes you get.