Note that there are some explanatory texts on larger screens.

plurals
  1. PODeriving from Control but hiding properties
    primarykey
    data
    text
    <p>Yes, I am aware this has been asked and answered before, but nothing fully addresses several issues.</p> <p><strong>References</strong></p> <ul> <li><a href="https://stackoverflow.com/questions/1528/hiding-inherited-members-in-c">Hiding inherited members</a></li> <li><a href="https://stackoverflow.com/questions/1528371/hiding-unwanted-properties-in-custom-controls">Hiding unwanted properties in custom controls</a></li> </ul> <p>Essentially, I'd like to derive from <code>Control</code> but without exposing certain properties. In my case, I am designing a <code>Container</code>, which will contain many <code>Element</code>s, arranged in a programmatic way. I'd like (and may <em>require</em>) that <code>Element</code> derives from <code>Control</code>, in order to get all of the drawing and events, but also be able to add <code>Element</code> objects to the <code>Container</code>'s <code>Children</code> collection (for obvious reasons).</p> <p>The only problem is, if <code>Element</code> derives from <code>Control</code>, then its <code>Location</code> 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 <code>Control</code> and hide/disable the <code>Location</code>.</p> <p>I'm aware of the following:</p> <ul> <li>This violates the SOLID Liskov substitution principle (design by contract)</li> <li>This has been asked before</li> <li>I could encapsulate <code>Control</code> in my <code>Element</code> and forward all of the calls to those of <code>Control</code> <ul> <li>This would be a ridiculous amount of work, and for no real gain.</li> <li>The encapsulating <code>Element</code> would still not be type-compatible with <code>Control</code> and could not be used in many cases where a derived form of <code>Control</code> would.</li> </ul></li> </ul> <p>I feel there must be some more elegant solution to this (seemingly) recurring problem. Thank you in advance!</p> <p><strong>Edit</strong></p> <p>The only way I see to accomplish this is:</p> <pre><code>public class Element : Control { public new Point Location { get; private set; } } </code></pre> <p>But then, I have the problem of, how does my container class set the <code>Element</code>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 <code>internal</code>.</p> <p><strong>Edit</strong></p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload