I should have read a little more before posting in exasperation (see
below).  I still think having the innards of the UserControl exposed in the
Controls collection is a bad idea that goes against Encapsulation,
Component "black box" theory, etc.

What I should have read more carefully is this. My code snippet that didn't
work:

=====================
[Browsable(false),
EditorBrowsable(EditorBrowsableState.Never)]
new private Control.ControlCollection Controls
{
  get
  {
    return base.Controls;
  }
}
=====================


was clearly explained in the following truth I read in the language ref
(quoted below)


=====================
A declaration of a new member hides an inherited member only
within the scope of the new member.

class Base
{
  public static void F() {}
}

class Derived: Base
{
  new private static void F() {} // Hides Base.F in Derived only
}

class MoreDerived: Derived
{
  static void G() { F(); } // Invokes Base.F
}

In the example above, the declaration of F in Derived hides
the F that was inherited from Base, but since the new F in
Derived has private access, its scope does not extend to
MoreDerived. Thus, the call F() in MoreDerived.G is valid
and will invoke Base.F
=====================

This was why when my Form accessed the name control's "Controls"
collection, it really wasn't.  Instead of chopping off this Controls
collection property, my "new private" Controls in the NameUserControl was
the one ignored, and the Form invoked the Controls property of the Control
ancestor class of the UserControl.

Since the Control class makes the Controls method public, there is
apparently no way to "truly" hide it, only to "hide" it with a new public
method.  Of course, if the method is there, it will look like it
should/could be used.

Maybe this would discourage use of the Controls collection and force
consumers (forms or whatever) to use the public acessor functions
specifically written to provide limited access to the constituent textbox
controls inside the UserControl:

=====================
[Browsable(false),
EditorBrowsable(EditorBrowsableState.Never)]
new private Control.ControlCollection Controls
{
  get
  {
    throw (new System.NotImplementedException());
  }
}
=====================

This is ridiculous, but how else can you force a consumer control to use
the accessors written to expose only certain properties of the constituent
controls to users of the control (Text property only, for example).

It really does seem that with all the controls exposed so publicly in the
Controls collection, that this is no better than older versions of VB that
let you just start accessing stuff from anywhere.

Jeff

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to