Yes, I am aware this has been asked and answered before, but nothing fully addresses several issues.
References
Essentially, I'd like to derive from Control
but without exposing certain properties. In my case, I am designing a Container
, which will contain many Element
s, arranged in a programmatic way. I'd like (and may require) that Element
derives from Control
, in order to get all of the drawing and events, but also be able to add Element
objects to the Container
's Children
collection (for obvious reasons).
The only problem is, if Element
derives from Control
, then its Location
property is exposed, and the user could easily destroy the programmatic placement. Naturally, my instinct is to keep all the goodies that come along with Control
and hide/disable the Location
.
I'm aware of the following:
- This violates the SOLID Liskov substitution principle (design by contract)
- This has been asked before
- I could encapsulate
Control
in myElement
and forward all of the calls to those ofControl
- This would be a ridiculous amount of work, and for no real gain.
- The encapsulating
Element
would still not be type-compatible withControl
and could not be used in many cases where a derived form ofControl
would.
I feel there must be some more elegant solution to this (seemingly) recurring problem. Thank you in advance!
Edit
The only way I see to accomplish this is:
public class Element : Control
{
public new Point Location { get; private set; }
}
But then, I have the problem of, how does my container class set the Element
s location? In C++ one would use a friend class. I suppose if I kept this control in its own assembly, I could mark the setter as internal
.
Edit
For those looking to do something this, my solution was: to not do it. Instead, I made a custom object and implemented all of the clicking / dragging, etc myself. It was just too complicated to try and use a Control, while excluding functionality like Location.