Reputation: 597
I have the following classes. For each of the control classes, I'd like to expose the data member via a property.
public class BasicData {}
public class AdvancedData : BasicData { }
public class BasicControl
{
private BasicData data;
public BasicData DataHide;
public virtual BasicData DataOverride;
}
public class AdvancedControl: BasicControl
{
private AdvancedData data;
public new AdvancedData DataHide; //Hide
public override BasicData DataOverride; //Override
}
If I make the property virtual, then my AdvancedControl
class has to expose the data as BasicData
and not AdvancedData
(which I'd prefer).
I can instead hide the base class's property but I've read that hiding should be avoided where possible.
I want to have just one data property exposed for each Control class and I want this property to have the same name in each class i.e. Data
. For BasicControl
this should be BasicData
. For AdvancedControl
this should be AdvancedData
.
What's the best way of designing my classes to achieve this?
Upvotes: 1
Views: 407
Reputation: 43876
You could use a generic approach:
public class BasicData {}
public class AdvancedData : BasicData { }
public class BasiccControl<T> where T : BasicData
{
private T data;
public T Data;
}
public class AdvancedControl : BasicControl<AdvancedData>
{
}
So you don't need to override or hide the members in each derivate and still have type safety (as long as you provide the correct generic arguments for T
).
Upvotes: 1