views:

79

answers:

2

Hi,

I have two lists based on the same class. I would like one list to inherit the values of the other list's class properties.

class feature {
   Guid id;
   string featureName;
   string featureType;
   .....
}

List <feature> companyFeatures;
List <feature> divisionFeatures;

The list of features is set at the company level.
When a division is created it should inherit all of the features from the company feature list. (Not copy)

If the division has a different property value in a specific feature than that of the company , the user wants to "save" this property's value in the division's feature list.

This gives the user the ability to add features at a later date at the company level and see these new features at the division level.

Usually there will not be many items in the division list as they are the same as the company list.

I have tried to join both lists using linq (an outer join). It seems to fail when there is no entry for a list element at the division level.

I guess I am doing something wrong (I might not understand outer joins correctly) Any help will be appreciated.

By the way is there a design pattern recommended for implementing inheritance.

Be happy and have a wonderful day

================================================================================

Adding an example (Hopefully this will clarify my objective)

List <feature> companyFeatures = new List <feature> 
{
    new <feature> { Guid1 , "Color" , "Text" , .... },
    new <feature> { Guid2 , "address" , "Link" , .... },
    new <feature> { Guid3 , "logo" , "Picture" , .... }
}

List <feature> divisionFeatures = new List <feature> 
{
    new <feature> { Guid2 , "address" , "text" , .... },
}

The feature list I am looking for after the "inheritance" should be:

{
    {Guid1, "Color" , "Text" , ...},
    {Guid2, "address" , "text" , ...},
    {Guid3, "logo" , "Picture" , ...}
}

Note that Guid2 now has a value of text for the type property. Hope this clarifies.

+1  A: 

This isn't, technically, inheritance -

If the goal here is to return a collection of all features in the companyFeatures and divisionFeatures lists, then you can just do:

IEnumerable<feature> totalFeatures = companyFeatures.Concat(divisionFeatures);

The Enumerable.Concat method will return an IEnumerable<T> which contains the elements from both lists.

Reed Copsey
Thanks,I understand your answer but I guess my question is not clear enough. I will add an example as my objective is that division properties will override company properties.
Julian
@Julian: My answer solves that objective.
Timwi
@Timwi: Thanks I have just got the example edited. I am still trying to understand the details of your answer (Below).
Julian
A: 

Correct me if I misunderstood. You have a Company class and a Division class and they both have a List<Feature> field? Something like...

public class Company {
    // ...
    public List<Feature> CompanyFeatures;
    // ...
}
public class Division {
    public Company Company;  // The company to which the division belongs
    // ...
    public List<Feature> DivisionFeatures;
    // ...
}

If this is the case, then I would suggest to turn the feature list into a read-only property and have it automatically take care of the “inheriting”. For exapmle...

public class Division {
    public Company Company;  // The company to which the division belongs
    // ...
    private List<Feature> _divisionFeatures;  // private now
    public IEnumerable<Feature> DivisionFeatures {
        get {
            // Take all the features for the divison...
            return _divisionFeatures.Concat(
                // ... and add all the company features except for those that
                // have the same name as one of the division features.
                Company.CompanyFeatures.Where(cf => !_divisionFeatures
                    .Any(df => df.Name == cf.Name))
            );
        }
    }
    // ...
}

Alternatively, of course you can still make the List<Feature> public (so that you can still manipulate it from outside) and call the property something like DivisionFeaturesWithInheritance.

Timwi
@Timwi: As far as I understand your code you add any feature that is "new" at the division level to the list of features at the company level. Nice code but. I am trying to override the specific fields for features that exist in both lists. (I hope my example above clarifies) In any case thanks for the help.
Julian
@Julian: You misunderstood the code. It takes all the features from the division level and adds only the ones from the company level that aren’t already in the division level.
Timwi
@Timwi: Accept my apologies you are right. Your solution is perfect for my problem. Thanks.I am able to click the answer check mark but
Julian
@Timwi: I am new to the system and can not raise the useful indicator yet.
Julian
That’s OK, don’t worry about it ☺
Timwi