views:

153

answers:

2

I was trying to extend the shape class to contain an additional variable but have found the class is sealed. How can I achive this simply, using an alternate implementation approach?

Is creating a new class and storing a shape an passing all the method calls through the easiest approach; I'm sure there is a better way though?

+2  A: 

If the variable you want to add is one which you would only ever be reading from, and is calculated as a result of other members of the Path class, you could add an extension method which would return your value.

However, if this is not the case:

Consider creating a class which inherits from Path's parent - Shape, which is not sealed. Then add a private Path member to this. You can grab all the shape method calls for free and would only have to provide wrappers for the unique members of Path.

+1  A: 

WPF has a powerful concept named Attached Properties. I'm not sure what you are trying to do, but perhaps you can solve your problem by using this concept. To provide an exampe, the Grid.Row and Grid.Column properties are properties attached to elements inside a Grid.

<Grid>
  ...
  <TextBlock Grid.Row="1" Grid.Column="2" ... />
</Grid>

The TextBlock class is unaware of the Grid.Row and Grid.Column properties used by the Grid.

You can read more about attached properties on MSDN.

Martin Liversage
I should have been slightly more specific; in this instance the variable is an object, so I don't think this will work, good suggestion though
Sebastian Gray
How would this approach not work? An attached property can be of any type.
siz
I went with a slightly different approach and just created a new object which stored the reference to the path and the reference to the other object I needed to link to. I'm sure this can be done in a more elegant way but seems to work OK for what I'm doing.
Sebastian Gray